Results 1 to 6 of 6
  1. #1
    GAccess is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    38

    Question Enable / Disable Command Button after conditions have been met

    Hi all,

    Beginner at VBA here. Been working on this for two days.
    Objective: After each field in my form is completed, I want my NewSave button to be enabled.

    Problem: Created an if/then code that disables the button but never re enables it after conditions are met
    Here is the code, Please Help!

    Code:

    Private Sub Form_Current()
                
        
    Dim promptmsg As Long
       
        
              
    If IsNull(Me.ContractValue) = False And IsNull(Me.ContractNumber) = False And IsNull(Me.InvoiceDate) = False  Then
            
                       Me
    !NewSave_Button.Enable True
         
                 End 
    If

             If 
    IsNull(Me.ContractValue) Or IsNull(Me.ContractNumber) Or IsNull(Me.InvoiceDate) = True Then
         


                       Me
    !NewSave_Button.Enable False

                      promptmsg 
    MsgBox("Please fill blank entries."vbOKOnly vbCritical"Blank Entries")

              
    End If

    End Sub


    this below is 
    for the command button

    Private Sub NewSave_Button_Click()

                
    Me.AmountPaid.Visible False

                Me
    .Notes.Visible False

                DoCmd
    .GoToRecord , , acNewRec

                DoCmd
    .RunCommand acCmdSave

               
    End Sub 

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,816
    The form Current event only executes when record gets focus, not after editing a field. If you want this code to execute after each entry, need to run code from AfterUpdate event of each control. You could put the conditional code in a separate Sub that can be called by the event procedures. I think you could simplify with:
    Code:
    If Not IsNull(Me.ContractValue) And Not IsNull(Me.ContractNumber) And Not IsNull(Me.InvoiceDate) Then
            
         Me!NewSave_Button.Enable = True
    
    Else     
         Me!NewSave_Button.Enable = False
    
         MsgBox "Please fill blank entries.", vbOKOnly + vbCritical, "Blank Entries"
    
    End If
    Message box has two forms - a function that must be used in an expression and a simple popup. Use of parens signifies the function.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    GAccess is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    38
    Hi June7,

    Unfortunately your suggestion did not work for me. Instead I used a "with" statement under Public Function that I found online. I made sure I noted all components within my form as required that I wanted. Then I used with and if statements: if .Tag = "Required" Then if .Value & "" = "" Then

    The only "issue" I am running into now, is that my msg box isn't populating what I asked for. But the statements are doing the trick.

    Thanks,

    Gaccess

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,816
    Show the complete code procedure.


    The With is just a shorthand so don't have to keep repeating reference qualifier like Me or Forms!formname.controlname.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    GAccess is offline Advanced Beginner
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    38
    Code:
    Public Function validateform(myform As Form) As Boolean
    'returns true if all required fields have data, or false if not.
    '
    It will also create a popup message explaining which fields need data

    Dim boolresponse 
    As Boolean
    Dim strError 
    As Variant
    Dim ctl 
    As Control
    boolresponse 
    True
    'starting off the boolean response as true
    strError = Null
    '
    the error is defined as null
     
    With myform
        
    For Each ctl In .Controls
        
          With ctl
          
            
    If .Tag "required" Then
            
               
    If .Value "" "" Then
               
                    boolresponse 
    False
                                    
                    strError 
    = (strError ", ") & .Name
                            
                End 
    If
                
            
    End If
            
          
    End With
          
       Next ctl
       
    End With
    If strError "" <> "" Then strError MsgBox("The following information must be entered first: " strErrorvbOKOnly vbInformation)
    validateform boolresponse
     

    End 
    Function
     

    Private 
    Sub NewSave_Button_Click()

    Dim enterfieldsmsg As Variant
      On Error 
    GoTo Err_NewSave_Button_Click

            
    If Me.ContractNumber "" <> "" Or Me.ContractValue "" <> "" Then
                
    If validateform(MeThen
                    DoCmd
    .GoToRecord , , acNewRec
                    DoCmd
    .RunCommand acCmdSave
                    Me
    .AmountPaid.Visible False
                    Me
    .Notes.Visible False
               
    Else
                    
    enterfieldsmsg MsgBox("Enter all fields"vbOKOnly vbCritical"Missing Information")
                    
        
                
    End If
            
    End If
            
    Exit_NewSave_Button_Click:
        Exit 
    Sub
    Err_NewSave_Button_Click
    :
        
    MsgBox Err.Description
        Resume Exit_NewSave_Button_Click 
    The code is above, do you have any suggestions to make the msg work? cause right now this code is blocking my ability to send error messages for duplicate entries etc.
    *sigh*

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,816
    Try this change:

    If validateform(Me.Name) Then

    Step debug.

    Also, the message boxes are fine but be aware the MsgBox has two forms, one as a function and the other as simple popup. Use of parens designates function and the MsgBox must be part of an expression. Without parens no variable needed and could simply be:

    If strError & "" <> "" Then MsgBox "The following information must be entered first: " & strError, vbOKOnly + vbInformation

    MsgBox "Enter all fields", vbOKOnly + vbCritical, "Missing Information"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Disable command button
    By rajulasb in forum Access
    Replies: 2
    Last Post: 08-04-2011, 05:32 AM
  2. Continuous Form Disable Command Button
    By mbake085 in forum Programming
    Replies: 4
    Last Post: 05-27-2011, 08:55 AM
  3. Enable or Disable Field in Forum
    By lolos66666 in forum Forms
    Replies: 5
    Last Post: 03-13-2011, 05:30 PM
  4. Function to Enable/Disable Field
    By swalsh84 in forum Programming
    Replies: 5
    Last Post: 11-04-2010, 02:48 PM
  5. how to disable command button
    By archie in forum Access
    Replies: 1
    Last Post: 08-27-2009, 11:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums