View Full Version : Run Macro if =2


Groundrush
07-24-2003, 07:10 AM
I have a Subform called "SubformCount" which keeps a count on certain Jobs

What I need is for a macro to activate when the count reaches 2 and notify the user that a letter needs to be sent.

I have been trying to use a message box action in a macro with the condition set to "[SubformCount]=2"

but I can't get it to work.

I also need one to activate when the count reaches 4 and more.



thanks in advance.:)

jtvcs
07-24-2003, 08:37 AM
Have you tied the macro to the after update event of the specific field on your sibform ? Also I would use a conditional macro which would check all variations you need. = 2 then Msgbox 'about two', >3 then Msgbox '4 or more'. HTH JT

Groundrush
07-25-2003, 02:52 AM
Yes I have tried that but no joy.

Is it possible that the problem is because the subform is based on a qry, or is it because of an error in the condition that I am using?.

the condition is:
[Forms]![frmAlarms]![SubfrmCount]![CountOfFalseAlarm]=2

FrmAlarms is the main form
SubfromCount is the subform in question
and the countOfFalseAlarm] is the field name in the subform


thanks:confused:

IMO
07-25-2003, 05:05 AM
Try this in the OnCurrent Event of the form...

Private Sub Form_Current()

If Me.[SubformCount]![countOfFalseAlarm] = "2" Then
DoCmd.RunMacro "YourMacro", , ""
End If

If Me.[SubformCount]![countOfFalseAlarm] > "3" Then
DoCmd.RunMacro "YourOtherMacro", , ""
End If

End Sub


IMO

IMO
07-25-2003, 05:22 AM
Sorry, missed out your message box bit!!

Private Sub Form_Current()

If Me.[SubformCount]![countOfFalseAlarm] = "2" Then
DoCmd.RunMacro "YourMacro", , ""
MsgBox "YourMessageHere"
End If

If Me.[SubformCount]![countOfFalseAlarm] > "3" Then
DoCmd.RunMacro "YourOtherMacro", , ""
MsgBox "YourMessageHere"
End If

End Sub


IMO

Groundrush
07-25-2003, 05:41 AM
Thanks for your time IMO

I have tried your code and I am getting error "2465"
cant find the field "I"
see attached

Maybe I have the incorrect form/field names

should it be:
Private Sub Form_Current()
If Me.[name of subform]![name of field in subform] = "2" Then
DoCmd.RunMacro "YourMacro", , ""
MsgBox "YourMessageHere"
End If
End Sub

ty

IMO
07-25-2003, 05:47 AM
Error "2465" means Access can't find a field name. What is the Name of your Subform and the Name of the field with the value in it?

IMO

Groundrush
07-25-2003, 06:01 AM
Subform Name is "SubfrmCount"

Field Name is "CountOfFalseAlarm"

I have just checked my spelling and made a change.

The message I now get is "You entered an expression that has no value"



:confused:


the main form that all this takes place in is called "frmAlarms"

IMO
07-25-2003, 06:17 AM
Can you post a stripped down version of your DB and I'll take a look? It sounds like there is no value in the field.

IMO

IMO
07-25-2003, 06:26 AM
Or you could try adding this to the begining of the code...

Private Sub Form_Current()

If Me.[SubfrmCount]![countOfFalseAlarm] = "" Then
End If


IMO

Groundrush
07-25-2003, 06:51 AM
Access 2000

IMO
07-25-2003, 08:23 AM
I can't seem to work out what you exactly want your DB to do (Then again it is Friday and all I can think of at the moment is the pub!), add a record every time a ComboBox is selected? I've posted back a version using 'DLookUp' rather than a Subform which seems to work if a record reaches 2 or is >3. Hope it's helped a little.

IMO

Groundrush
07-28-2003, 05:01 AM
Thanks IMO


I happen to have another thread that has started to pick up on the same thing and I have been getting some help from Robert Dunstan.

see http://www.access-programmers.co.uk/forums/showthread.php?s=&threadid=51906

He has modified your code and put it in the Got Focus of the Operative No

at the moment it almost works but I am getting a runtime message 2427 when ever I try and enter a new record that has a new property.

see my latest version

Thanks

IMO
07-28-2003, 05:56 AM
Rather than use a Query, why not use DCount to count the number of Alarms for each Job Number. Create a text box on the form and enter...

=DCount("[FalseAlarm]","Alarms","[JobNo] = '" & [Forms]![frmAlarms]![JobNo] & "'AND [FalseAlarm] = 'YES'")

as the Control Source. You can then have the OnCurrent event of the form check the TextBox for the value.

Private Sub Form_Current()

If Me.YourTextBox = "2" Then
MsgBox "YourMessageHere"
End If

If Me.YourTextBox > "3" Then
MsgBox "YourMessageHere"
End If

End Sub

IMO

IMO
07-28-2003, 06:02 AM
Oops! Forgot the attachment!

IMO

Groundrush
07-28-2003, 07:13 AM
Wow that's what I need

Although I cant have it on the Job No, it needs to be on the property as we may get 3 alarm calls at the same property with 3 different Job No's

Is there a way to make it work by using the property in the qry that is linked to the job No instead?

I have just tried to include the property in the table but I can't get the combo box to save the details of the Job No and Property at the same time.

Also What would you suggest doing to cancell the Code, say when activating a command button?.

i.e when a property first reaches 2 false alarms the first code will activate instructing the user that a letter needs to be sent.
When the user clicks on send first action letter, it will de-activate the code until it reaches 4 or more causing the second part of the code to activate and so on..

maybe thats another ball game on another thread..

I am very grateful for your help, thanks

IMO
07-28-2003, 08:20 AM
Here's a quick way to disable the code using toggle buttons, I'll have to look at the other problems tomorrow

IMO

IMO
07-29-2003, 12:15 AM
Change the DCount to...

=DCount("[FalseAlarm]","qryAlarm","[BG_SITE] = '" & [Forms]![frmAlarms]![JobNo].[Column](1) & "'AND [FalseAlarm] = 'YES'")

Hope this is what you needed

IMO

Groundrush
07-29-2003, 01:07 AM
IMO

Thank you very much, you have helped me a great deal.

I tried changing the code last night at home to pick up the Property in the qry, but I did not change [JobNo] to [JobNo].Column](1) as you have this morning

It works Perfectly now.


I have also looked at your quick way to disable the code using toggle buttons and will work on that now

Thanks again:)

IMO
07-29-2003, 01:10 AM
Glad it works, we got there in the end!

IMO

Groundrush
07-29-2003, 04:56 AM
IMO

You have helped me more than I realised.

I didn't notice that you had put some code in the main form on Current

Private Sub Form_Current()
If Me.FirstLetterSent = False And Me.LookUpAlarms = 2 Then
MsgBox "This Property has had 2 False Alarms within the last 12 Months, please send out a First Action Letter."
End If

If Me.SecondLetterSent = False And Me.LookUpAlarms > 3 Then
MsgBox "This Property has had 4 or more False Alarms within the last 12 Months, please send out a Second Action Letter"
End If

End Sub

I have been sitting here trying to work out how to go about doing something similar and found your code.

thanks

I will test it properly now

cheers:)

IMO
07-29-2003, 05:02 AM
I forgot to mention I'd added that:o

IMO