how to do this bit of code better?

D

DawnTreader

Hello All

If Me.SRServRepWOValid = True Then
If IsNull(Me.PONumber) Then
MsgBox "Has the Customer issued a Purchase Order for the service?"
Me.PONumber.SetFocus
Else
If IsNull(Me.WorkOrderNumber) Then
MsgBox "Service Reports cannot be submitted with out a work
order.", vbOKOnly, "Work Order Required"
Me.WorkOrderNumber.SetFocus
Else
If IsNull(Me.ReasonForCall) Then
MsgBox "What was the reason for the service call?",
vbOKOnly, "Reason for Service Empty"
Me.ReasonForCall.SetFocus
Else
If IsNull(Me.ServicePerformed) Then
MsgBox "What service did you perform?", vbOKOnly,
"Service Performed Empty"
Me.ServicePerformed.SetFocus
Else
If IsNull(Me.SRSubmittedDate) Then
Me.SRSubmittedDate = Date
End If
Me.SRStatusID = 2
Call cmdSave_Click
End If
End If
End If
End If
End If

i have tried to think of a better way to do this, in the past i have used a
validation system like this:

Private Sub cmdClose_Click() '3rd level action

If (ValidateForm = True) Then
DoCmd.Close acForm, "frmManageServiceReports"
Call refresh_lists
Else
Me.ServiceReportDate.SetFocus
End If

End Sub
Private Function ValidateForm()

Dim Valid As Boolean

Valid = True

Select Case Me.ServiceReportDate
Case Me.ServiceReportDate > Now()
MsgBox "Service Report Date is in the Future", vbOKOnly, "Date
Error"
Valid = False
Case IsNull(Me.ServiceReportDate) = True
MsgBox "Service Report Date is missing.", vbOKOnly, "Date
Required"
Valid = False
End Select

ValidateForm = Valid

End Function

problem is that i need the code to drop the user at the first field that
isnt valid.

anyone have an idea of how to cause the validation to send the user to the
field that first caused the validation to fail?
 
C

ChrisJ

You can use your "in the past" code by
changing the "Valid = False" statement in each "Case" clause to
"ValidateForm = False"
adding an "Exit function" statement in each "Case" clause immediately after
the "ValidateForm = False" statement
 
D

DawnTreader

Nice!

that exit function thing is great! one question about it, if i know i am
going to be using the exit i guess i should close any open record sets and
stuff like that first right?

additionally i suppose i can do this as well without "penalty"?

If IsNull(Me.WorkOrderNumber) Or Len(Trim(Me.WorkOrderNumber)) = 0 Then
ValidateForm = False
MsgBox "Please enter a work order number", vbOKOnly, "Work Order
Missing"
Me.WorkOrderNumber.SetFocus
Exit Function
End If

thanks again. this has helped in more than just one situation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top