dkmoreland
Registered User.
- Local time
- Today, 05:00
- Joined
- Dec 6, 2017
- Messages
- 129
I have a form on which certain fields are only visible when the correct value is chosen in the combo box.
Here is the code I have in the After Update property for the combo box - it is working fine.
Private Sub Issue_Type_AfterUpdate()
If Me.[Issue Type] = "Bindery" Then
Me.[Roll #].Visible = False
Me.Lot.Visible = False
Me.cboPressInitials.Visible = False
Me.[Box Range].Visible = True
Me.[Last Box Number].Visible = True
Else
Me.[Roll #].Visible = True
Me.Lot.Visible = True
Me.[Box Range].Visible = False
Me.[Last Box Number].Visible = False
Me.cboPressInitials.Visible = False
End If
End Sub
However, I need these fields to be required if they are visible. I am using this code on the Before Update property of the form to check to make sure the required fields are populated. I didn't write this - some kind soul posted it somewhere else and I borrowed it.
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Msg As String, Style As Integer, Title As String
Dim ctl As Control, DL As String
DL = vbNewLine & vbNewLine
For Each ctl In Me.Controls
If ctl.Tag = "?" Then
If Trim(ctl & "") = "" Then
Msg = "Required field '" & ctl.Name & "' cannot be blank"
Style = vbCritical + vbOKOnly
Title = "Required Data Error! . . ."
MsgBox Msg, Style, Title
Me(ctl.Name).SetFocus 'set focus to the field in error.
Cancel = True 'cancel saving the record!
Exit Sub 'return to the form for correction.
End If
End If
Next
End Sub
If I set the tags to ? on those fields that have conditional visibility, I get a Set Focus error, since focus can't be set on an invisible field.
How can i set these fields to be required when visible? is there a smoother way to do this?
Thanks in advance!
Here is the code I have in the After Update property for the combo box - it is working fine.
Private Sub Issue_Type_AfterUpdate()
If Me.[Issue Type] = "Bindery" Then
Me.[Roll #].Visible = False
Me.Lot.Visible = False
Me.cboPressInitials.Visible = False
Me.[Box Range].Visible = True
Me.[Last Box Number].Visible = True
Else
Me.[Roll #].Visible = True
Me.Lot.Visible = True
Me.[Box Range].Visible = False
Me.[Last Box Number].Visible = False
Me.cboPressInitials.Visible = False
End If
End Sub
However, I need these fields to be required if they are visible. I am using this code on the Before Update property of the form to check to make sure the required fields are populated. I didn't write this - some kind soul posted it somewhere else and I borrowed it.
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Msg As String, Style As Integer, Title As String
Dim ctl As Control, DL As String
DL = vbNewLine & vbNewLine
For Each ctl In Me.Controls
If ctl.Tag = "?" Then
If Trim(ctl & "") = "" Then
Msg = "Required field '" & ctl.Name & "' cannot be blank"
Style = vbCritical + vbOKOnly
Title = "Required Data Error! . . ."
MsgBox Msg, Style, Title
Me(ctl.Name).SetFocus 'set focus to the field in error.
Cancel = True 'cancel saving the record!
Exit Sub 'return to the form for correction.
End If
End If
Next
End Sub
If I set the tags to ? on those fields that have conditional visibility, I get a Set Focus error, since focus can't be set on an invisible field.
How can i set these fields to be required when visible? is there a smoother way to do this?
Thanks in advance!