Hi everyone
Just need some help quickly with the following code:
The specified controls have "Required" tag but when I leave it empty and click submit, it follows through to the last message box saying all required fields are filled.
Just need some help quickly with the following code:
Code:
Private Sub btnSubmit_Click()
Dim ctrl As Control
Dim missingFields As String
Dim isValid As Boolean
' Initialize variables
missingFields = ""
isValid = True
' Loop through all the controls on the form
For Each ctrl In Me.Controls
' Check if the control is a TextBox or ComboBox
If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
' Check if the control is tagged as required and is empty
If ctrl.Tag = "Required" And Trim(ctrl.Value) = "" Then
missingFields = missingFields & ctrl.Name & vbNewLine
isValid = False
End If
End If
Next ctrl
' If there are missing fields, alert the user
If Not isValid Then
MsgBox "The following fields are required and must be filled out:" & vbNewLine & missingFields, vbExclamation, "Missing Fields"
Else
' Proceed with submission logic
MsgBox "All required fields are filled!", vbInformation, "Success"
End If
End Sub
The specified controls have "Required" tag but when I leave it empty and click submit, it follows through to the last message box saying all required fields are filled.