View Single Post
  #2 (permalink)  
Old March 5th, 2004, 12:49 PM
jurgenw jurgenw is offline
Authorized User
Join Date: Feb 2004
Location: Edmonton, Alberta, Canada.
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts

This is a rather elementary question so it is necessary to attempt to be clear and concise. The event procedures written by you and the wizards are VBA statements in a structured programming language. There are two basic structures that will address your needs. Given that you suggest that you have two option buttons, an If statement should address your needs. If you had more options to choose from, a Select Case structure might be more appropriate for readability and maintainability.

The basic code would be:

If Me.Option1 = True Then
    'the script portion that performs the action for that situation goes here
    'the script portion that performs the alternate action goes here
End If

where the script portion is just that portion of the code that actually performs the action. The If to End If section of code begins after the line that begins 'Private Sub...' and any line that starts with 'On Error...' You have to substitute the name of your option buttons with those in the example and any wizard generated code that has to do with Exit Sub and End Sub after the code that performs the action should remain the same.

The easiest way to do this is create two buttons, let the wizard generate the code for both, add the line with the If test in the example above before the part that does the first action, add the Else line and then copy and paste the alternate action portion of the code from the other button, paste it after the Else statement and then add the End If in the next line. Then delete the alternate action button from your form and delete all the code written for the deleted button including the 'Private Sub...' to 'End Sub' and all the lines between.

If your option buttons are in an option group, you can test the option group for the option button value rather than checking a button for being 'True' (selected). In that case the code would be:

If Me.OptionGroup = 1 Then
    'action 1
    'action 2
End If

Either of the examples could also have been written with an ElseIf but that requires a 2nd test:

If Me.OptionGroup = 1 Then
    'action for the option button whose value is set to 1
ElseIf Me.OptionGroup = 2 Then
    'action for the option button whose value is set to 2
End If

If you post the scripts for both of the buttons, it will be easy to see the names of your controls and offer more precise advice as well as advice on how you may want to revise the names of your controls. You should also post the names of your option group (if you are using one) and the names of your option buttons.

J├╝rgen Welz
Edmonton AB Canada
Reply With Quote