If Statement for Running a Macro

Sykesjas

Registered User.
Local time
Today, 21:16
Joined
Jun 14, 2010
Messages
10
Hi,

I have a subform which calculates a number of hours. However I want to be able to run a macro if the total hours are 37 and over.

For example

If Field - Number of hours, total sum is greater than or equal to 37 and you click save then it runs a macro which runs an append query.

If the total hours is less than 37 then it wont allow the user to run the macro

What would be the best way to go about this?

Thanks,

Jason
 
You need something that deals with either visible or enable

Behind the textbox, or one of the form events, that has the total place in something like

If me.txtTotal.value>37 then
me.commadbuttonname.visible=true
else
me.commandbutton.visible=false
End if

Another option

if me.txtTotal.value >37 then
me.commandbutton.enable=True
else
me.commandbutton.enable=False
End if
 
Thanks for your response

The name of the field with a total is number of hours and the name of the button is Btn AppendQuery

Where would I place them in your code? I am having difficulty trying to get your code to work
 
* its the If me.txttotal Im having particular trouble with
 
If I am understanding what you are saying correctly then you do not want to play with the visible or enabled properties of the command button as the command saves the record as well as running the macro.

Instead you need to amend the code where the macro is run using an if statement to ensure the macro is only run when the condition is met

i.e.

Code:
If Me.YourTotalHoursControlNameHere >37 Then
'Your run macro code here
End if
 
thanks very much for your help I managed to get this working on a test database. However to get this working in my actual database im having diffculty because the field i want to base the code on is in a sub form and the command button is outside of this subform

this is the current code which I have managed to work on my test

If Me.Text17.Value > 37 Then
Me.Command13.Visible = True
Else
Me.Command13.Visible = False
End If


however the text17 in my actual database would be in a subform.
 
Why are you using the visible property?

Do you not need to save records where text17's value is less than 37?

I was under the impression that you always wanted to be able to save the record using the save button, but that the save button should also run a macro (which runs your append query) when text17's value is >37????


To refer to a subforms control, use the following:-

Code:
Forms!YourMainFormNameHere!YourSubFormNameHere!YourSubFormControlNameHere
 

Users who are viewing this thread

Back
Top Bottom