Using option group to open form with command button (1 Viewer)

iainmid

Registered User.
Local time
Today, 07:03
Joined
Mar 1, 2007
Messages
35
I have an option group with two options form1 and form2. What do i have to do to use a command button to open form1 or form2 depending on the selection from the option group.
 

boblarson

Smeghead
Local time
Yesterday, 23:03
Joined
Jan 12, 2001
Messages
32,059
If your option group values are 1 and 2 and your forms are named form1 and form2 then you could just use
Code:
   DoCmd.OpenForm "form" & Me.YourOptionGroupNameHere

Otherwise you can do this
Code:
Dim strForm As String

If Me.YourOptionGroupNameHere = 1 Then
      strForm = "YourForm1NameHere"
Else
      strForm = "YourForm2NameHere"
End If
DoCmd.OpenForm strForm
 

iainmid

Registered User.
Local time
Today, 07:03
Joined
Mar 1, 2007
Messages
35
This is the code i have tried to use on click on the command button but a compile error keeps occuring

Private Sub Command9_Click()
Dim strForm As String
If Me.ReportOptions = 1 Then
strForm = "Contacts"
Else
strForm = "Main Menu"
End If
DoCmd.OpenForm strForm
End Sub
 

missinglinq

AWF VIP
Local time
Today, 02:03
Joined
Jun 20, 2003
Messages
6,423
Is your Option Group actually named ReportOptions? I ask this because the Option Group Wizard is kind of tricky in that it asks you to enter a Caption for it, but this is exactly what it is, a Caption for display! Unless you go into Properties and change it, the actual name of the control will remain whatever Access assigns to it, something like FrameN, where N is a number. If ReportOptions is actually only the Caption, Me.ReportOptions = 1 will bomb!
 

iainmid

Registered User.
Local time
Today, 07:03
Joined
Mar 1, 2007
Messages
35
I've checked the name and it seems to be correct. i've uploaded an example of what im doing if you can see where im going wrong
 

Attachments

  • Example.mdb
    188 KB · Views: 280

Kiwiman

Registered User
Local time
Today, 07:03
Joined
Apr 27, 2008
Messages
799
Howzit

The name of the frame control is called frame2, you have a label called ReportOptions

your code should be

Code:
Private Sub Command9_Click()
Dim strForm As String
If Me.frame2 = 1 Then
strForm = "Contacts"
Else
strForm = "Main Menu"
End If
DoCmd.OpenForm strForm
End Sub
 

iainmid

Registered User.
Local time
Today, 07:03
Joined
Mar 1, 2007
Messages
35
Cheers for that it seems to have worked. What would the code be if i was to add another value to the option box. i had hoped it might be something like this but this doesn't work

Private Sub Command9_Click()
Dim strForm As String
If Me.ReportOptions = 1 Then
strForm = "Contacts"
If Me.ReportOptions = 2 Then
strForm = "Main Menu"
Else
strForm = "Reports"
End If
DoCmd.OpenForm strForm
End Sub
 

Kiwiman

Registered User
Local time
Today, 07:03
Joined
Apr 27, 2008
Messages
799
Howzit

I would use a Select case statement

Code:
Private Sub Command9_Click()
Dim strForm As String

Select Case Me.Frame2
    Case Is = 1
        strForm = "Contacts"        ' Open contracts form
    Case Is = 2
        strForm = "Main Menu"       ' Open main form
    Case Is = 3
        etc                         ' open another form
    Case Else
        'something else             ' something else
End Select
        
DoCmd.OpenForm strForm              ' Open required form
    
    
End Sub
 

Kiwiman

Registered User
Local time
Today, 07:03
Joined
Apr 27, 2008
Messages
799
Howzit

Doing it the If else way would be something like

Code:
Private Sub Command9_Click()

Dim strForm As String

If Me.Frame2 = 1 Then
    strForm = "Contacts"
ElseIf Me.Frame2 = 2 Then
    strForm = "Main Menu"
Else
    strForm = "Reports"
End If

DoCmd.OpenForm strForm

End Sub
 

iainmid

Registered User.
Local time
Today, 07:03
Joined
Mar 1, 2007
Messages
35
Cheers
How do i code it so that the first two options open a form and the third option opens a report
 

Kiwiman

Registered User
Local time
Today, 07:03
Joined
Apr 27, 2008
Messages
799
Howzit

You can have

Code:
Private Sub Command9_Click()
Dim strForm As String

Select Case Me.Frame2
    Case Is = 1
        strForm = "Contacts"                            ' Open contracts form
    Case Is = 2
        strForm = "Main Menu"                           ' Open main form
    Case Is = 3
        strdocname = "rptReport"                        ' Open report
    Case Else
        'something else                                 ' something else
End Select
        
        
Select Case Me.Frame2
    Case Is = 1, 2
        DoCmd.OpenForm strForm                          ' Open required form
    Case Is = 3
        DoCmd.OpenReport strdocname, acViewPreview      ' Open Report in preview mode
        'DoCmd.OpenReport strdocname                     ' Print report
    Case Else
        ' something else
End Select
   
End Sub

or

Code:
Private Sub Command9_Click()
Dim strForm As String

Select Case Me.Frame2
    Case Is = 1
        strForm = "Contacts"                            ' Open contracts form
        DoCmd.OpenForm strForm                          ' Open required form
    Case Is = 2
        strForm = "Main Menu"                           ' Open main form
       DoCmd.OpenForm strForm                          ' Open required form
    Case Is = 3
        strdocname = "rptReport"                        ' Open report
        DoCmd.OpenReport strdocname, acViewPreview      ' Open Report in preview mode

    Case Else
        'something else                                 ' something else
End Select
        
End Sub
 

Users who are viewing this thread

Top Bottom