Results 1 to 11 of 11
  1. #1
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65

    Defining SQL parameter for Access Query

    I have a fairly complex query where the parameter [Starting Year] is a user input and used in a WHERE statement. I'd like for the [Starting Year] to be picked up from a form and have this statement in the sql:

    Code:
    WHERE ((([Starting Year])=[forms]![frm_startYear]![txtYear] And ([Starting Year]) Between Year([qry_ProjectTracker].[DATE_START]) And Year(IIf(IsDate([qry_ProjectTracker].[DATE_END])
    The query is still prompting for [Starting Year], even though the form is open and available. Is there a syntax that I'm missing here? This has worked for me in the past, but my WHERE statements have never been so complicated.

    I've also tried putting the call for [Starting Year] ([Starting Year])=[forms]![frm_startYear]![txtYear]) at the beginning of the code, before the SELECT function, but this hasn't worked either.



    I'm going to include the whole code down here, in case that helps. Thank you for any hints and ideas you can give me....

    Code:
    PARAMETERS [Starting Year] Short;
    SELECT qry_ProjectTracker.PROJECT_NAME, qry_ProjectTracker.DATE_START, qry_ProjectTracker.DATE_END, qry_ProjectTracker.OBJ_HEADING, qry_ProjectTracker.OBJ_TXT, (IIf([Starting Year]+1>=Year(qry_ProjectTracker.DATE_START) And [Starting Year]+1<=Year(qry_ProjectTracker.DATE_START)+4,IIf([Avg2YR]>2,"> 90% complete",IIf([Avg2YR]=2,"66-90% complete",IIf([Avg2YR]<2,"< 65% complete"))))) AS 2YearProjTxt, (IIf([Starting Year]+2>=Year(qry_ProjectTracker.DATE_START) And [Starting Year]+2<=Year(qry_ProjectTracker.DATE_START)+4,IIf([Avg3YR]>2,"> 90% complete",IIf([Avg3YR]=2,"66-90% complete",IIf([Avg3YR]<2,"< 65% complete"))))) AS 3YearProjTxt, (IIf([Starting Year]+3>=Year(qry_ProjectTracker.DATE_START) And [Starting Year]+3<=Year(qry_ProjectTracker.DATE_START)+4,IIf([Avg4YR]>2,"> 90% complete",IIf([Avg4YR]=2,"66-90% complete",IIf([Avg4YR]<2,"< 65% complete"))))) AS 4YearProjTxt, (IIf([Starting Year]+4>=Year(qry_ProjectTracker.DATE_START) And [Starting Year]+4<=Year(qry_ProjectTracker.DATE_START)+4,IIf([Avg5YR]>2,"> 90% complete",IIf([Avg5YR]=2,"66-90% complete",IIf([Avg5YR]<2,"< 65% complete"))))) AS 5YearProjTxt, qry_ProjectTracker.UNIQUE_OBJ_LINK
    FROM tbl_perf_measure, qry_ProjectTracker
    WHERE ((([Starting Year])=[forms]![frm_startYear]![txtYear] And ([Starting Year]) Between Year([qry_ProjectTracker].[DATE_START]) And Year(IIf(IsDate([qry_ProjectTracker].[DATE_END]),[qry_ProjectTracker].[DATE_END],Now()))) AND (([Starting Year]+4) Between Year([qry_ProjectTracker].[DATE_START]) And Year(IIf(IsDate([qry_ProjectTracker].[DATE_END]),[qry_ProjectTracker].[DATE_END],Now()))))
    GROUP BY qry_ProjectTracker.PROJECT_NAME, qry_ProjectTracker.DATE_START, qry_ProjectTracker.DATE_END, qry_ProjectTracker.OBJ_HEADING, qry_ProjectTracker.OBJ_TXT, (IIf([Starting Year]+1>=Year(qry_ProjectTracker.DATE_START) And [Starting Year]+1<=Year(qry_ProjectTracker.DATE_START)+4,IIf([Avg2YR]>2,"> 90% complete",IIf([Avg2YR]=2,"66-90% complete",IIf([Avg2YR]<2,"< 65% complete"))))), (IIf([Starting Year]+2>=Year(qry_ProjectTracker.DATE_START) And [Starting Year]+2<=Year(qry_ProjectTracker.DATE_START)+4,IIf([Avg3YR]>2,"> 90% complete",IIf([Avg3YR]=2,"66-90% complete",IIf([Avg3YR]<2,"< 65% complete"))))), (IIf([Starting Year]+3>=Year(qry_ProjectTracker.DATE_START) And [Starting Year]+3<=Year(qry_ProjectTracker.DATE_START)+4,IIf([Avg4YR]>2,"> 90% complete",IIf([Avg4YR]=2,"66-90% complete",IIf([Avg4YR]<2,"< 65% complete"))))), (IIf([Starting Year]+4>=Year(qry_ProjectTracker.DATE_START) And [Starting Year]+4<=Year(qry_ProjectTracker.DATE_START)+4,IIf([Avg5YR]>2,"> 90% complete",IIf([Avg5YR]=2,"66-90% complete",IIf([Avg5YR]<2,"< 65% complete"))))), qry_ProjectTracker.UNIQUE_OBJ_LINK, (IIf([Starting Year]>=Year(qry_ProjectTracker.DATE_START) And [Starting Year]<=Year(qry_ProjectTracker.DATE_START)+4,IIf([Avg1YR]>2,"> 90% complete",IIf([Avg1YR]=2,"66-90% complete",IIf([Avg1YR]<2,"< 65% complete")))));

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Then you'd want the form reference in PARAMETERS, not the old bracketed criteria. You'd also want:

    FieldName = [forms]![frm_startYear]![txtYear]


    where FieldName is the field in the data you want the criteria on.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    I've tried setting this up in the beginning, but I don't think I have the correct syntax.

    Here are a couple options I tried:

    PARAMETERS [Starting Year] Short AND [Starting Year]=[forms]![frm_StartYear]![txtYear];

    or PARAMETERS [Starting Year] Short;
    Set [Starting Year]=[forms]![frm_StartYear]![txtYear];


    Thanks!

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You don't want [Starting Year] in there at all, if I'm understanding correctly. Wherever you had that in your original query, you'd replace it with the form reference. Then instead of prompting for the value it will get it from the form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    [Starting Year] was never in another query or table. This is the first place this parameter is used (or created).

    The original data uses a project start date and year 1, year 2, etc. thru year 5. The user can say "I want to look at all projects going on in 2003" (2003 = example [Starting Year]) and the query will do the math to figure out if any of the projects were going on during this time and spit out results for 2003.

    Am I making any sense? Sorry that this is so complicated.

  6. #6
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Oh, it's like a variable you're trying to set? I don't think you'll be able to do that. Try this type of thing:

    [forms]![frm_StartYear]![txtYear] Between Year([qry_ProjectTracker].[DATE_START]) And Year(IIf(IsDate([qry_ProjectTracker].[DATE_END]),[qry_ProjectTracker].[DATE_END],Now()))
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    That will take me a bit (it's referenced 10-15 times spottily in the code).

    Is there a way I could declare it as a variable in the code? Surely sql will allow that. It knows to prompt the user for it when it runs the query...

  8. #8
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It prompts the user because it's bracketed and not a field in the table. I know of no way to declare it as a variable in SQL. Changing it shouldn't be too hard though. Copy the SQL into Wordpad or something, do a Find/Replace, and copy it back.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    Hi pbaldy,
    I made a form for the query to retrieve the Starting Year variable, and reference it in the SQL as [Forms]![frm_StartYear]![txtYear], but it doesn't pick up any of the values. I suspect this is because it is picking up the year as a text string instead of a numeric value. Is there anyway I can declare it as a number or a year in the form or in the SQL statement?

  10. #10
    Ashe is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2010
    Location
    Blacksburg VA
    Posts
    65
    I'm terribly sorry. It does work, I'm just having some trouble updating the value like I should.

    Thank you for your help!

  11. #11
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help! You have to remember to tab off the textbox (or hit enter) during testing. Otherwise the value isn't available to the query. I still have a flat spot on my head from that one.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Parameter Query....
    By Casper2012 in forum Queries
    Replies: 5
    Last Post: 08-16-2011, 11:06 AM
  2. progamatically defining a likned table
    By ajinc in forum Programming
    Replies: 1
    Last Post: 06-02-2011, 06:16 PM
  3. Replies: 3
    Last Post: 04-27-2011, 06:34 PM
  4. defining and setting value of a varible?
    By meet_binay in forum Queries
    Replies: 5
    Last Post: 01-17-2011, 03:50 PM
  5. defining Criteria between two dates
    By tamu46 in forum Queries
    Replies: 1
    Last Post: 12-04-2010, 11:58 PM

Tags for this Thread

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