Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
| Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 5th, 2004, 04:47 AM
Authorized User
 
Join Date: Mar 2004
Location: , , Latvia.
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default Button acts depending on radio button values

Hello!

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?

Thanks,
Janis

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

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
Else
    '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
Else
    '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.



Ciao
J├╝rgen Welz
Edmonton AB Canada
jwelz@hotmail.com
  #3 (permalink)  
Old March 7th, 2004, 05:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Melbourne, Vic, Australia.
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:
Code:
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
Steven

I am a loud man with a very large hat. This means I am in charge
  #4 (permalink)  
Old March 9th, 2004, 11:05 AM
Authorized User
 
Join Date: Mar 2004
Location: , , Latvia.
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much! :D

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

Thanks,
Janis :)

  #5 (permalink)  
Old March 10th, 2004, 12:53 AM
Friend of Wrox
 
Join Date: Sep 2003
Location: Madison, Wisconsin, USA.
Posts: 451
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Ben Horne
Default

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:
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.

Thanks,
Janis :)

Ben Horne
Madison Area Technical College student
-------------------------
http://community.webshots.com/user/valerian114

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Radio button gaurisharya C# 2005 2 March 16th, 2008 06:43 AM
radio-button grstad HTML Code Clinic 3 February 28th, 2006 08:32 AM
depending on radio button value Drop down menu MuthuAL Classic ASP Databases 5 December 29th, 2004 01:54 AM
Get values of dynamically created radio button con karthick General .NET 0 November 5th, 2004 07:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.