Problem with Email VBA Code

jjake

Registered User.
Local time
Yesterday, 18:45
Joined
Oct 8, 2015
Messages
291
Hello,

So i have designed a work order request form that a user fills in. When the user clicks submit to submit the report generated the form checks to make sure the text boxes have values/data in them. If it doesn't it prompts the user with a message box. When all boxes are complete and the vba code acknowledges that all data required is there, it generates a report and fires off an email.

For some reason whenever i get all the data filled in and click submit, the form just flashes at me like it's refreshing and doesn't do nothing. It worked just fine before i put the prompts in there.

Code:
Private Sub Command569_Click()

Dim strReport As String

strReport = "rptNewWorkOrder"
Me.Refresh

If IsNull(Me.RequistionedBy) Then
MsgBox "Who is this workorder requested by?"

ElseIf IsNull(Me.RequistionedDate) Then
MsgBox "What is the date of this workorder request?"

ElseIf IsNull(Me.cboEquipmentType) Then
MsgBox "What is the equipment Type?"

ElseIf IsNull(Me.cboPlantNum) Then
MsgBox "What is the Plant #?"

ElseIf IsNull(Me.Combo577) Then
MsgBox "What is the Unit/Equipment ID?"

ElseIf IsNull(Me.Priority) Then
MsgBox "What is the priority of this work order?"

ElseIf IsNull(Me.RepairType) Then
MsgBox "What is the repair type of this work order?"

ElseIf IsNull(Me.WorkScope) Then
MsgBox "What is the scope of work?"

End If
Exit Sub

DoCmd.OpenReport strReport, acViewPreview, , "WorkORderid = " & Me!WorkOrderID

DoCmd.SendObject acSendReport, "rptNewWorkOrder", "PDF", "First.Last@Email.com;First.Last@Email.com", , , "New Work Order", "", False

DoCmd.Close acForm, "frmworkorderrequestnew", acSaveYes
DoCmd.Close acReport, "rptNewWorkOrder", acSaveYes

End Sub
 
You're telling your code to exit before doing anything.
Your Exit Sub needs to be within the if statement.
Code:
Private Sub Command569_Click()

Dim strReport As String

strReport = "rptNewWorkOrder"
Me.Refresh

If IsNull(Me.RequistionedBy) Then
MsgBox "Who is this workorder requested by?"

ElseIf IsNull(Me.RequistionedDate) Then
MsgBox "What is the date of this workorder request?"

ElseIf IsNull(Me.cboEquipmentType) Then
MsgBox "What is the equipment Type?"

ElseIf IsNull(Me.cboPlantNum) Then
MsgBox "What is the Plant #?"

ElseIf IsNull(Me.Combo577) Then
MsgBox "What is the Unit/Equipment ID?"

ElseIf IsNull(Me.Priority) Then
MsgBox "What is the priority of this work order?"

ElseIf IsNull(Me.RepairType) Then
MsgBox "What is the repair type of this work order?"

ElseIf IsNull(Me.WorkScope) Then
MsgBox "What is the scope of work?"

End If
[COLOR="Red"]Exit Sub[/COLOR]

DoCmd.OpenReport strReport, acViewPreview, , "WorkORderid = " & Me!WorkOrderID

DoCmd.SendObject acSendReport, "rptNewWorkOrder", "PDF", "First.Last@Email.com;First.Last@Email.com", , , "New Work Order", "", False

DoCmd.Close acForm, "frmworkorderrequestnew", acSaveYes
DoCmd.Close acReport, "rptNewWorkOrder", acSaveYes

End Sub
 
So I removed the end sub and now it triggers the event, but it still triggers after the msgbox has notified the user. How would I get it to not send the email unless the boxes were complete?
 
Move that code into an Else Clause:

If...
ElseIf...
ElseIf...
Else
your code here
End If
 
No problem. TJ looked to be offline, but would likely have had the same answer.
 

Users who are viewing this thread

Back
Top Bottom