Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    pledbetter is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93

    Passing values to query parameters


    Looks like my earlier attempt to post failed. Let's try again. please refer to attachments. I have a query with Date formatted parameters. I created a form with a button with an "OnClick" action that runs the following VBA code. My attempts to use DoComd.SetParameter and DoCmd.OpenQuery apparently does no pass the parameters as expected. When I click on the command button I still get prompts for my parameters. Here is the OnClick code:


    Private Sub btnGetLastWeeksData_Click()

    Dim BG As Date
    Dim ED As Date


    Select Case Weekday(Now())
    Case 1
    BG = Date - 6
    ED = Date - 2

    Case 2
    BG = Date - 7
    ED = Date - 3

    Case 3
    BG = Date - 8
    ED = Date - 4

    Case 4
    BG = Date - 9
    ED = Date - 5

    Case 5
    BG = Date - 10
    ED = Date - 6

    Case 6
    BG = Date - 11
    ED = Date - 7

    Case 7
    BG = Date - 12
    ED = Date - 8

    End Select

    DoCmd.SetParameter "BeginDate", BG
    DoCmd.SetParameter "EndDate", ED
    DoCmd.OpenQuery "qryCustomDates"




    End Sub
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Have you actually defined parameters in the query?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    pledbetter is offline Advanced Beginner
    Windows 7 64bit Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Yes. It's in my attached document labeled Query.doc And the creation is evident by the prompt from those very same parameters when I click the button

  4. #4
    pledbetter is offline Advanced Beginner
    Windows 7 64bit Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Here are the query parameter definitions
    Attached Files Attached Files

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Not wrong, but all that Select Case is probably replaceable with
    BG = Date - (Weekday(Now())+5)
    ED = Date - (Weekday(Now())+1

    I've never tried to deal with parameters in that fashion. Prefer to Dim qdef as Parameter and define them in code, or write the sql and pass them as variables even if I have to do something like DLookup first. I find variables simpler.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Quote Originally Posted by Micron View Post
    Not wrong, but all that Select Case is probably replaceable with
    BG = Date - (Weekday(Now())+5)
    ED = Date - (Weekday(Now())+1
    Sweet! That's a nice algorithm! I don't understand why it's so difficult to pass a value, properly formatted, to a parameter of an existing query. I'm getting back into VBA after a long hiatus. I certainly have done it before. I don't understand why this code just blinks at me and runs the query but does not pass the parameters.

  7. #7
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I'm sure that given the wealth of talent here, it will get figured out, but I have to take a break.
    Got stitches in my R ring finger today because I was careless with the table saw and the meds are not doing a whole lot for me.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    Code:
    Dim BG As DateDim ED As Date
    Dim str As String
    BG = Date - 5 - Weekday(Now())
    ED = Date - 1 - Weekday(Now())
    BG = #1/19/2011#
    ED = #3/19/2011#
    str = "#" & Format(BG, "mm/dd/yyyy") & "#"
    DoCmd.SetParameter "bgp", str
    str = "#" & Format(ED, "mm/dd/yyyy") & "#"
    DoCmd.SetParameter "edp", str
    Debug.Print str
    DoCmd.OpenQuery "transactions query"
    PARAMETERS [bgp] DateTime, [edp] DateTime;
    SELECT [bgp], [edp], Transactions.[TransactionID], Transactions.[TransactionDate], Transactions.[TransactionAmount], Transactions.[CustomerID]
    FROM Transactions
    WHERE Transactions.TransactionDate Between bgp And edp;
    this query works

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Quote Originally Posted by Micron View Post
    I'm sure that given the wealth of talent here, it will get figured out, but I have to take a break.
    Got stitches in my R ring finger today because I was careless with the table saw and the meds are not doing a whole lot for me.
    Ouch! Finally finishing my guitar after all these years?
    Here's to a speedy recovery
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    THis worked for me, but did not initially?
    Code:
    Sub TestParameters()
    Dim BD As Date, ED As Date
    
    
    BD = DateAdd("yyyy", -9, Date)
    ED = DateAdd("yyyy", -1, Date)
    
    
    DoCmd.SetParameter "BeginDate", BD
    DoCmd.SetParameter "EndDate", ED
    DoCmd.OpenQuery "qryParameters"
    End Sub
    Code:
    PARAMETERS BD DateTime, ED DateTime;
    SELECT TestTransactions.ID, TestTransactions.TransactionDate
    FROM TestTransactions
    WHERE (((TestTransactions.TransactionDate) Between [BD] And [ED]));
    and it should not have, as I had the paremeter names wrong?
    I changed the names and got the prompt. Sometimes I do and sometimes I do not, and it runs fine?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    Quote Originally Posted by ano View Post
    Code:
    Dim BG As DateDim ED As Date
    Dim str As String
    BG = Date - 5 - Weekday(Now())
    ED = Date - 1 - Weekday(Now())
    BG = #1/19/2011#
    ED = #3/19/2011#
    str = "#" & Format(BG, "mm/dd/yyyy") & "#"
    DoCmd.SetParameter "bgp", str
    str = "#" & Format(ED, "mm/dd/yyyy") & "#"
    DoCmd.SetParameter "edp", str
    Debug.Print str
    DoCmd.OpenQuery "transactions query"
    PARAMETERS [bgp] DateTime, [edp] DateTime;
    SELECT [bgp], [edp], Transactions.[TransactionID], Transactions.[TransactionDate], Transactions.[TransactionAmount], Transactions.[CustomerID]
    FROM Transactions
    WHERE Transactions.TransactionDate Between bgp And edp;
    this query works

    Hey good morning! OK I feel I need to give more information on my project here. I was originally attempting to focus on the syntax of passing a Date formatted object to a query parameter which seems to be ignored, for some reason. But let me expand:

    I want to create a query that pulls sales info from "Last Week". I had to calculate the Beginning Date and the End Date, hence the weird Case statement. Micron's cool algorithm will definitely replace that. So the job of figuring out when Monday and Friday of "Last Week" is complete. The query in question has two defined, Date formatted parameters: BeginDate and EndDate. I also created a form that (for now) has a single Command Button. The OnClick Event has the referenced code. I work in a very distracting environment so my variable for BeginDate is "BG" instead of "BD". So my two variables represent "Begin Date" (BG) and "End Date" (ED). So now my questions about your code:

    Why the static assignment assignments for BG & ED? Why are you passing strings to a Date formatted parameter? Is that what you do regardless of how the parameter is formatted?

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I have just lost my last post???

    This works.

    Public Const strcJetDate = "\#mm\/dd\/yyyy\#" 'Needed for dates in queries as Access expects USA format.

    Code:
    Sub TestParameters()
    Dim BD As Date, ED As Date
    
    
    BD = DateAdd("yyyy", -9, Date)
    ED = DateAdd("yyyy", -3, Date)
    
    
    
    
    DoCmd.SetParameter "BeginDate", Format(BD, strcJetDate)
    DoCmd.SetParameter "EndDate", Format(ED, strcJetDate)
    
    
    DoCmd.OpenQuery "qryParameters"
    End Sub
    I have never used SetParameters, so I had to experiment.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    a)Why the static assignment assignments for BG & ED?b) Why are you passing strings to a Date formatted parameter? c)Is that what you do regardless of how the parameter is formatted?
    a)static to adjust for my database test
    b)the date field did not transfer,after string with the date # convention the query worked
    c)not sure what u mean here , u use date variable thats ok and within vba no problems, but seems transfer to sql need string. dont know of other externe transfers have same issue

    sorry for the confusion : i used an old database to check my test so i first replace ur select case into a single line for each date and then i overwrite to adjust to my dates,
    without the string i did not get output but after string i had result. welshgasman do the same with strcjetdate

    if u only want same week as today u not need parameters
    WHERE datepart("ww",Transactions.TransactionDate) = datepart("ww",now()); ' is weeknr
    i change my where clause to this and added a record in the table and got the new record. meaning no parameters use
    Last edited by ano; 08-30-2024 at 07:59 AM. Reason: answer questions

  14. #14
    pledbetter is offline Advanced Beginner
    Windows 11 Access 2019
    Join Date
    Jan 2010
    Location
    Martinsville, IN
    Posts
    93
    OK, another dumb question. how do I mark this as "Solved"?

  15. #15
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Look for "thread tools" near the top right of the page
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Passing parameters to union query
    By nightowl128 in forum Programming
    Replies: 1
    Last Post: 05-02-2017, 12:58 PM
  2. passing parameters to a query
    By PatrickCairns in forum Programming
    Replies: 1
    Last Post: 12-11-2012, 08:47 PM
  3. Passing parameters into vb using access query
    By markyboy171 in forum Programming
    Replies: 4
    Last Post: 06-15-2012, 05:46 AM
  4. Replies: 37
    Last Post: 09-15-2011, 11:57 AM
  5. Passing parameters to a query
    By stephenaa5 in forum Queries
    Replies: 3
    Last Post: 09-10-2010, 05:32 PM

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