Opening a form depending on which option button I click on

George_E

Registered User.
Local time
Today, 23:34
Joined
Sep 28, 2009
Messages
32
Hey guys, unfortunately my vba knowledge is very limited right now....

I have a form with 2 option buttons and a button.

If option1 button is clicked on and the form button is clicked the form opened is cars2.
If the option2 button is clicked and the form button is clicked, the form cars 3 will open. Below is my code which I have attempted but its not compiling properly:

Private Sub Command10_Click()
If option1.Enabled = True Then
DoCmd.OpenForm "cars2"
Else
If option2.Enabled = True Then
DoCmd.OpenForm "cars3"
End If
End If
End Sub

Does anyone know where I am going wrong, or better still, know how to do this?

Kind regards

George
 
If you by Optionbutton mean Radiobuttons, the they usually belongs to a OptionGroup. If so you have to interrogate the
value of the optionGroupe and not the individual buttons.
You can use a Select Case instead of If's

Code:
Private Sub Command10_Click()
   Select Case me!NameOfOptionGroup
   Case 1
     DoCmd.OpenForm "cars2"
   Case 2
     DoCmd.OpenForm "cars3"
   Case Else
     'do nothing
   End Select
End Sub

JR
 
If you by Optionbutton mean Radiobuttons, the they usually belongs to a OptionGroup. If so you have to interrogate the
value of the optionGroupe and not the individual buttons.
You can use a Select Case instead of If's

Code:
Private Sub Command10_Click()
   Select Case me!NameOfOptionGroup
   Case 1
     DoCmd.OpenForm "cars2"
   Case 2
     DoCmd.OpenForm "cars3"
   Case Else
     'do nothing
   End Select
End Sub
JR

Hello Jr, thankyou for your response.

I know this may be a it of a silly question but how could I get the NameOfOptionGroup, each radio button has a separate name :confused:

Thank you again for your response

George
 
Hi, George.

You do not indicate that you have your options buttons in an Option Group control. If not, you need to place your option buttons in an Option Group control. Then you can just check the value of the Option Group.

For example, if I have created an options group control and named it "optCarTypes", and I have two Option Buttons placed in the Option Group control, I would have two values (normally 1 and 2) that can be returned as the value of the Options Group control. You can also set the default value of the Option Group control to be 0 (Zero). This will cause neither of the options to be selected when the form open.

Using this type of setup you can control when your command button that actually opens a form is available. I prever to always be proactive with my application programming and would have the default value for my command button to be "enabled = no" and the default value of my Option Group control to be 0 (zero). Then I would use the after update event of the Option Group control to enable the command button only when the user had made a selection from the Option Group control.

This way, the user can only click on the command button after is enabled. When they do click on the command button I would then use the On Click event of the command button to check the value of the "optCarTyes" group control to see which of your forms to load using a select case statement, like:

Select Case Me.optCarTypes
Case 1 'use this option only when option button one is clicked
'Open one form here
DoCmd.OpenForm "cars2"
Case 2 'use this option only when option button two is clicked
'Open one form here
DoCmd.OpenForm "cars3"
End Select
 
know this may be a it of a silly question but how could I get the NameOfOptionGroup, each radio button has a separate name :confused:
[/QUOTE]

See MR B. replay

JR
 
Last edited:
Hey guys I now understand what you guys meant by an option group and thus the problem is solved.

I appreciate your help and without you both I wouldnt have solved the problem :)

Geore
 

Users who are viewing this thread

Back
Top Bottom