Question : MS Access VBA SQL query with varables - is it possible?

I have an Acess DB that has a few tables and forms and queries. I originally wanted to have a text box on a form take a string and store that in a variable and then pass that into an existing query that had the results bound to a 2 column list box. When I tried to do the query I was trying to put variables into the criteria like this:

left$(MASTN.LASTN,2)= CVK

where CVK was a variable declared in my form. I soon had a good haha at my naive attempts. I knew that Ive seen stored procedures that used 'Delcare @BlahBlah as Datatype'    to use variables but I know think this isnt possible in a save Access query. Is it?

I moved on to use DAO to get a record set with a query and use a SQL string composited with the variables of choice. I wanted to use the recordset as the input of an append query. When I showed this to a freind he said to instead use another table just to store the users typed in data temporarily and contruct my saved query to use the data in that table thus bypassing the inability to use variables in an access query... This did not seem very elegant.

The objective is to have a user type a name in a textbox. The first two letters are used to get some matching data into a listbox from table A  - but the list box is bound to another table B  that takes as its input the contents returned from the first query.  In other words the list box is bound to table B and has records appended to it from table A based on the match (if there are any) with the users input.

I was trying to use an append query but I think I got the order of code events screwed up. help!

Answer : MS Access VBA SQL query with varables - is it possible?

oops i made an error, the function itself cannot a code variable in a query, so it should be:

function fnCVK() as string 'whatever
 fnCVK = gs  'where gs is a global variable set earlier to whatever value from your form etc
end function

the query would then be:
select ....
from ... etc
where left$(MASTN.LASTN,2)  = fnCVK()
Random Solutions  
 
programming4us programming4us