Run report based on SQL

eugz

Registered User.
Local time
Today, 13:30
Joined
Aug 31, 2004
Messages
128
Hi All.
I tried to run report and used such code:
Code:
Private Sub lblDelivery_Click(Sql As String)
    Sql = "SELECT Receiving.ReceiveID, Receiving.Date, Devices.Device, qLocation.Location, Receiving.Amount" & _
    "FROM Devices" & _
    "INNER JOIN Receiving" & _
    "ON Devices.DeviceID = Receiving.DeviceID)" & _
    "INNER JOIN qLocation ON Receiving.ReceiveID = qLocation.ReceiveID" & _
    "WHERE Receiving.Date Between [Enter First Date] And [Enter Last Date]" & _
    "ORDER BY Devices.Device, Receiving.Date;"
    
    stDocName = "rDelivery"
    DoCmd.OpenReport stDocName, acPreview
    DoCmd.Maximize
    
    DoCmd.RunSQL Sql

End Sub
What is my problem?
Thanks.
 
Hello:

I believe you need to set the RecordSource property of your report to your SQL statement!


rDelivery.RecordSource = Your SQL Statement

Regards
Mark
 
Thank Mark for replay.
When I use your suggestion I got run-time error '424' Object required.

Previously I had run-time error '3075' Syntax error (missing operator) in query expression:
"[Devices].[DeviceID] = [Receiving].[DeviceID] INNER JOIN qLocation ON [Receiving].[ReceiveID] = [qLocation].[ReceiveID]"

Do you have idea to fixed it?
Thanks
 
Last edited:
You certainly can't do it your way, and while Mark is on the right track, you'd either have to open the report in design view or make the variable global and use it in the report's open event. The real question is why you're doing it this way, rather than simply making that SQL the permanent source for the report. There's nothing dynamic about it, so no reason for it to be set in code.

Btw, you'd also run into spacing problems with that SQL.
 
I would like to open report rDelivery by click lable lblDelivery and get first message to enter Start Date and then get second message to enter End Date. If you know how to do that for better way I will appreciate if you show how to do it.
Thanks.
 
Sure; either enter that SQL directly in the report's record source property (without the quotes "&" and line continuations characters of course) and save it that way, or create a saved query with that SQL and base the report on that saved query. Either way, you'll get the 2 date prompts when running the report. You really only need to build SQL in VBA when there's something dynamic about it (something about the structure of the SQL itself changes).
 

Users who are viewing this thread

Back
Top Bottom