Oct 01 2024 01:19 PM
I have a Signin form (frmSignIn) with two Text Boxes asking for a UserName (txtName) and Password (txtPassword). The form also has two command buttons Enter (cmdEnter) and Add User (cmdAddUser).
I would like to validate that data has been entered into the text boxes before processing the form.
I have tried the following VBA Code in the BeforeUpdate event for the text Box.
Option Compare Database
Option Explicit
Private Sub txtName_BeforeUpdate(Cancel As Integer)
If txtName = "" Or IsNull(txtValue) Then
MsgBox "You are a Dummy"
Cancel = True
End If
End Sub
This only works if I enter a value in the txtName Box and then backspace to erase the value.
What am I doing wrong?
Oct 01 2024 02:12 PM
Make the operational code in the buttons' Click event procedures conditional on neither text box control being Null or containing a zero-length string, e.g.
Private Sub cmdEnter_Click()
Const MESSAGE_TEXT = "Both a valid user name and password must be entered."
If Nz(Me.txtName,"") = "" Or Nz(Me.txtPassword,"") = "" Then
MsgBox MESSAGE_TEXT, vbExclamation, "Invalid Operation"
Else
' operational code goes here
End If
End Sub
Private Sub cmdAddUser_Click()
Const MESSAGE_TEXT = "Both a valid user name and password must be entered."
If Nz(Me.txtName,"") = "" Or Nz(Me.txtPassword,"") = "" Then
MsgBox MESSAGE_TEXT, vbExclamation, "Invalid Operation"
Else
' operational code goes here
End If
End Sub
Oct 01 2024 07:54 PM
@MichaelB200, you need also to validate the other textbox. add code to the Click event of cmdEnter:
Private Sub cmdEnter_Click()
If IsNull(txtName) Then
MsgBox "Please provide your username."
Me.txtName.SetFocus
Exit Sub
End If
If IsNull(txtValue)
MsgBox "Please provide your password."
Me.txtValue.SetFocus
End If
End Sub