Dual Action Command Button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scottbouley
    New Member
    • Sep 2008
    • 30

    #1

    Dual Action Command Button

    I'm trying to create a command button that will perform one action on the first click and another action on the second click. I tried to accomplish it by using a variable but I think the variable is resetting each time causing a loop. I'm thinking I should move the variable declaration and set value but don't know where to put it or how to declare a Public variable. My code is below:

    Code:
    Private Sub ToggleReturnMode_Click()
        Dim tgl As String
        tgl = "First"
        
        If tgl = "First" Then
            [sfrm_Edit_Sales_Details].Controls("Returned").ColumnHidden = False
            MsgBox "Check Returned Checkbox for each item to return and click this button again.", vbOKOnly
            tgl = "Second"
        Else
        MsgBox "Code for second click runs", vbOKOnly
        End If
    End Sub
    Thanks in advance
  • puppydogbuddy
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    Hi ,
    I use a different method entirely, which I discuss later on in this reply. First try your method with a global variable and see if it works. You would use a standard module named for example (modGlobals) in which you define your variable as follows:

    Public yourGlobalVaria bleName As Datatype

    where yourVariableNam e is often prefaced with a "g" to make it easy to identify in code.
    _______________ _______________ _______________ ___

    The way I code my toggle buttons is to switch the button's caption and execute the applicable button code depending on the caption being displayed, thus creating a toggle effect. If the button is "off" at the time it is clicked, the code will change the caption to "On" and execute code applicable to the "On" button. The toggle button's caption property was initially set at "Off" before executing the following code.

    The code example below toggles a search button in a social security # database between "on" and "off"
    Code:
    Option Compare Database
    Option Explicit
    Dim blnOnButtonClicked As Boolean
    
    Private Sub btnToggle_Click()
    On Error GoTo Error_Routine
    
    If Me.btnToggle.Caption = "Off" Then
       Me.btnToggle.Caption = "On"
       Me![txtSearch].Visible = True
       Me!FindSSN.Visible = True
       blnOnButtonClicked = True
       Me!txtSearch.SetFocus
    ElseIf Me.btnToggle.Caption = "On" Then
       Me.btnToggle.Caption = "Off"
       Me!FindSSN.Visible = False
       blnOnButtonClicked = False
       Me!SSN.SetFocus
       Me![txtSearch].Visible = False
    End If
    
    Exit_Continue:
        Exit Sub
    Error_Routine:
            MsgBox "Error# " & Err.Number & " " & Err.Description
            Resume Exit_Continue
    End Sub

    Comment

    • scottbouley
      New Member
      • Sep 2008
      • 30

      #3
      I like your method but have two questions:

      First, is there any reason I can't declare the variable inside the sub-routine since it's only used there?

      Second: I don't really want the caption to show On or Off. Will setting the caption still work if I assign a picture to the button? Maybe I should change the data type to string and use descriptive captions instead?

      Comment

      • puppydogbuddy
        Recognized Expert Top Contributor
        • May 2007
        • 1923

        #4
        Originally posted by scottbouley
        I like your method but have two questions:

        First, is there any reason I can't declare the variable inside the sub-routine since it's only used there?

        Second: I don't really want the caption to show On or Off. Will setting the caption still work if I assign a picture to the button? Maybe I should change the data type to string and use descriptive captions instead?
        No, there isn't any reason why you can't declare a variable inside the procedure if that is the only place you use it. Further, even if you use it in more than one procedure on the same form, you can put it in the general declarations section of the form like I did in my sample code. The only reason I mentioned the global variable is because you asked about it.

        As to your second question, you can choose any captions you want for your button. On and Off were captions that were used in the example I gave you because the toggle was used to enable or disable certain controls related to a search field. I've never tried to toggle a picture caption, so I don't know if it would work....It won't hurt to try it and refer to the caption picture object by its name.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Your situation is a perfect candidate for a Static, Procedural Level, Variable. The code below should do the trick:
          Code:
          Private Sub ToggleReturnMode_Click()
          Static intClickNumber As Integer
          
          intClickNumber = intClickNumber + 1
          
          If intClickNumber = 1 Then
            [sfrm_Edit_Sales_Details].Controls("Returned").ColumnHidden = False
            MsgBox "Check Returned Checkbox for each item to return and click " & _
                   "this button again.", vbOKOnly
          Else
            MsgBox "Code for second click runs", vbOKOnly
            intClickNumber = 0        'RESET Static variable?
          End If
          End Sub

          Comment

          • scottbouley
            New Member
            • Sep 2008
            • 30

            #6
            Thanks to both of you for your responses.

            ADezii's idea was what I was trying to do but I had already successfully implimented PDB's by the time I read it. I like the way it worked out. I substituted Returns for Off and Continue for On and it appears to be more intuitive.

            Thanks again both of you, I greatly appreciate your assistance.

            Comment

            Working...