How To: Disable button in subform until new record is created?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SclWrkr
    New Member
    • Mar 2012
    • 2

    How To: Disable button in subform until new record is created?

    Regarding command buttons in Access 2007 . . .

    Related to a similar thread, http://bytes.com/topic/access/answer...-if-field-null, I have a more basic question, but minimal VBA experience.

    I want to keep a command button on a subform disabled until the user enters data into any field-- in other words, enable the button as soon as a record is created and the EncounterID autonumber is generated (which occurs and displays in a hidden field on the form as soon as the user enters data into any other form field).

    I've tried dropping this code in various events, including
    Code:
    Private Sub Form_Current():
    
    If Nz(Me!EncounterID,"") = "" Then
         Me!EnrollButton.Visible = False
    Else
         Me!EnrollButton.Visible = True
    End If
    It almost worked-- I have a similar problem as the user in the old thread-- The button is disabled when EncounterID is null, and stays disabled when EncounterID is NOT null. When I toggle between records, the button is finally enabled.

    I want the button to become enabled as soon as the EncounterID is written, without having to toggle between records.

    Many thanks in advance to anyone who can help!
    Last edited by NeoPa; Mar 15 '12, 01:18 AM. Reason: Added mandatory [CODE] tags for you.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32662

    #2
    Your code is triggered by the form's Current event, but logically it should be triggered by the form's Dirty event.

    Try this :
    Code:
    Private Sub Form_Dirty():
        Me.EnrollButton.Visible = (Me.EncounterID > "")
    End Sub

    Comment

    • SclWrkr
      New Member
      • Mar 2012
      • 2

      #3
      Hi NeoPa,

      Thanks so much for your prompt reply! I tried implementing the code as an Event in the subform property. When I compiled the code, the system threw an error: Invalid use of Null.

      Access would not let me use the procedure Form_Dirty(), so this is the code I ended up using. The procedure is the default if I go to Form->Properties->Event->On Dirty.
      Code:
      Private Sub Form_Dirty(Cancel As Integer)
           Me.cmdEnroll.Visible = (Me.EncounterID > "")
      End Sub
      Other suggestions?
      /SclWrkr
      Last edited by NeoPa; Mar 19 '12, 08:53 PM. Reason: Note: The button name has been changed to cmdEnroll, so this is correct in the code snippet - Mod added mandatory [CODE] tags.

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32662

        #4
        You question refers to a sub-form, but doesn't adequately explain where that fits into the situation. I've assumed it was irrelevant, but if it's not (and the failure of your code indicates that might be the case) then you need to explain what, if anything, is on the main form and what, apart from the CommandButton [cmdEnroll], is on the form in the sub-form.

        If everything is on the sub-form, as I'd assumed, then your code should have worked. Clearly the situation is more complicated than you've explained.

        Comment

        Working...