Auto-enable a form button

  • Thread starter Thread starter NecKroM
  • Start date Start date
N

NecKroM

Guest
Hi all!!
I'm kinda newbie on this, therefore I a have a question for u that has been eating my brains out:
I have a form inwitch certain fields need to be filled before proceding to the next form. The second form uses values filled in the first. I have a 'Next' button to proceed from the 1st form to the 2nd. The thing is, the user is allowed to proceed from a form to another, without entering any information (even with the fields setted as Necessary). What I would like to know is if there is any way do disable the 'Next' button until the user fills certain fields in the 1st form.. so that the second doesn't appear with wrong or no information at all.

Tankyou all by the help
 
I think the best way to do this would be to put data verification code at the command button, so it will give the user a message that they can't advance to the next form because of blank fields. Something like:

Code:
If Nz(MyField1) = "" or Nz(MyField2) = "" or Nz(MyField3) = "" Then
    Msgbox "Cannot proceed.  There are blank fields."
    Exit Sub
End If

If it were only one field that required data you could enable/disable the button based on that field but it sounds like it's more than one.
 
You could do something like this:
Code:
Private Sub Text0_Change()
       
    If Not (IsNull(Text0) Or Text0 = "") And Not (IsNull(Text0) Or Text0 = "") Then
        Me!Next.Enabled = True
    Else
        Me!Next.Enabled = False
    End If
    
        
End Sub


Private Sub Text1_Change()

    If Not (IsNull(Text0) Or Text0 = "") And Not (IsNull(Text0) Or Text0 = "") Then
               Me!Next.Enabled = True
    Else
        Me!Next.Enabled = False
    End If

End Sub

However the Change event doesn't seem to recognize the backspace key...can anyone chime in with some way around this?

Sam.
 
You need to put your validation code in the forms BeforeUpdate event so that you can cancel the event if the validation is not met. This will prevent the record from being saved and also prevent the form from being closed.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Form_BeforeUpdate_Err
        
    If Nz(MyField1) = "" Or Nz(MyField2) = "" Or Nz(MyField3) = "" Then
        MsgBox "Cannot proceed.  There are blank fields."
        DoCmd.CancelEvent
        Exit Sub
    End If
        
Form_BeforeUpdate_Exit:
    Exit Sub
    
Form_BeforeUpdate_Err:
    MsgBox Err.Number & " - " & Err.Description
    Resume Form_BeforeUpdate_Exit
        
End Sub
 
I had thought about suggesting the Before Update event but that event doesn't seem to fire until you change records within the form. I just tried a sample of that and I could go right to the command button without the Before Update event ever firing.
 
RichO said:
I had thought about suggesting the Before Update event but that event doesn't seem to fire until you change records within the form. I just tried a sample of that and I could go right to the command button without the Before Update event ever firing.
Then the form must not be closing when the button is clicked. Your way should cover the problem if the tested fields are null when the button is clicked.

Code:
If Nz(MyField1) = "" or Nz(MyField2) = "" or Nz(MyField3) = "" Then
    Msgbox "Cannot proceed.  There are blank fields."
    Exit Sub
Else
    DoCmd.OpenForm "Your2ndFormNameHere"
End If
My BeforeUpdate suggestion will prevent the 1st form from closing if either of those fields are null.
 
OK. Actually on my first test form I was trying to use the Before Update event to change the command button's enable property based on field content, which in this case the event was not firing.
 

Users who are viewing this thread

Back
Top Bottom