Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2007
    Posts
    127

    Unanswered: VB OptionButton linked with CommandButton

    I have a form that has two check boxes (checkbox - IP and checkbox OP),

    And I have a command button which runs a macro which then displays a report.

    How can I link the check boxes to the command button so that if 'checkbox-IP' is selected then it should run Macro 1 and if 'checkbox-OP' was selected then Macro 2 will run after the command buttons is pressed?

    This is my code for the command button


    Code:
    Private Sub Command7_Click()
    On Error GoTo Err_Command7_Click
    
    If Nz(Me.SearchBox.Value, "") = "" Then
        MsgBox "Please enter a unit numnber!"
        Exit Sub
    Else
    
    
        Dim stDocName As String
    
        stDocName = "Macro1"
        DoCmd.RunMacro stDocName
    
    Exit_Command7_Click:
        Exit Sub
    
    Err_Command7_Click:
        MsgBox Err.Description
        Resume Exit_Command7_Click
        End If
    End Sub

  2. #2
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1
    Quote Originally Posted by akhlaq768
    I have a form that has two check boxes (checkbox - IP and checkbox OP),

    And I have a command button which runs a macro which then displays a report.

    How can I link the check boxes to the command button so that if 'checkbox-IP' is selected then it should run Macro 1 and if 'checkbox-OP' was selected then Macro 2 will run after the command buttons is pressed?

    This is my code for the command button


    Code:
    Private Sub Command7_Click()
    On Error GoTo Err_Command7_Click
    
    If Nz(Me.SearchBox.Value, "") = "" Then
        MsgBox "Please enter a unit numnber!"
        Exit Sub
    Else
    
    
        Dim stDocName As String
    
        stDocName = "Macro1"
        DoCmd.RunMacro stDocName
    
    Exit_Command7_Click:
        Exit Sub
    
    Err_Command7_Click:
        MsgBox Err.Description
        Resume Exit_Command7_Click
        End If
    End Sub
    Hi

    Assuming the checkboxes are named IP and OP then something like this
    Code:
    Private Sub Command7_Click()
    On Error GoTo Err_Command7_Click
    
    If Nz(Me.SearchBox.Value, "") = "" Then
        MsgBox "Please enter a unit numnber!"
        Exit Sub
    Else
    
    
        Dim stDocName As String
    
        If IP Then
            stDocName = "Macro1"
        ElseIf OP Then
            stDocName = "Macro2"
        End If
    
        DoCmd.RunMacro stDocName
    
    Exit_Command7_Click:
        Exit Sub
    
    Err_Command7_Click:
        MsgBox Err.Description
        Resume Exit_Command7_Click
        End If
    End Sub
    ??

    I also assume that you cannot have IP and OP true at the same time !

    MTB

  3. #3
    Join Date
    Oct 2007
    Posts
    127
    I just used your code,

    i get the following error....

    'Object doesn't support this property or method'

  4. #4
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Are IP and OP separate, free-standing checkboxes, or are they checkboxes in an Option Group, as your title suggests? If they're part of an Option Group, Mike's code won't work.

    Code:
    If IP Then
       stDocName = "Macro1"
    ElseIf OP Then
       stDocName = "Macro2"
    End If
    would have to be something like

    Code:
    If OptionFrameName = 1 Then
      stDocName = "Macro1"
    ElseIf OptionFrameName = 2 Then
      stDocName = "Macro2"
    End If
    Assuming the first option was labeled IP and the second OP. You'd have to replace OptionFrameName with the actual name of yours.
    Last edited by Missinglinq; 02-20-09 at 10:27.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1
    Thanks Link, my mistake. I never use Checkboxes in option groups, only Radio Buttons.

    Cheers

    MTB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •