Use parameter in the query

Hi

Is it possible to use following query with parameter for the number.

SELECT TOP 5 Table1.[Section No], Table1.[Field Value]
FROM Table1;

Can I use instead of 5 expression [Enter Parameter Value].

I need query which runs top records to be dynamic
Database-2-1.accdb
maximyshkaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jerryb30Commented:
You might dynamically change the query SQL in vba with an InputBox.
0
Dale FyeCommented:
No.

However, you could use a form to select how many records you want the query to return, then modify the query, or build the query on the fly in the Click event of a button on your form.  That is normally the way I do this when I want to select a variable number of records at a time.
0
maximyshkaAuthor Commented:
How can I do it?  Would you please show with the provided db sample.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Dale FyeCommented:
take a look at form1 and the code associated with the Open Query button
Dynamic-Top-5.accdb
0
maximyshkaAuthor Commented:
Thanks a lot, fyed

If I need to input percentage value (instead of number) into text box do I need to change vba code?

Or I should not use percentage value with the form?
0
Dale FyeCommented:
Yes, if you want to use a percentage rather then an actual # of records, you should use something like

strSQL = "SELECT TOP " & me.txt_Percent & "PERCENT Table1.* FROM Table1"

If you want to configure it so that the user can select "Percent or Number", you could add a combo box following the text box with value list and Rowsource values of 'Percent' and 'Number'

Then the SQL might look something like:

     strSQL = "SELECT TOP " & me.txt_Number _
                 & IIF(me.cboPctOrNum = "Percent", " PERCENT ", " ") _
                 & "Table1.* FROM Table1"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maximyshkaAuthor Commented:
Thanks a lot
0
Dale FyeCommented:
Glad I could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.