I have the following code which if a record exists creates a PDF and sends an e-mail
Now I may have more than one outstanding record in my table but "QryLoadNo" only shows the one record at a time so that I can create the PDF & E-mail to the relevant user after sending the e-mail I then update the record to complete status, sometimes there may only be one outstanding record. what I would like is that it will run the code if a outstanding record exists and then run the code again if there is another outstanding record in "QryLoadNo" but I am unsure on how to create a loop to do this
Any help would be much appreciated
Regards
Paul
Code:
Public Function SendMail()
Dim strFilename As String
Dim strNewName As String
Dim strHead As String
Dim strSub As String
Dim ddate As String
Dim strfrp As String
Dim strPlant As String
Dim rsCount As Integer
rsCount = DCount("*", "QryLoadNo")
If rsCount = 0 Then 'Check to see if there are any records, if not quit the application
DoCmd.CancelEvent
Else
DoCmd.OpenForm "frmDespatchData", acNormal
DoCmd.OpenReport "rptLoadDetails", acViewPreview, "", "", acNormal
strFilename = Forms![frmDespatchData]![lstCustomer]
strSub = " "
strLoadID = Forms![frmDespatchData]![txtLoadID]
strPlant = Forms![frmDespatchData]![txtPlant]
D = Format(Now, "dd")
m = Format(Now, "mm")
Y = Format(Now, "yy")
ddate = D & "-" & m & "-" & Y
strHead = "Pallet Transfer Details From " & strPlant & " To Wellington Dated " & ddate & " Load ID_" & strLoadID
strNewName = DLookup("[Path]", _
"tblWMail_FilePaths", "[Type] = 'E-mail'") & strFilename & "_" & ddate & "_" & "LoadID_" & strLoadID & ".pdf"
Call SaveReportAsPDF("rptLoadDetails", strNewName)
Dim patha, pathT, pathC, pathH, pathS, CustMailTo, CustMailCC As String
CustMailTo = Forms![frmDespatchData]![txtContact1]
CustMailCC = Forms![frmDespatchData]![txtContact2]
patha = strNewName
strSub = "This Is A Test Message In Regards To Transfers" & _
vbCrLf & vbCrLf & _
"Please find attached Transfer Details For Load ID" & strLoadID & _
vbCrLf & vbCrLf & _
vbCrLf & vbCrLf & _
"PLEASE NOTE: Do Not Reply To This E-Maill Address. If You Require Information Regarding This Transfer" & _
vbCrLf & vbCrLf & _
"Please Contact The Relevant Site"
pathT = CustMailTo
pathC = CustMailCC
pathH = strHead
pathS = strSub
Dim Session As vbMAPI_Session
Dim Store As vbMAPI_Store
Dim Folder As vbMAPI_Folder
Dim FolderItems As vbMAPI_FolderItems
Dim Item As vbMAPI_MailItem
Set Session = vbMAPI_Init.NewSession
Session.LogOn
Set Store = Session.Stores.DefaultStore
Set Folder = Store.GetDefaultFolder(FolderType_Drafts)
Set FolderItems = Folder.Items
Set Item = FolderItems.Add
Item.To_ = pathT ' Add the To recipient(s) to the message.
Item.CC = pathC ' Add the CC recipient(s) to the message.
Item.Subject = pathH ' Set the Subject
Item.Body = pathS 'Set The Body
Item.Importance = Importance_High 'Set The Importance of the message.
Item.Attachments.Add patha ' Add Any Attachments
With Session.AddressBook.ResolveName("deliverynotification@xxxxx.co.uk") ' Set the "From" field
Item.SenderName = .Name
Item.SenderEntryID = .EntryID
Item.SenderSearchKey = .SearchKey
Item.SentOnBehalfOfName = .Name
Item.SentOnBehalfOfEntryID = .EntryID
Item.SentOnBehalfOfAddressType = .AddressType
Item.SentOnBehalfOfEmailAddress = .Address
Item.SentOnBehalfOfSearchKey = .SearchKey
End With
For Each Recipient In Item.Recipients ' Resolve each Recipient's name.
Recipient.Resolve
Next
On Error Resume Next
Item.Display
If Err.Number = MAPI_E_USER_CANCEL Then
MsgBox "User cancelled the Outlook dialog. Continuing…"
ElseIf Err.Number <> 0 Then
MsgBox Err.Description ' Some other error
End If
On Error GoTo 0 ' turns off previous ‘on error resume next’ statement
DoCmd.OpenQuery "QryWM02b_Update_Sent", acViewNormal
End If
End Function
Now I may have more than one outstanding record in my table but "QryLoadNo" only shows the one record at a time so that I can create the PDF & E-mail to the relevant user after sending the e-mail I then update the record to complete status, sometimes there may only be one outstanding record. what I would like is that it will run the code if a outstanding record exists and then run the code again if there is another outstanding record in "QryLoadNo" but I am unsure on how to create a loop to do this
Any help would be much appreciated
Regards
Paul