Form validation (1 Viewer)

borolee13

Registered User.
Local time
Today, 19:44
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.
 

roh_8_it_3

Registered User.
Local time
Today, 12:44
Joined
Feb 15, 2005
Messages
79
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
 
R

Rich

Guest
Should be If ((IsNull(Me.SomeControl) Or Me.SomeControl="" ) Then
etc
Also DoCmd.Save is the wrong method, it's
RunCommand acCmdSaveRecord
 

roh_8_it_3

Registered User.
Local time
Today, 12:44
Joined
Feb 15, 2005
Messages
79
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.
 

borolee13

Registered User.
Local time
Today, 19:44
Joined
May 11, 2005
Messages
10
So it is ok to have this on a Save button on the form???
 

borolee13

Registered User.
Local time
Today, 19:44
Joined
May 11, 2005
Messages
10
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??
 

borolee13

Registered User.
Local time
Today, 19:44
Joined
May 11, 2005
Messages
10
Does anyone understand why I am having this problem????
 

ghudson

Registered User.
Local time
Today, 15:44
Joined
Jun 8, 2002
Messages
6,195
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"
 

borolee13

Registered User.
Local time
Today, 19:44
Joined
May 11, 2005
Messages
10
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
 

ghudson

Registered User.
Local time
Today, 15:44
Joined
Jun 8, 2002
Messages
6,195
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
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:44
Joined
Feb 19, 2002
Messages
42,973
All working ok now
Are you sure? Your code is in the wrong event. If the user just closes the form or scrolls to a new record, the current record will be saved but none of your code will be executed because the user didn't press the save button.
 

borolee13

Registered User.
Local time
Today, 19:44
Joined
May 11, 2005
Messages
10
Thanks Pat.

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

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:44
Joined
Feb 19, 2002
Messages
42,973
There are hundreds of posts that cover where edit code should go. The BeforeUpdate event of the form is the last event executed before a record is saved. It is ALWAYS executed when a form is closed or the form is scrolled to a different record or if the data is expressly saved among other things.

Your code needs to be split. You need to save the current record in the click event of the save button but not with DoCmd.Save - that saves the form NOT the record. Your record is being saved but not by this code. It is silently being saved by Access as I described in the first paragraph. Use - DoCmd.RunCommand acCmdSaveRecord - to save the current record.

By expressly saving the current record, you cause the form's BeforeUpdate event to fire. It will execute your edit code. The code needs to be changed though because each failed edit must cancel the update. So to just take one edit as an example:
Code:
If IsNull(Me.Team) Or Me.Team = "" Then
    MsgBox "You must enter your Team name", vbInformation, "Data Validation"
    Me.Team.SetFocus
    Cancel = True
 

Users who are viewing this thread

Top Bottom