none
Disable Add New or Save Button If a Textbox is empty RRS feed

  • Question

  • Hello,

    I am trying to disable the save or add new button if a textbox is empty on an access form but it is not working, the button is still enabled. So I disable in the button property's panel and flip the code to enable it but that does not work also.

    This is the code:

    Private Sub form_load()
    If Len(Me.FIRST_NAME) = 0 Then
    Me.CommandAddNew.Enabled = False
    End If
    End Sub

    Thanks for the input everyone.


    Syn

    Monday, October 3, 2011 6:27 AM

Answers

  • Hi Syn,

    I usely do this with a Form with Unbound Text Fields, and before saving the values to the Table, I do a test and validate the fields.

    However you might achieve this by setting the Tag option for the Text fields you want to check to "Req".

    Then change the OnCurrent event into this:

    Private Sub Form_Current()
    
    Dim ctl As Control
    
    For Each ctl In Me.Controls
    
    If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
    
      If ctl.Tag <> "Req" Then
      Exit Sub
      End If
       
      If ctl.Tag = "Req" And IsNull(ctl.Value) Then
      Me.cmdNewRecord.Enabled = False
      Else
      Me.cmdNewRecord.Enabled = True
      End If
      
    End If
    
    Next ctl
      
    End Sub
    


    Then in the AfterUpdate event of all your Text Fields, you place:

    Private Sub YourTextFieldNameHere_AfterUpdate()
    
    Call Form_Current
    
    End Sub
    

    Hope this helps,

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    • Marked as answer by Synthologic Monday, October 3, 2011 8:22 PM
    Monday, October 3, 2011 7:03 PM
    Moderator

