I need more help with my Leave card database, I am trying to set up some code that emails the authorising managers using a query and a subquery to email each manager with the leave requests for them. To do this I have basically cannibalised some code that I used in another database which was set up to email several people, each with several records in a subquery, so I know that this code works, I've just basically changed the names of things.
It keeps getting stuck on the recordset, I put:
Set rst=dbs.OpenRecordset("QOUtgoingAuthReqs", dbOpenSnapshot)
Which is exactly the same as my other database, but when I run it, I get an error message saying too few parameters and highlighting this line of code, saying that rst = Nothing.
I've pasted the whole code below so that you can see that I've defined everything and set the dbs, hope someone can tell me what's wrong?
It keeps getting stuck on the recordset, I put:
Set rst=dbs.OpenRecordset("QOUtgoingAuthReqs", dbOpenSnapshot)
Which is exactly the same as my other database, but when I run it, I get an error message saying too few parameters and highlighting this line of code, saying that rst = Nothing.
I've pasted the whole code below so that you can see that I've defined everything and set the dbs, hope someone can tell me what's wrong?
Code:
Private Sub SendReqs_Click()
Call MailGuard
End Sub
Sub MailGuard()
'Need to re-word this MailGuard
Dim Display As Variant
If MsgBox("Do you want to display the emails before sending?", vbYesNo, "You are about to Mail") = vbNo Then
Display = "NO"
If MsgBox("This will send the emails without further warning!" + vbCrLf + vbCrLf + "ARE YOU SURE YOU WISH TO SEND?", vbYesNo, "You are about to Mail") = vbNo Then
Display = "NO"
Else
Call MattSendMail(Display)
End If
Else
Display = "YES"
Call MattSendMail(Display)
End If
End Sub
Sub MattSendMail(Display As Variant)
Dim dbs As Database, rst As Recordset, CurrentForm As Form
Dim DN As Variant, UN As Variant, RN As Variant, CID As Variant, OLRN As String, OLUN As String
Dim Counter As Integer
Set dbs = CurrentDb
Set CurrentForm = Screen.ActiveForm
DoCmd.Hourglass True
Counter = 0
Set rst = dbs.OpenRecordset("QOutgoingAuthReqs", dbOpenSnapshot)
rst.MoveFirst
Do
DN = rst.Fields![DispName]
UN = rst.Fields![UserName]
RN = rst.Fields![NRID]
OLRN = rst.Fields![Run]
OLUN = rst.Fields![UN]
CID = Me![CID]
Call MattSendMailMessage(DN, UN, RN, OLRN, OLUN, CID, Display)
Counter = Counter + 1
If Not rst.EOF Then rst.MoveNext
Loop Until rst.EOF
DoCmd.Hourglass False
If Display = "NO" Then
MsgBox "You have just sent:" + Str(Counter) + " Emails"
Else
MsgBox "You have just prepared:" + Str(Counter) + " Emails"
End If
rst.Close
Set dbs = Nothing
End Sub
Sub MattSendMailMessage(DN As Variant, UN As Variant, RN As Variant, OLRN As Variant, OLUN As Variant, CID As Variant, Display As Variant)
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim dbs As Database, rst1 As Recordset, SQLString As String, strLeave As String
SQLString = "SELECT QOutgoingRequests.*" _
& " FROM QOutgoingRequests" _
& " WHERE (((QOutgoingRequests.NRID)='" & RN & "'));"
Set dbs = CurrentDb
Set rst1 = dbs.OpenRecordset(SQLString, dbOpenSnapshot)
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
Do While Not rst1.EOF
strLeave = strLeave & vbCrLf & rst1.Fields("ReqLine")
rst1.MoveNext
Loop
With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(OLRN)
objOutlookRecip.Type = olTo
' Set the Subject, Body, and Importance of the message.
.Subject = "~~~LEAVE REQUEST~~~"
.Body = DN + " (" + UN + ") Requests the following leave:" + vbCrLf + vbCrLf + strLeave + vbCrLf + vbCrLf + "To Authorise, please go to Leave Card System ~~LINK~~" + vbCrLf + vbCrLf + vbCrLf + vbCrLf + "CardID: " + CID
.Importance = olImportanceNormal 'Normal importance
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
If Display = "YES" Then
.Display
Else
.Send
End If
End With
Set objOutlook = Nothing
End Sub
Last edited by a moderator: