p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access (http://p2p.wrox.com/forumdisplay.php?f=18)
-   -   Button acts depending on radio button values (http://p2p.wrox.com/showthread.php?t=10128)

janise March 5th, 2004 04:47 AM

Button acts depending on radio button values

Could someone, please, explain how to program a button to act one way if one radio button's choice in a form is selected and in another way if the second choice is selected?

I can get both command scripts from making a button for each action separately, but how can I now combine them?


jurgenw March 5th, 2004 12:49 PM

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

Steven March 7th, 2004 05:01 PM

If you've got a whole heap of options, it might be best to steer clear of a massively nested IF statement, an use Case Select statements. eg:

Select Case Me.OptionGroup
     Case 1
       'Do Something
     Case 2
       'Do Something Else
     Case 3
       'Do Something Else again
'Keep adding as many cases as needed
     Case Else
       'Nothing was selected
End Select


I am a loud man with a very large hat. This means I am in charge

janise March 9th, 2004 11:05 AM

Thank you very much! :D

There were some problems with this but I managed to solve them too by myself.

Janis :)

Ben Horne March 10th, 2004 12:53 AM

Just remember that since you want Access to execute VBA code, you CANNOT have a ' sign in front of code you want executed. This is because Access treats that kind of stuff as comments so therefore, comments are not executed

Hope This Helps,


quote:Originally posted by janise
 Thank you very much! :D

There were some problems with this but I managed to solve them too by myself.

Janis :)

Ben Horne
Madison Area Technical College student

Go there. I have a lot of awesome photos that I rendered myself

All times are GMT -4. The time now is 11:46 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.