All replies

  • you can do it like this

    Private Sub TextStatus()
      If Vba.Len(Me.MyText)=0 Then
         Me.MyButton.Enable=False
      Else
         Me.MyButton.Enable=True
      End If
    End Sub
    
    Public Sub MyForm_Load
       Call TextStatus()
    End Sub
    
    Private Sub MyText_Change()
       Call TextStatus()
    End Sub
    
     
    

     

    Monday, October 3, 2011 6:45 AM
  • you can do it like this

    Private Sub TextStatus()
      If Vba.Len(Me.MyText)=0 Then
         Me.MyButton.Enable=False
      Else
         Me.MyButton.Enable=True
      End If
    End Sub
    
    Public Sub MyForm_Load
       Call TextStatus()
    End Sub
    
    Private Sub MyText_Change()
       Call TextStatus()
    End Sub
    
     
    

     

    Can you provide me a little more explanation on this.
    I already have this on the form load and I think there is a conflict.

    I have his on the form load:

    Private Sub form_Load()
    If Not Me.NewRecord Then
    DoCmd.RunCommand acCmdRecordsGoToNew
    End If
    

    I am trying to understand how to make this work.

    Thanks again.

    SYN
    Monday, October 3, 2011 11:34 AM
  • Try this:

    Private Sub TextStatus()
      if (IsNull(Me.MyText)) then
         Me.Mybutton.Enable=False
      ElseIf (Vba.Len(Me.MyText)=0)Then
         Me.MyButton.Enable=False
      Else
         Me.MyButton.Enable=True
      End If
    End Sub
    
    
    

     

    and checkstatus call after insert new record
    Monday, October 3, 2011 1:10 PM
  • Try this:

    Private Sub TextStatus()
    if (IsNull(Me.MyText)) then
    Me.Mybutton.Enable=False
    ElseIf (Vba.Len(Me.MyText)=0)Then
    Me.MyButton.Enable=False
    Else
    Me.MyButton.Enable=True
    End If
    End Sub


     

    and checkstatus call after insert new record
    Where should I place this code? I need the button to remain disable when the form loads and it should enable when the textbox contain something or length is not zero.

    SYN
    Monday, October 3, 2011 1:41 PM
  • Hello,

    I am trying to disable the save or add new button if a textbox is empty on an access form but it is not working, the button is still enabled. So I disable in the button property's panel and flip the code to enable it but that does not work also.

    This is the code:

     

    Private Sub form_load()
    If Len(Me.FIRST_NAME) = 0 Then
    Me.CommandAddNew.Enabled = False
    End If
    End Sub

    Thanks for the input everyone.


    Syn

     

    Hi Syn,

    I think what you are trying to accomplish is, that the user needs to complete the First Name before creating a New Record. Is that correct?

     

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Monday, October 3, 2011 4:40 PM
    Moderator
  • Hello,

    I am trying to disable the save or add new button if a textbox is empty on an access form but it is not working, the button is still enabled. So I disable in the button property's panel and flip the code to enable it but that does not work also.

    This is the code:

     

    Private Sub form_load()
    If Len(Me.FIRST_NAME) = 0 Then
    Me.CommandAddNew.Enabled = False
    End If
    End Sub

    Thanks for the input everyone.


    Syn

     

    Hi Syn,

    I think what you are trying to accomplish is, that the user needs to complete the First Name before creating a New Record. Is that correct?

     

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Here is my story.

    I have a button on my form that saves and create a new record. I want this button to be disabled when the form first open.
    So if you open the form, the button is disable, when you enter information on the form, the button becomes enable which allows you to save. But when the record is saved, the button goes back to disabled.

    This way the user cannot save an incomplete record. I am only using first name as an example.

    Thanks for your help!

     

    SYN

    Monday, October 3, 2011 4:57 PM
  • Hi Syn,

    You can accomplish this using the OnCurrent Event of your Form, see below example:

    Private Sub Form_Current()
    If IsNull(Me.FIRST_NAME) Then
    
      Me.CommandAddNew.Enabled = False
        Else
     Me.CommandAddNew.Enabled = True
     
    End If
    
    End Sub
    

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Monday, October 3, 2011 5:15 PM
    Moderator
  • Hi Syn,

    You can accomplish this using the OnCurrent Event of your Form, see below example:

     

    Private Sub Form_Current()
    If IsNull(Me.FIRST_NAME) Then
    
      Me.CommandAddNew.Enabled = False
        Else
     Me.CommandAddNew.Enabled = True
     
    End If
    
    End Sub
    

    Hope this helps,

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Okay it disable the button but when I type into the box the button does not become enable. The button is suppose to become active when text is in it and it is not empty.


    Thanks for your time and patience.

    SYN
    Monday, October 3, 2011 5:29 PM
  • Hi Syn,

    When you want to trigger the OnCurrent event after making an Entry in Text Field FIRST_NAME, you can then call the OnCurrent event in the After Update event of your Text Field, like:

    Private Sub FIRST_NAME_AfterUpdate()
    
    Call Form_Current
    
    End Sub
    


    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Monday, October 3, 2011 5:45 PM
    Moderator
  • Hi Syn,

    When you want to trigger the OnCurrent event after making an Entry in Text Field FIRST_NAME, you can then call the OnCurrent event in the After Update event of your Text Field, like:

     

    Private Sub FIRST_NAME_AfterUpdate()
    
    Call Form_Current
    
    End Sub
    


     

    Hope this helps,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"


    So finally, it work on one field but not on other fields, how can I apply this code to work on all required fields?

     

    Thanks a million!

     

    SYN

    Monday, October 3, 2011 6:00 PM
  • Hi Syn,

    I usely do this with a Form with Unbound Text Fields, and before saving the values to the Table, I do a test and validate the fields.

    However you might achieve this by setting the Tag option for the Text fields you want to check to "Req".

    Then change the OnCurrent event into this:

    Private Sub Form_Current()
    
    Dim ctl As Control
    
    For Each ctl In Me.Controls
    
    If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
    
      If ctl.Tag <> "Req" Then
      Exit Sub
      End If
       
      If ctl.Tag = "Req" And IsNull(ctl.Value) Then
      Me.cmdNewRecord.Enabled = False
      Else
      Me.cmdNewRecord.Enabled = True
      End If
      
    End If
    
    Next ctl
      
    End Sub
    


    Then in the AfterUpdate event of all your Text Fields, you place:

    Private Sub YourTextFieldNameHere_AfterUpdate()
    
    Call Form_Current
    
    End Sub
    

    Hope this helps,

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    • Marked as answer by Synthologic Monday, October 3, 2011 8:22 PM
    Monday, October 3, 2011 7:03 PM
    Moderator
  • Hi Syn,

    I usely do this with a Form with Unbound Text Fields, and before saving the values to the Table, I do a test and validate the fields.

    However you might achieve this by setting the Tag option for the Text fields you want to check to "Req".

    Then change the OnCurrent event into this:

     

    Private Sub Form_Current()

    Dim ctl As Control

    For Each ctl In Me.Controls

    If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then

    If ctl.Tag <> "Req" Then
    Exit Sub
    End If

    If ctl.Tag = "Req" And IsNull(ctl.Value) Then
    Me.cmdNewRecord.Enabled = False
    Else
    Me.cmdNewRecord.Enabled = True
    End If

    End If

    Next ctl

    End Sub

     


    Then in the AfterUpdate event of all your Text Fields, you place:

     

    Private Sub YourTextFieldNameHere_AfterUpdate()

    Call Form_Current

    End Sub

     

    Hope this helps,

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    I assumed you are meaning I should put the word Req into the Tag field which is in the Other section of the properties sheet, right?
    I did that and the button is disable but is not enabled when I enter a value.

    Any ideas why?

     

    SYN

    Monday, October 3, 2011 7:37 PM
  • Hi Syn,

    I usely do this with a Form with Unbound Text Fields, and before saving the values to the Table, I do a test and validate the fields.

    However you might achieve this by setting the Tag option for the Text fields you want to check to "Req".

    Then change the OnCurrent event into this:

     

    Private Sub Form_Current()
    
    Dim ctl As Control
    
    For Each ctl In Me.Controls
    
    If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
    
      If ctl.Tag <> "Req" Then
      Exit Sub
      End If
       
      If ctl.Tag = "Req" And IsNull(ctl.Value) Then
      Me.cmdNewRecord.Enabled = False
      Else
      Me.cmdNewRecord.Enabled = True
      End If
      
    End If
    
    Next ctl
      
    End Sub
    

     


    Then in the AfterUpdate event of all your Text Fields, you place:

     

    Private Sub YourTextFieldNameHere_AfterUpdate()
    
    Call Form_Current
    
    End Sub
    

     

    Hope this helps,

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"


    Good afternoon,

    I finally got this working, thanks for all your help, you have been incredibly helpful! I have learn many new things.


    This is what I did.

    Private Sub ToggleCommandButton()
        Me!CommandAddNew.Enabled = _
            (Len(Me.FIRST_NAME.Value & vbNullString) And _
            Len(Me.LAST_NAME.Value & vbNullString) And _
            Len(Me.ANT_ENTRY_TERM.Value & vbNullString))
    End Sub
    
    
    Private Sub Form_Current()
    Call ToggleCommandButton
    End Sub
    
    
    Private Sub FIRST_NAME_AfterUpdate()
    Call ToggleCommandButton
    End Sub
    
    
    Private Sub LAST_NAME_AfterUpdate()
    Call ToggleCommandButton
    End Sub
    
    
    Private Sub ANT_ENTRY_TERM_AfterUpdate()
    Call ToggleCommandButton
    End Sub

    You previously suggestion had work but I was using an or rather then a AND.


    Thanks again!


    SYN

    Monday, October 3, 2011 8:22 PM