Using query to populate email body

wilsation

Registered User.
Local time
Today, 02:58
Joined
Jul 28, 2014
Messages
44
Hi, I'm using the code below to create an email and then populate it with the results of the query - this works fine.
Note the WHERE part of the query - we'll be revisiting it soon....

Code:
Private Sub CmdEmail_Click()
 
    Dim olApp As Object
    Dim olItem As Variant
    Dim olatt As String
    Dim olMailTem As Variant
    Dim strSendTo As String
    Dim strMsg As String
    Dim strTo As String
    Dim strcc As String
    Dim rst As DAO.Recordset
    Dim rs As DAO.Recordset
    Dim db As DAO.Database
    Dim qry As DAO.QueryDef
    Dim fld As Field
    Dim varItem As Variant
    Dim strtable As String
    Dim rec As DAO.Recordset
    Dim strqry As String
 
                strqry = "SELECT tblRTBApps.[CHS No], tblRTBApps.[RTB1], chs_table.ADDRESS, chs_table.PostCode " & _
"FROM chs_table INNER JOIN tblRTBApps ON chs_table.CHS_NO = tblRTBApps.[CHS No] " & _
"WHERE (((tblRTBApps.[RTB1]) Between (Date()-5) And Date())) " & _
"ORDER BY tblRTBApps.[RTB1] DESC;"
    strSendTo = ""
    strTo = "[EMAIL="xxx@xxx.co.uk"]some email address[/EMAIL]"
    strcc = ""
 
    Set olApp = CreateObject("Outlook.application")
    Set olItem = olApp.createitem(olMailTem)
 
    olItem.Display
    olItem.To = strTo
    olItem.cc = strcc
    olItem.Body = "Some body text here"
    olItem.Subject = "subject text here"
 
    Set db = CurrentDb
    Set rec = CurrentDb.OpenRecordset(strqry)
    If Not (rec.BOF And rec.EOF) Then
        rec.MoveLast
        rec.MoveFirst
        intCount = rec.RecordCount
            For intLoop = 1 To intCount
                olItem.Body = olItem.Body & rec("[CHS No]") & " " & rec("[Address]") & " " & rec("PostCode")
                rec.MoveNext
            Next intLoop
            End

Currently the query selects all where the date is within the last five days:
Code:
WHERE (((tblRTBApps.[RTB1]) Between (Date()-5) And Date()))

Instead of this users would like to choose the dates used in the query. I've created a form with two text boxes, txtStartDate and txtEndDate and a Submit button (which now fires the email code) so the user selects the dates which are passed to the query.

The WHERE clause now looks like this:
Code:
WHERE (((tblRTBApps.[RTB1]) Between " & Me.[txtStartDate] & " And " & Me.[txtEndDate] & "

This works fine when I run the query on its own, I get the results expected. However, when I add the new WHERE clause to the email code the email is generated but not populated with the results.
There's no error - just no results of the query.

Please help, I've spent a couple of days on this now.

Thanks
 
Needs delimiters:

WHERE (((tblRTBApps.[RTB1]) Between #" & Me.[txtStartDate] & "# And #" & Me.[txtEndDate] & "#"
 
Hi there, thanks for the reply, my WHERE now looks like this and generates this error:

Code:
"WHERE (((tblRTBApps.[RTB1]) Between #" & Me.[txtStartDate] & "# And #" & Me.[txtEndDate] & "# "

Runtime error 3075
Syntax error (missing operator) in query expression
 
Add a Debug.Print strqry to you code before opening the recordset.
This will print your SELECT query to the immediate window , (Press ctrl-G in the debug window.

Copy and paste that here or just view it to see what is actually being passed to the query engine.
 
PMFJI,

Would not the dates also have to be in mm/dd/yyy format?
 
I'll get out of the way.
 
Hi Minty, this is what is being sent when I select dates, can't see too much wrong with it tbh.

SELECT tblRTBApps.[CHS No], tblRTBApps.[RTB1], chs_table.ADDRESS, chs_table.PostCode FROM chs_table INNER JOIN tblRTBApps ON chs_table.CHS_NO = tblRTBApps.[CHS No] WHERE (((tblRTBApps.[RTB1]) Between #07/11/16# And #30/11/16# ORDER BY tblRTBApps.[RTB1] DESC;
 
I don't think your dates are being parsed as dates. I'm sure the year should be four digit 2016 , and as Gasman correctly pointed out they should be in american format.
Set the format on the form controls to short date and see if that cures the first part of the problem.

You may then want to apply a format, have a look here for a repeatable way of handling the US / UK Date issue http://allenbrowne.com/casu-08.html
 
Ok, so I'm now sending the dates in American format.

SELECT tblRTBApps.[CHS No], tblRTBApps.[RTB1], chs_table.ADDRESS, chs_table.PostCode FROM chs_table INNER JOIN tblRTBApps ON chs_table.CHS_NO = tblRTBApps.[CHS No] WHERE (((tblRTBApps.[RTB1]) Between #11/02/2016# And #11/30/2016# ORDER BY tblRTBApps.[RTB1] DESC;

However, I still get the same syntax error on the WHERE clause.

Thanks
 
YES YES YES!
Got it, I'd left a bracket open after changing to American format.

Thanks all for your help.

Much appreciated
 
I'll get out of the way.

Don't be like that Paul. :(

I am just trying to see if I have got an idea as to what might be wrong for my benefit. Having to have dates in USA format seems to trip a lot of people up especially when you can put dd/mm/yyyy format albeit with # delimiters in the query designer.
 

Users who are viewing this thread

Back
Top Bottom