Don't want SQL statement to execute if a field is empty

  • Thread starter James Grady via AccessMonster.com
  • Start date
J

James Grady via AccessMonster.com

HI,

This is my first message to post out here on the site. But i have been an
avid user of the site. It has been very helpful in the past. However i
have ran into a problem i cannot get past and i am sure it will be really
easy to fix. I have a form that has 5 textboxes, and when all the
textboxes have been filled, the user clicks a button, and the button
executes and "Insert into" statement that puts the values in the table. So
now i am trying to do some error handling. I want a MSGbox to pop up if
one of the fields hasn't been entered or is left blank (cuz all the fields
need to be entered), and also...to not execute the sql statement.
Currently, when i leave one of the fields blank and then click the button,
the message box comes up sayin "Some field must be entered", but it also
executes the SQL statement, adding the record to the table. How do i go
about this? Do i create a Iif statement or what? here is my code for the
form:


Private Sub btnaddemp_Click()

Dim strSQL As String

If Nz(txtFullname, "") = "" Then

MsgBox "Need Employee Please"
Cancel = True
Me.txtFullname.SetFocus
End If


strSQL = "INSERT INTO Employee (FullName, Address, Phone, Email)
VALUES('" & Me!txtFullname & "', '" & Me!txtAddress & "', '" & Me!txtPhone
& "', '" & Me!txtEmail & "')"

'Using DAO
CurrentDb.Execute strSQL


MsgBox "Customer has been added!"




'Reset form for next new record ...
'Me!txtFullname = Null
'Me!txtAddress = Null
'Me!txtPhone = Null
'Me!txtEmail = Null
End Sub
 
K

Klatuu

The reason the SQL is executing is because it is after the End If where you
check txtFullname. Below is (untested code) what will solve your problem (I
Hope)
Private Sub btnaddemp_Click()

Dim strSQL As String
Dim blnOkGo As Boolean

blnOkGo = True
If Nz(txtFullname, "") = "" Then

MsgBox "Need Employee Please"
blnOkGo = False
Me.txtFullname.SetFocus
End If

If blnOkGo And Nz(txtAddress, "") = "" Then
MsgBox "Need Address Please"
blnOkGo = False
Me.txtAddress.SetFocus
End If
!!!!! Repeat The Above for each field. Note that once blnOkGo = False
!!!!! Nothing else executes.

If BlnOkGo Then
strSQL = "INSERT INTO Employee (FullName, Address, Phone, Email)
VALUES('" & Me!txtFullname & "', '" & Me!txtAddress & "', '" &
Me!txtPhone
& "', '" & Me!txtEmail & "')"

'Using DAO
CurrentDb.Execute strSQL


MsgBox "Customer has been added!"

'Reset form for next new record ...
'Me!txtFullname = Null
'Me!txtAddress = Null
'Me!txtPhone = Null
'Me!txtEmail = Null
End If
End Sub
 
J

James Grady via AccessMonster.com

Thank you very much for your help. You guys are awesome!! I will
definitely bring all my problems to you guys. :) haha!
 

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