G
Guest
I finally got that to work correctly but am now experiencing another issue.
In the query that I want to loop through each Expr1 and send a separate
report for each, the attachments being sent out are all "510". I need to
send one out for each [Expr1]. Apparently the loop isn't working correctly.
I am at a loss since this code isn't clicking on how it works. Again I
really appreciate all of your help. I am posting the updated code below so
you can see what changed. Thanks.
Private Sub cmdEmailReports_Click()
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strBaseSQL As String
Set qdf = CurrentDb.QueryDefs("qryAGHSvcCode")
strBaseSQL = qdf.SQL
Set rst = CurrentDb.OpenRecordset(strBaseSQL)
With rst
Do Until .EOF
strSQL = Left(strBaseSQL, Len(strBaseSQL) - 3)
qdf.SQL = strSQL
DoCmd.SendObject acSendReport, "rptAGHLateCharges",
"SnapshotFormat(*.snp)", !Recipient, , , "YourSubject", "YourMessage", False
.MoveNext
Loop
.Close
End With
qdf.SQL = strBaseSQL
Set qdf = Nothing
Set rst = Nothing
End Sub
In the query that I want to loop through each Expr1 and send a separate
report for each, the attachments being sent out are all "510". I need to
send one out for each [Expr1]. Apparently the loop isn't working correctly.
I am at a loss since this code isn't clicking on how it works. Again I
really appreciate all of your help. I am posting the updated code below so
you can see what changed. Thanks.
Private Sub cmdEmailReports_Click()
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strBaseSQL As String
Set qdf = CurrentDb.QueryDefs("qryAGHSvcCode")
strBaseSQL = qdf.SQL
Set rst = CurrentDb.OpenRecordset(strBaseSQL)
With rst
Do Until .EOF
strSQL = Left(strBaseSQL, Len(strBaseSQL) - 3)
qdf.SQL = strSQL
DoCmd.SendObject acSendReport, "rptAGHLateCharges",
"SnapshotFormat(*.snp)", !Recipient, , , "YourSubject", "YourMessage", False
.MoveNext
Loop
.Close
End With
qdf.SQL = strBaseSQL
Set qdf = Nothing
Set rst = Nothing
End Sub
Douglas J Steele said:Since Expr1 is a computed field in the query, you can't use it in the WHERE
clause like that.
Instead, you need
And Left([SVCCODE], 3) = '510'
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
shoe said:strBaseSQL reads the following:
SELECT [tblImportAGH].[SVCCODE], Left([SVCCODE],3) AS Expr1,
[tblImportAGH].[ACCT NUMBER], [tblImportAGH].[PT NAME], [tblImportAGH].[DOS],
[tblImportAGH].[DTL CR DATE], [tblImportAGH].[POST DATE],
[tblImportAGH].[AMOUNT], [tblImportAGH].[FILE DATE], [tblImportAGH].[InOut],
[POST DATE]-[DOS] AS Days
FROM tblImportAGH
WHERE ((([tblImportAGH].[FILE DATE])=#4/30/2005#)) And [Expr1]='510';
I did figure out the last part because I looked at the syntax and the last
line of the SQL code was reading:
WHERE ((([tblImportAGH].[FILE DATE])=#4/30/2005#)) WHERE [Expr1]='510';
I changed it to input the AND on [Expr1] instead of the second WHERE. I
hope that answers everything you needed. Thanks.
Douglas J Steele said:What does strBaseSQL look like?
I don't understand, though, how you could have gotten past the other
problem. This problem would occur before the other one!
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
I actually got the code to get past this error. I am now getting a new
error
of Error 3061 Too few parameters. I do have a file date on a form that
the
specified query uses. The form is open and populated when I run the
macro.
The code errors on the following line:
Set rst = CurrentDb.OpenRecordset(strBaseSQL)
I think my head is going to explode from all of this code.
Thank you all so much for your help. All of you are extremely helpful.
:
It sounds to me the constructed SQL String is invalid.
Add the statement:
Debug.Print strSQL
just before the statement
qdf.SQL = strSQL
Run the code and you should see the constructed SQL String in the
Immediate
window. Copy and post it to the newsgroup.
I agreed with Douglas about removing the last 3 characters from the
source
SQL. IIRC, I think you need to remove the last character (only, not
3.
--
HTH
Van T. Dinh
MVP (Access)
I have the following code which is for looping through each department
(Approximately 200) and sending a separate e-mail to the recipient
listed
in
a report. I am getting an error 3075( Missing operator) on line
qdf.SQL =
strSQL and can't figure out the issue. VBA newbie still trying to
figure
out
how to create bug free code. Thanks in advance for any assistance you
can
offer.
Private Sub cmdEmailReports_Click()
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strBaseSQL As String
Set qdf = CurrentDb.QueryDefs("qryAGHSvcCode")
strBaseSQL = qdf.SQL
Set rst = CurrentDb.OpenRecordset(strBaseSQL)
With rst
Do Until .EOF
strSQL = Left(strBaseSQL, Len(strBaseSQL) - 3)
strSQL = strSQL & " WHERE [Expr1]='" & ![Expr1] & " '"
qdf.SQL = strSQL
DoCmd.SendObject acSendReport, "rptAGHLateCharges",
"SnapshotFormat(*.snp)", !Recipient, , , "YourSubject", "YourMessage",
False
.MoveNext
Loop
.Close
End With
qdf.SQL = strBaseSQL
Set qdf = Nothing
Set rst = Nothing
End Sub