Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stopping Code With If Statement

Status
Not open for further replies.

jbento

Technical User
Jul 20, 2001
573
US
I have the following code, that copy records to a new table, and also sends email with those fields. I would like to prevent email from popping up if a condition is not met: Here is my code, that is associated with a command button:

Private Sub Command124_Click()
On Error GoTo Err_Command124_Click
Dim db As DAO.Database, rst As DAO.Recordset
'Copy Record and Save Record to new table
Set db = CurrentDb
Set rst = db.OpenRecordset("tbl_NameOfTable")
rst.AddNew

rst!field1 = Me!field1
rst!field2 = Me!field2
rst!field3 = Me!field3
rst!field4 = Me!field4

If IsNull(Me.field1) And IsNull(Me.field3) And IsNull(Me.field3) And IsNull(Me.field4) Then
MsgBox "You must enter text for field1 or field2, or field3 or field4", vbExclamation
field1.SetFocus
End If

rst.Update
rst.Close
db.Close

'send email
DoCmd.SendObject acSendNoObject, "", acFormatTXT, "emailaddresshere",,,"SubjectHere", "BodyTextHere, " & Me!field1 & ", " & Me!field2 & ", " & Me!field3 & ", " & Me!field4, True

Exit_Command124_Click:
Exit Sub

Err_Command124_Click:
MsgBox Err.Description
Resume Exit_Command124_Click
End Sub

The problem I'm having with this code, is when I hit the command button, and field 1 thru 4 are not populated, it goes ahead and brings up the email window, which is not correct. I would like for the code not to bring up the email window, until field 1 or field2, or field 3 or field 4 is populated.

Can someone help me with this. I bet it's something easy I'm not doing here.

Thanks to all.
 
On the line after the field1.setfocus put Exit Sub Maq B-)
<insert witty signature here>
 
I will try that tomorrow, and I will let you know if it works or not. Thanks for your assistance.

Jerome
 
Maquis,
I tried it, but the only thing is, once the code stops and allows the user to go back and input information into the boxes that are blank, the Submit button doesn't work. For instance, if I don't enter any data in Field 1 thru 4 and click the submit button, the code should tell me I haven't done that, then I would go back and enter where I had to, but now I need to click the submit button to finish the process. At this point, when I click the submit button, nothing happens. Can you tell me why nothing happens?

Jerome
 
I'm sorry, but I don't have a good answer for you there. The submit button should work when you press it again.

This may sound far-fetched, but I swear it's true! I had the exact same problem with one of the forms I designed. I would click the &quot;update and Exit&quot; button and the thing with just sit there and do nothing. Turns out the problem was a timer another form which called the form I was working in. When I clicked my button, I expected the form to close and go back to the &quot;timer form&quot;, but instead nothing happened. When I disabled the timer on the back form everything worked perfectly. Go figure?? Quirk of Access I guess.

I don't suppose you have any timers in your forms?


Oh, 1 more thing, looking over your code it would be a good idea to put these lines before the Exit sub statement. It probably won't make a difference with the button problem, but you never know.
rst.close
set rst to nothing
db.close
set db to nothing


Good Luck


Maq B-)
<insert witty signature here>
 
Hey, thanks anyway my man. I will try the other part with the Exit Sub. I really thank you for your help in this. Take care:).

Jerome
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top