VBA Buttons

  • Thread starter Thread starter J
  • Start date Start date
J

J

I have a form with an update button. When the button is clicked, it
executes queries and macros. What I also need this button to do is be
able to see if a check box located on another form is checked or not
and then be able to run the code that is based on the check box. The
code for the check box is supposed to run separate queries depending on
the status of the check box. Any help on this topic is appreciated.
If you need more information to help me with my problem just let me
know.
 
J said:
I have a form with an update button. When the button is clicked, it
executes queries and macros. What I also need this button to do is be
able to see if a check box located on another form is checked or not
and then be able to run the code that is based on the check box. The
code for the check box is supposed to run separate queries depending on
the status of the check box. Any help on this topic is appreciated.
If you need more information to help me with my problem just let me
know.

If Forms!frmMyFormName!chkMyCheckBox Then
'Run the queries
End If

If your check box is on a subform then reference it through its parent:

If Forms!frmMyFormName!sfrmMySubForm!chkMyCheckBox Then
'Run the queries
End If

HTH - Keith.
www.keithwilby.com
 
I have some code written but it says that It can't find the form that I
described. I already checked to see if the spelling of the form name
matched up and it did. Here's the code I used, maybe there's some
small detail I'm overlooking:

If [Forms]![Status wk 01]![chk1] = True Then
DoCmd.OpenQuery "Status wk 01 Jobs Not in Job Card Table"
Else
DoCmd.OpenQuery "Status wk 01 Serial Numbers not in Job Card Table"
End If
 
Since you have spaces in the query name I suggest you surround it with []
and try that.

Also, are you trying to open a FORM or a QUERY? You said form twice in your
message and then your code is trying to open a query.
 
The above IF statement is meant to look at a check box on a form and
then determine whether the box is checked or not. If the box is
checked, it runs a query, if the box is not checked, it runs a separate
query. So, my code looks at the check box oln my form in order to
determine which query to open, does that make better sense now? When I
click the update button it is supposed to run a variety of queries and
macros as well as check this form after the previous queries and macros
have been run. Here is the full click event code just in case it might
make a difference:

Private Sub cmdUpdate_Click()
DoCmd.RunMacro "WarningOff"
DoCmd.OpenQuery "Archive"
DoCmd.OpenQuery "Remove Archive(From Status Table)"
DoCmd.RunMacro "CreateTableOfDataNotInJobCardTable"
DoCmd.RunMacro "WarningOn"
If [Forms]![Status wk 01]![chk1] = True Then
DoCmd.OpenQuery "Status wk 01 Jobs Not in Job Card Table"
Else
DoCmd.OpenQuery "Status wk 01 Serial Numbers not in Job Card Table"
End If
MsgBox ("Update Complete")
 
Did you try the following?

DoCmd.OpenQuery "[Status wk 01 Serial Numbers not in Job Card Table]"

Not the addition of the [] around the name of the query.
 
Using:

If [Forms].[Status wk 01].[chk1] = True Then
DoCmd.OpenQuery "[Status wk 01 Jobs Not in Job Card Table]"
Else
DoCmd.OpenQuery "[Status wk 01 Serial Numbers not in Job Card Table]"
End If
MsgBox ("Update Complete")

I get the same message which says something like

"Run Time Error 2450"
"Status cannot find the form "status wk 01" referred in a macro,
expression or visual basic code"
 
Ok.

The correct way to refer to a Form Control is
[Forms]![Status wk 01]![Chk1]

Do you have a form named "Status wk 01"? Is the form open? I know you
said it exists, but it must be open AND it must not be a subform.

Where are you running this code? Where is the button cmdUpdate? If it is
on the form "Status wk 01", then you can use

If Me.Chk1 = True Then
DoCmd.OpenQuery "[Status wk 01 Jobs Not in Job Card Table]"
Else

End If

Me is a sshorthand way to refer to the form, subform, report, or subreport
that is calling the code.
 
Back
Top