Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Oct 2003
    Location
    Ogunquit, Maine, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hide a control (Access 2K)

    I would like to have a control on a form (command button) change its visibility depending on a value in a text field in that record. Specifically, if a RegNumb has not been assigned to a record, then I want a command button to appear that will increment the maximum value of RegNumb in the table by 1 and assign it to that record. I have the button that does the incrementing working with this code

    Private Sub cmdRegNumb_Click()
    [RegNumb] = (txtMaxRegNumb) + 1
    End Sub

    but I want to change its visibility with something like this:

    If ([RegNumb] = Null) Then
    cmdRegNumb.Visible = True
    Else
    cmdRegNumb.Visible = False
    End If


    Is this a possible thing to do?

  2. #2
    Star Lounger
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hide a control (Access 2K)

    Yes it is possible, and the approach you suggest should work fine. Have you tried it?

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Hide a control (Access 2K)

    You cannot use = Null in a comparison, for Null is not really a value, it indicates the lack of a value. Instead, use the IsNull function:

    If IsNull(Me.RegNumb) Then
    Me.cmdRegNumb).Visible = True
    Else
    Me.cmdRegNumb.Visible = False
    End If

    This can be simplified to

    Me.cmdRegNumb.Visible = IsNull(Me.RegNumb)

    You can use this code in the On Current event of the form, so that the visibility of the button is kept correct as the user moves from record to record. It is also a good idea to check whether the command button has the focus, and change the focus if so, for you cannot hide a control that has the focus:

    Private Sub Form_Current()
    If Screen.ActiveControl.Name = "cmdRegNumb" Then
    Me.SomeOtherControl.SetFocus
    End If
    Me.cmdRegNumb.Visible = IsNull(Me.RegNumb)
    End Sub

    Note: personally, I would use the Enabled property of the command button instead of the Visible property, but that is a matter of taste.

  4. #4
    New Lounger
    Join Date
    Oct 2003
    Location
    Ogunquit, Maine, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hide a control (Access 2K)

    Hans,

    Thanks, that took care of my problem. Appreciate the rapid response.

Posting Permissions

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