Form validation

borolee13

Registered User.
Local time
Today, 02:49
Joined
May 11, 2005
Messages
10
I am trying to validate fields in a form, before an email is sent saying the form is OK. this is my code :

Private Sub Save_Click()

If Me.Team = "" Then
MsgBox "You must enter your Team name", "Data Validation"
Me.Team.SetFocus

ElseIf Me.Customer = "" Then
MsgBox "You must enter the Customer Name", "Data Validation"
Me.Team.SetFocus

ElseIf Me.Address = "" Then
MsgBox "Your must enter the 1st line of the address", "Data Validation"
Me.Address.SetFocus

ElseIf Me.Address1 = "" Then
MsgBox "You must enter the 2nd line of the address", "Data Validation"
Me.Address1.SetFocus

ElseIf Me.QueryContact = "" Then
MsgBox "You must enter a Contact name", "Data Validation"
Me.QueryContact.SetFocus

ElseIf Me.QueryPhone = "" Then
MsgBox "You must enter a Contact telephone number", "Data Validation"
Me.QueryPhone.SetFocus
End If

DoCmd.Save
DoCmd.SendObject , , , Me.ReqBy, , , "Invoice Requisition " & Me.HeaderID, "You have submiited an Invoice Requistion to Finance. The requistion was too " & Me.Customer & " and totalled " & Me.InvTotal & ". You will receive a further email confirming the Invoice Number, once Finance have issued the invoice.", no
DoCmd.SendObject , , , "lee.mason", , , "Invoice Requistion " & Me.HeaderID, "An invoice requisition from " & Me.ReqBy & " is waiting to be authorised.", no

End Sub

Can someone please explain why the email sends, even though some of the fields are not completed.

Thanks,
Lee.
 
hi,

exit yout sub if the validations fails.

ex-


Private Sub cmdSave_Click()
On Error GoTo Err_cmdSave_Click

If IsNull(Me!fieldname) Then
MsgBox "Please select a fieldname", vbCritical + vbOKOnly + vbDefaultButton1, "Missing data"
Me![fieldname].SetFocus
GoTo Exit_cmdSave_Click
End If


Exit_cmdSave_Click:
Exit Sub

Err_cmdSave_Click:
MsgBox Err.Description
Resume Exit_cmdSave_Click
end sub
 
Should be If ((IsNull(Me.SomeControl) Or Me.SomeControl="" ) Then
etc
Also DoCmd.Save is the wrong method, it's
RunCommand acCmdSaveRecord
 
Thanks..Yes i always check the empty string as well as the Null like

If ((IsNull(Me.SomeControl) Or Me.SomeControl="" )

I just wrote that to give a example.I will take care to post things right in future.

One more thing ..Once Pat pointed me out that all the validations should on the before update event.
 
So it is ok to have this on a Save button on the form???
 
Thanks for the help i received.

I have now changed my code as suggested, but when i click on the Save Input button, I now get 'Run Time Error 13 - Type Mismatch'

When i debug, it goes to the following line of the code:

ElseIf ((IsNull(Me.Address1) Or Me.Address1 = "")) Then
MsgBox "You must enter the 2nd line of the address", "Data Validation"
Me.Address1.SetFocus

and highlights the middle line.

In the form, i have left the address1 field blank to test the code.

Any advice please??
 
Does anyone understand why I am having this problem????
 
MsgBox "You must enter the 2nd line of the address", "Data Validation"
You are missing a required parameter.

Code:
MsgBox "You must enter the 2nd line of the address!", vbInformation, "Data Validation"
 
Thanks for that.

I have now found another problem. If all of the fields have been completed, then I want to send an email too the user. If not, i don't, until all the fields have been completed.

Thnis is the end of my code, and it sends an email even when all the fields ar not complete.

I'm sure i am missing something really easy here :

ElseIf ((IsNull(Me.QueryPhone) Or Me.QueryPhone = "")) Then
MsgBox "You must enter a Contact telephone number", vbInformation, "Data Validation"
Me.QueryPhone.SetFocus

RunCommand acCmdSaveRecord
DoCmd.SendObject , , , Me.ReqBy, , , "Invoice Requisition " & Me.HeaderID, "You have submiited an Invoice Requistion to Finance. The requistion was too " & Me.Customer & " and totalled £" & Me.InvTotal & ". You will receive a further email confirming the Invoice Number, once Finance have issued the invoice.", no
DoCmd.SendObject , , , "lee.mason", , , "Invoice Requistion " & Me.HeaderID, "An invoice requisition from " & Me.ReqBy & " is waiting to be authorised.", no

End Sub
 
You need to exit the sub after you give the user the warning message box.

ElseIf ((IsNull(Me.QueryPhone) Or Me.QueryPhone = "")) Then
MsgBox "You must enter a Contact telephone number", vbInformation, "Data Validation"
Me.QueryPhone.SetFocus
Exit Sub

You need to use the code tags in your post for it makes it easier to read your code if you use tabs and line breakes.

You code should look something like this...

Code:
If [I]something[/I]

ElseIf [I]something[/I] 

Else [I]something[/I]
 
End If
 
Thanks Pat.

Where else would you suggest that i put the code???
 

Users who are viewing this thread

Back
Top Bottom