Discussion Locked

Access - set RecordSource from variable?

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a drop-down combo box in a form with 10 values (say A, B, C, etc.). I would like to change the RecordSource property of the form based on what is selected from the combo box. Is this possible?

I tried storing the combo value, which match the names of the tables, to a variable (strSource), and then using the following line of code:

Me!RecordSource = "SELECT * FROM strSource"

But SQL tries to interpret the variable name literally. In other words, it can't find a table named "strSource".

I can do it with an IF or Select statement, but I'm just wondering if there is a way to get the SQL statement here to read from a variable.

thanks!

3 replies

by peyison

You need to use string concatenation to do this:

Me!RecordSource = "Select * From " & strSource

VBA will resolve the string first, and then assign it to RecordSource.

Hope this helps.

This discussion is closed to new replies.