Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access IF THEN Question 1

Status
Not open for further replies.

ccepaulb

Technical User
Jan 29, 2004
89
US
I have a button on an Access form that when clicked runs a macro that echoes, runs a query and maximizes the window. Now I added an option group that has two check boxes. What I want to do is, when the button is now clicked it will run either macro1 or macro2 based on which check box is checked.
Macro1 would run a query that sorts on Field A and Macro2 would run a query that sorts on Field B.
I thought it would be easiest (not good with code )to create 2 separate queries and 2 macros that would run either choice. With that said, how would I add an IF THEN statement into a query.
So it would say... when button is clicked and check box 1 is checked, then run macro1 that runs query1, if check box 2 is checked, then run macro2 that runs query2.
I know some more seasoned Access uses are saying there is a better way, but without being very knowledgeable with writing code, that's the best I could think of
Any suggestions?

Thanks, Paul
 
I was playing around with something like this (doesn't work though)

Private Sub ViewDataButton_Click()

If ViewCheck1 = 1 Then
DoCmd.OpenQuery "ItemAcctVoids"
Else
DoCmd.OpenQuery "ItemAcctVoids2"
End If

End Sub
 
If you want to run one or the other, you need to use Option buttons in an Option Group rather than check boxes. That way only ONE option can be selected.

When you create the frame or option group, if the wizard button (looks like a magic wand) is selected in the Tools toolbar, it gives you the chance to assign a value to each option. You give your first option the value of 1, and 2 to the next option.

In your code, you need to refer to the frame or option group in which the Option buttons are sitting.

For example, assuming that your frame is called "fmeVoid", your code would read as follows:

Private Sub ViewDataButton_Click()
If Me.fmeVoid = 1 Then
DoCmd.OpenQuery "ItemAcctVoids"
Else
DoCmd.OpenQuery "ItemAcctVoids2"
End If
End Sub
 
Hi Paul,

Sorry about the delay, but here are the answers to your additional questions.

To maximise the query, try DoCmd.Maximise as the next command after your DoCmd.OpenQuery.

You can use the same command in your report, by setting the "On Open" event for the report to DoCmd.Maximise.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top