Hi all. I have a bit of code that I want to run when a Database starts up.
It has to run a query, then send an email if the query shows up a particular
result.
However, I am getting an error as soon as the database opens. It is a Run
Time Error 13, type mismatch and it seems to be having a problem with this
line of code:
Set recOverBudget = db.OpenRecordset("qryGetoverbudget")
The query name is spelt correctly, and definetly returns results.
Any Idea what is going on?
Here is all the code:
Public Sub checkit()
' Start up code run when database is started. It will send an email to
notify the admin of which clients are over budget
' Define some variables first
Dim db As Database ' current database
Dim recOverBudget As Recordset ' recordset of those
client over budget
Dim objOutlook As New Outlook.Application ' outlook object
Dim objMessage As MailItem ' outlook mail message
Dim strMessageBody As String ' body of email message
' open the database and recordset of clients over budget
Set db = CurrentDb()
Set recOverBudget = db.OpenRecordset("qryGetoverbudget")
' now create a list of clients to add to the body of the email
While Not recOverBudget.EOF
' create a string containing the order details
strMessageBody = strMessageBody & "," & recOverBudget("Client")
recOverBudget.MoveNext
Wend
' Now send the email to the administrator, informing them which clients
are over budget this month
Set objMessage = objOutlook.CreateItem(olMailItem)
With objMessage
.Subject = "New Order"
.Body = "The following clients are over budget this month" &
vbCrLf & vbCrLf & strMessageBody
.Send
End With
End Sub
Regards
Will