Results 1 to 15 of 15
  1. #1
    Chevy757IT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2015
    Posts
    16

    Variables in Queries

    I have a query that counts how man records match a certain criteria and what I want to do is have the query request a user to input a number value (how many people are at the company, which changes week by week) and then calculate that number to get a percentage. I have the count part but I cant figure out how to get query to request user input to input the number from the company into the equation. Is this even possible if not is there another way with forms that this can be done?

    Something like this:

    Expr1: [TotalEnrollmentsCountQRY]![CountOfLast Name, First Name]/[Request user input for a number]



    User inputs 347

    Expr1: [TotalEnrollmentsCountQRY]![CountOfLast Name, First Name]/[347]

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    Why not have a form, bound to your "count" query, with an unbound textbox for the user to enter 347. Then do the calculation in an unbound textbox on the form.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    If you do not want a form, consider InputBox and a TempVar ?
    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

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    Quote Originally Posted by Welshgasman View Post
    If you do not want a form, consider InputBox and a TempVar ?
    How would you assign a value to the TempVar?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Something along the lines of.....

    Code:
    tempvars("HowMany")=inputbox("How many")
    debug.Print tempvars("HowMany")
    12
    I entered 12 to the inputbox prompt
    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

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    Quote Originally Posted by Welshgasman View Post
    Something along the lines of.....

    Code:
    tempvars("HowMany")=inputbox("How many")
    debug.Print tempvars("HowMany")
    12
    I entered 12 to the inputbox prompt
    Sorry, I don't think my question was very clear. What I meant was how do you run that code from a query?
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by Bob Fitz View Post
    Sorry, I don't think my question was very clear. What I meant was how do you run that code from a query?
    You wouldn't Bob, the same as you would not from the form.?

    It is just another method to get the input, without having to create a form.?
    You'd refer to the TempVar in the same way that you would the Form control in the query, just that this method is a little simpler in my mind.?
    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

  8. #8
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Not that I think users should be working with queries but what's wrong with a simple parameter prompt for this? If [Request user input for a number] is not the actual name of a field then I'm missing something because that ought to do exactly what's being asked for - as long as it's understood that the value can only be entered once for all records.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by Micron View Post
    Not that I think users should be working with queries but what's wrong with a simple parameter prompt for this? If [Request user input for a number] is not the actual name of a field then I'm missing something because that ought to do exactly what's being asked for - as long as it's understood that the value can only be entered once for all records.
    Like this?
    Code:
    SELECT TestTransactions.TransactionDate, TestTransactions.Description, TestTransactions.Amount, [Numberof] AS Expr1, [amount]/[expr1] AS Expr2
    FROM TestTransactions;
    I entered 10 for NumberOf
    Attached Thumbnails Attached Thumbnails queries.PNG  
    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

  10. #10
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I had in mind exactly what was posted. [Request user input for a number] strikes me as not being an actual field name so just using that would cause a prompt without giving it to an alias field first. I think that what was posted was never tried - or that is a field name after all, in which case your way should work. Going to wait for OP now.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    Chevy757IT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2015
    Posts
    16
    The [Request user input for a number] was just my way of explaining what I needed not an actual field. The number requested by the user does not need to be stored so I thin the TempVar might be the best option. My ultimate goal is have this percentage displayed on a report.

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    No need for a Tempvar now, you can do as Micron suggested and I tested. ?
    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
    Chevy757IT is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jan 2015
    Posts
    16
    How would I do the parameter prompt?

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Just as I have in that sql?
    Just replace the fieldnames for your own fields.

    Here is the GUI window for that sql
    Attached Thumbnails Attached Thumbnails sql gui.PNG  
    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

  15. #15
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I guess I wasn't clear enough. Trying again: just use [Request user input for a number] as a field. Doesn't matter if it's part of an equation or what. Access will not recognize it as a field so you'll get a prompt. Are you sure that [CountOfLast Name, First Name] shouldn't be [CountOfLastName], [First Name]
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 1
    Last Post: 10-16-2019, 03:45 PM
  2. SQL queries with variables
    By johnseito in forum Queries
    Replies: 5
    Last Post: 09-22-2017, 09:56 PM
  3. Variables In VBA
    By jo15765 in forum Programming
    Replies: 4
    Last Post: 12-15-2016, 08:19 AM
  4. Using Public variables between forms/queries/reports
    By dcrake in forum Sample Databases
    Replies: 2
    Last Post: 12-25-2015, 05:44 PM
  5. Replies: 8
    Last Post: 09-19-2013, 06:22 AM

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