Ok. I have form, F_Project, that when opened I want to make sure certain fields are populated. The following code is in the Before Update Event and seems to work well when I tab through and it tries to update but the event prevents it.
The problem is, I also want to have command button on the this form to close the form. The only code there is, On Click, DoCmd.Close.
What happens is if I click the close button I created, it will find the first text or combo box that is required to be populated, give me the message, I click OK, then is closes the form instead of setting the focus on the empty control.
How to do correct this?
Thanks for any help!
code#
Private Sub Form_BeforeUpdate(Cancel As Integer)
'An asterisk (*) in the Tag Property of the text for combo boxes to be validated.
Dim msg As String, Style As Integer, Title As String
Dim nl As String, ctl As Control
nl = vbNewLine & vbNewLine
For Each ctl In Me.Controls
If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Then
If ctl.Tag = "*" And Len(ctl.Value & "") = 0 Then ' "" indicates a null string
msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _
"Enter the data and try again . . . "
Style = vbQuestion + vbOKOnly
Title = "Required Data..."
MsgBox msg, Style, Title
ctl.SetFocus
Cancel = True
Exit For
End If
End If
Next
End Sub
code#
The problem is, I also want to have command button on the this form to close the form. The only code there is, On Click, DoCmd.Close.
What happens is if I click the close button I created, it will find the first text or combo box that is required to be populated, give me the message, I click OK, then is closes the form instead of setting the focus on the empty control.
How to do correct this?
Thanks for any help!
code#
Private Sub Form_BeforeUpdate(Cancel As Integer)
'An asterisk (*) in the Tag Property of the text for combo boxes to be validated.
Dim msg As String, Style As Integer, Title As String
Dim nl As String, ctl As Control
nl = vbNewLine & vbNewLine
For Each ctl In Me.Controls
If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Then
If ctl.Tag = "*" And Len(ctl.Value & "") = 0 Then ' "" indicates a null string
msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _
"Enter the data and try again . . . "
Style = vbQuestion + vbOKOnly
Title = "Required Data..."
MsgBox msg, Style, Title
ctl.SetFocus
Cancel = True
Exit For
End If
End If
Next
End Sub
code#