r/MicrosoftAccess • • Apr 19 '20

use a variable as an input

hello all, each monday i run 6 separate queries using a date from the previous week, so on 4-20, i'll have to modify 6 queries with this input of >=20200413 i'd like to put this value in a table and just modify all queries to pull this value and run them. any way i can do this? i tried creating a blank table with this value and reference in my date field in my queries and it didn't work. i'm sure im doing something wrong though ty

0 Upvotes

6 comments sorted by

1

u/[deleted] Apr 20 '20

Try var = input('message') and then put the variable 'var' in the query string

1

u/nickcasa Apr 20 '20

not sure i follow you. below is one of my queries, how would i substitute your code for my date field?

SELECT IWSE4S8_SCTRN.STCOMP, IWSE4S8_SCTRN.STDATE, IWSE4S8_SCTRN.STTCKT

FROM IWSE4S8_SCTRN

WHERE (((IWSE4S8_SCTRN.STCOMP)="12") AND ((IWSE4S8_SCTRN.STDATE)>=20200413));

this is what i was trying to do and it's not working.

SELECT IWSE4S8_SCTRN.STCOMP, IWSE4S8_SCTRN.STDATE, IWSE4S8_SCTRN.STTCKT

FROM IWSE4S8_SCTRN

WHERE (((IWSE4S8_SCTRN.STCOMP)="12") AND ((IWSE4S8_SCTRN.STDATE)=[test19]![date]));

test19 is my table with a value of ">=20200413" in a field called "date"

1

u/[deleted] Apr 20 '20

I feel obliged to tell you that the query you are using makes your susceptible to SQL injections however to accomplish what you want the easiest way would be to use the input I gave you and replace the part of the query you want to change to the input variable. For example if you want the select part to be a variable change to "SELECT var FROM blah blah blah where blah blah" and when input shows up the user can type IWESE4SB4. If you want the from or where the user can type the clause they want etc... Another option would be to make different variables and have the user select one. For example var_1 = IWESE4SB4 and the user would type var_1 in the input and then the variable on the SQL string would be IWESE4SB4...

1

u/nickcasa Apr 20 '20

i would like the value to pull from a table as i have a bunch of these i run each week. i just want to change the value in a temp table and have all queries pull that value. is this possible?

1

u/[deleted] Apr 20 '20

You could make a table with a column called 'variables' or whatever names you like and have the values listed with a primary key and create a SQL statement with a conditional that states "If condition x = True: var = a query for the variable containing the primary key you want" elif etc...

1

u/[deleted] Apr 20 '20

I have much better ways of accomplishing what you want my friend; however giving you the direct answer would violate the forum's rules. I'm a bit tipsy right now, but if you send me a private message with what you're trying to accomplish exactly, I can look at it in the morning and give you a better answer.