Results 1 to 10 of 10
  1. #1
    Rpschwar is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    29

    SQL statement from passed Parameters

    I have a Query that passes parameters and I want to see what the SQL statement is after the parameters are passed. Here is the Query with Parameters:
    PARAMETERS Forms!RosterDialogBox!BeginningDate DateTime, Forms!RosterDialogBox!EndingDate DateTime;


    SELECT tblAttendance.RehearsalDate, tblAttendance.ID, tblAttendance.DateEntered, tblMembers.FirstName, tblMembers.LastName, tblInstrument.Instrument, tblInstrument.GroupNbr, tblInstrument.OrderNbr
    FROM (tblAttendance INNER JOIN tblMembers ON tblAttendance.ID = tblMembers.ID) INNER JOIN tblInstrument ON tblMembers.Instrument = tblInstrument.Instrument
    WHERE (((tblAttendance.RehearsalDate) Between [Forms]![RosterDialogBox]![BeginningDate] And [Forms]![RosterDialogBox]![EndingDate]))
    ORDER BY tblAttendance.RehearsalDate DESC , tblInstrument.GroupNbr, tblInstrument.OrderNbr, tblMembers.LastName, tblMembers.FirstName;

    I want to see an output like:
    SELECT tblAttendance.RehearsalDate, tblAttendance.ID, tblAttendance.DateEntered, tblMembers.FirstName, tblMembers.LastName, tblInstrument.Instrument, tblInstrument.GroupNbr, tblInstrument.OrderNbr
    FROM (tblAttendance INNER JOIN tblMembers ON tblAttendance.ID = tblMembers.ID) INNER JOIN tblInstrument ON tblMembers.Instrument = tblInstrument.Instrument
    WHERE (((tblAttendance.RehearsalDate) Between #1/1/2016# And #1/1/2017#))
    ORDER BY tblAttendance.RehearsalDate DESC , tblInstrument.GroupNbr, tblInstrument.OrderNbr, tblMembers.LastName, tblMembers.FirstName;

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    You don't need the Parameters, the SQL is ok just as it is, with referencing the dates from the form.

    Why do you need to see actual values instead of form references? You will have to create the SQL in VBA for this.

  3. #3
    Rpschwar is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    29
    I don't think you understood my question. The first query with parameters is what I have. Once I enter the parameters I want to see the SQL statement with the parameters inserted like I show in the second sql statement. How can I display that sql statement?

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Like I said, VBA. Where and how do you want to display it?

  5. #5
    Rpschwar is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    29
    This is within a form or report load and I displayed the recordset.sql to show the sql line with parameters but I want to assign the actual sql statement to a variable that I would like to pass to another form without it asking for the parameters again. So I want the sql with the actual values replaced it the sql line.

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The second form can also use the same parameters, if the form "RosterDialogBox" is still open.

    How are you passing it to the second form? One option would be to use a global variable.

    Then:
    variablename="copy and paste from the SQL above ... WHERE (((tblAttendance.RehearsalDate) Between #" & [Forms]![RosterDialogBox]![BeginningDate] & "# And #" & [Forms]![RosterDialogBox]![EndingDate] & "#))........"

    Or if you are inside the form you can use the Me keyword.

  7. #7
    Rpschwar is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    29
    I'm just wanting a simple command the will show the sql statement like the .sql statement gives you. If I can run the recordset, why can't I just see the sql string with the parameters replaced?

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I'm not sure whether you have a saved query in Access or SQL in VBA.
    But as aytee said "You will have to create the SQL in VBA".....

    Quote Originally Posted by Rpschwar View Post
    I'm just wanting a simple command the will show the sql statement like the .sql statement gives you
    There is no "simple command". To show the .SQL statement means you are using VBA to get the SQL from a variable in code or from a saved query. Using VBA, you could create a query def object, set the query def object to the saved query, then get the SQL using the .sql property. But it still wouldn't have the actual dates.

    You could use the RecordSource property to get the SQL of the saved query - you would/could see the reference to the form/control the date values were taken from, but still wouldn't be able to see anything like
    Code:
    WHERE (((tblAttendance.RehearsalDate) Between #1/1/2016# And #1/1/2017#))


    Quote Originally Posted by Rpschwar View Post
    <snip> If I can run the recordset, why can't I just see the sql string with the parameters replaced?
    Because the SQL with the actual date values only exist for a split second. When the query is executed, the SQL is generated, then executed and is gone into the ether. So there is no way to "grab" the SQL that is generated with the dates in the SQL statement for that nano-second that it exists.

    And actually you don't "run the recordset". You execute (run) a SQL query that returns a record set object.


    Options:
    You could hard code the dates in a saved query.... (not very useful IMO)

    You could use VBA to modify the SQL of a saved query to set the where condition and use the saved query as the record source for the form/report. (Programmatically hard coding the dates)

    You could use a saved query without a where clause, then add a where clause in the OpenForm/OpenReport command (Docmd.OpenReport(ReportName, View, FilterName, WhereCondition, WindowMode, OpenArgs) ) ***** I would use this option.


    My $0.02.......

  9. #9
    Rpschwar is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    29
    So I guess the best option is not to use the parameter option but have 2 input boxes for the dates and to just build the sql with those dates.

  10. #10
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Sorry, but yes.

    If you must be able to see the SQL with the dates, that is what you will have to do..

    Good luck with your project.....

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Connect To SQL Server With Passed Credentials
    By jo15765 in forum Programming
    Replies: 3
    Last Post: 05-24-2017, 11:18 AM
  2. Filter not being passed
    By GraeagleBill in forum Reports
    Replies: 3
    Last Post: 11-16-2013, 07:35 PM
  3. How do I use a passed parameter in a Case statement
    By tarenja in forum Programming
    Replies: 7
    Last Post: 08-04-2013, 10:49 PM
  4. Query help. IIF Statement, a few parameters.
    By parkerjallen in forum Queries
    Replies: 2
    Last Post: 11-15-2012, 09:04 AM
  5. Form Data Not being passed onto query
    By fmoses in forum Queries
    Replies: 2
    Last Post: 08-20-2009, 08:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums