G
george 16-17
Greetings all,
I am in need of some assistance with coding a function that checks bound
required fields on a form before saving a new record. It works fine, but the
problem that I am having is that when all the fields are complete, I would
like the form to be refreshed. As the code is written now, it refreshes the
data even if the required fields are incomplete. So essentially: Incomplete
data = no refresh, Complete data = refresh.
Here is my code:
Function SaveNewRecord(frm As Form)
' save function for new record - prompts save msgbox
Dim strMsg As String
strMsg = "New record has not been saved."
strMsg = strMsg & " Do you wish to save?"
Dim lngAnswer As Long
lngAnswer = MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save New Record?")
Dim ctl As Control
If frm.NewRecord And frm.Dirty Then
Select Case lngAnswer
Case vbYes 'Saves new record and checks if required fields are
null
For Each ctl In frm.Controls
If ctl.Tag = "reqd" Then 'required fields are tagged
with reqd
If Len(ctl.Value & vbNullString) = 0 Then
MsgBox "You must enter a value into """ & _
ctl.Controls(0).Caption & """.",
vbOKOnly, "Required field"
Cancel = True
ctl.SetFocus
ctl.BackColor = 16777088 'highlights ctl
Else
frm.refresh
End If
End If
Next ctl
Case vbNo 'Discards new record
Cancel = True
frm.Undo
Case vbCancel 'cancels save
Cancel = True
End Select
End If
End Function
This discussion group has been great and has helped me many times already.
Thanks in advance and any assistance is appreciated,
george
I am in need of some assistance with coding a function that checks bound
required fields on a form before saving a new record. It works fine, but the
problem that I am having is that when all the fields are complete, I would
like the form to be refreshed. As the code is written now, it refreshes the
data even if the required fields are incomplete. So essentially: Incomplete
data = no refresh, Complete data = refresh.
Here is my code:
Function SaveNewRecord(frm As Form)
' save function for new record - prompts save msgbox
Dim strMsg As String
strMsg = "New record has not been saved."
strMsg = strMsg & " Do you wish to save?"
Dim lngAnswer As Long
lngAnswer = MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save New Record?")
Dim ctl As Control
If frm.NewRecord And frm.Dirty Then
Select Case lngAnswer
Case vbYes 'Saves new record and checks if required fields are
null
For Each ctl In frm.Controls
If ctl.Tag = "reqd" Then 'required fields are tagged
with reqd
If Len(ctl.Value & vbNullString) = 0 Then
MsgBox "You must enter a value into """ & _
ctl.Controls(0).Caption & """.",
vbOKOnly, "Required field"
Cancel = True
ctl.SetFocus
ctl.BackColor = 16777088 'highlights ctl
Else
frm.refresh
End If
End If
Next ctl
Case vbNo 'Discards new record
Cancel = True
frm.Undo
Case vbCancel 'cancels save
Cancel = True
End Select
End If
End Function
This discussion group has been great and has helped me many times already.
Thanks in advance and any assistance is appreciated,
george