Show or hide a button in base of the value of auto calculated textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Asterisco
    New Member
    • Aug 2013
    • 3

    Show or hide a button in base of the value of auto calculated textbox

    Hi,
    I have a filtered form with a textbox and command button.
    The Control's Source of the textbox is set to:
    =Count([IDAutos])
    Which count the filtered records on the form.
    The button's property: Visible is set to "No"

    All I want is when the form open and filter the records, the button visibility change in based on the results of the value of the text box.
    Example:
    Code:
    If textbox value is > 1 Then
    button.visible = True
    
    If textbox value is = 1 Or <1 Or "Null" Then
    button.visible = False
    I tried in After Update Event of the textbox the normal code for this:

    Code:
    If Me.textbox > 1 Then
    Me.button.Visible = True
    Else
    Me.textbox =1 Or Me.textbox <1
    Me.button.Visible = False
    Tried this code in OnCurrent Event of the form and OnFilter and OnApplyFilter but nothing work.
    I suspect is related to the auto calculated value of the textbox, but really, I don't know. And if this is the case, how to proceed?
    Any help?

    Thank in advance and forgive me for my awful English (is from Google Translator)
    Last edited by Rabbit; Aug 22 '13, 03:41 PM. Reason: Fixed code tags
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    The problem seems to not be the event you are using because both OnCurrent and ApplyFilter events should have worked. The problem is with the code you are using to change the visibility. Have you tried making it visible by default so that you can see what value is in the textbox?

    Better code to set the value might be to use this in the ApplyFilter:
    Code:
    me.recordsetclone.movelast
    me.textbox.value = Me.RecordsetClone.RecordCount
    me.recordsetclone.movefirst
    Jim

    Comment

    • Asterisco
      New Member
      • Aug 2013
      • 3

      #3
      OK.
      The textbox is visible by default, is the command button which is set to not visible and I want to show or hide this button in base of the textbox value.
      With my original code, Count([IDAutos]), the textbox show 1 or 2 or 3, etc in dependence of the number of records filtered in the form.
      This form will show how many cars the customer have: 1, 2, 3, etc.
      Now, if the customer have, for example, 1 car o none, there is not need for the command button to be visible. For the contrary, is the customer have more than 1 car the button has to be visible for the customer to select which car will opérate.

      I tied your code in the ApplyFilter Event of the form, but then, the textbox is empty no manner how many records filtered by the Form.

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        Please do the trouble shooting and setup steps as given here:
        > Before Posting (VBA or SQL) Code

        Then right before this line: If Me.textbox > 1 Then insert debug.print "The Count = " & Me.textbox

        Run your code/open the form etc....
        Now press <ctrl><g>
        the immediates window will open and you should see somehting like:
        "The Count = 5" or whatever your textbox value has in it.

        Please post back with the result.

        The next step will be to "step" thru your code in debug mode to determine where the logic is failing.

        Comment

        • Asterisco
          New Member
          • Aug 2013
          • 3

          #5
          I did what you said and the immediates window show.... nothing, but the textbox (which is set to visible, like I said before) show: "3", which is the number of records filtered by the form.

          At first I spoke of a single button, but they are several buttons, lines and text boxes, like you can see in the code below:

          Code:
          Private Sub CantReg_AfterUpdate()
          
          Debug.Print "The Count = " & Me.CantReg
          If Me.CantReg > 1 Then
           Me.Marco23.Visible = True
           Me.Etiqueta31.Visible = True
           Me.PrimerReg.Visible = True
           Me.RegAnterior.Visible = True
           Me.IndReg.Visible = True
           Me.RegSiguiente.Visible = True
           Me.UltimoReg.Visible = True
           Me.NuevoReg.Visible = True
          Else
          Me.CantReg = 1 Or Me.CantReg < 1
           Me.Marco23.Visible = False
           Me.Etiqueta31.Visible = False
           Me.PrimerReg.Visible = False
           Me.RegAnterior.Visible = False
           Me.IndReg.Visible = False
           Me.RegSiguiente.Visible = False
           Me.UltimoReg.Visible = False
           Me.NuevoReg.Visible = False
          End If
          End Sub
          I repeat, if necessary, the control source of the textbox "CantReg" is set to: Count([IDAutos]).
          IDAutos is the autonumber, primary key, field of the form.

          Comment

          • zmbd
            Recognized Expert Moderator Expert
            • Mar 2012
            • 5501

            #6
            multiple buttons, multiple text etc...
            that's vital detail that without we are chasing the tail.
            Debug.Print "The Count = " & Me.CantReg
            MUST have printed at least "The Count = " in the window that opens when you press <ctrl><g>!
            Please try again.

            I am suspecting that you are either refering to the wrong control on your form or to something not in the recordset.

            Comment

            • jimatqsi
              Moderator Top Contributor
              • Oct 2006
              • 1293

              #7
              What is line 14? That doesn't look valid.

              Jim

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                jimatqsi
                Good Eyes.
                - i'd almost bet money that line 13 is supposed to be some sort of an "elseif" construct and not a valid one for VBA.

                Comment

                Working...