dynamic SQL using current RecordSource

JoshuaAnthony

Registered User.
Local time
Today, 21:15
Joined
Dec 18, 2003
Messages
68
Hi,

The following works for me:

Code:
strSQL = "SELECT * FROM [" & Me.RecordSource & "]"

but when I try to select certain fields from the recordSource, it doesn't seem to work. This is one of my failed attempts:

Code:
strSQL = "SELECT [" & Me.RecordSource.[ID Number] & "],[" & Me.RecordSource.[Field1] & "]  FROM [" & Me.RecordSource & "]"

Any suggestions to set me in the right direction?

Thanks,

Joshua
 
Code:
Dim strSource As String
strSource = Me.RecordSource

strSQL = "SELECT [" & strSource & "].[ID Number], [" & strSource & "].[Field1] FROM [" & strSource & "];"

I made the variable to store the reocordsource rather than navigate the form's class three times to get the recordsource.
 
Last edited:
Actually, if the recordsource is static then it would be even better to declare it as a Constant - it improves speed once the code is compiled.

Code:
Const strSource = "RecordSourceName"

strSQL = "SELECT [" & strSource & "].[ID Number], [" & strSource & "].[Field1] FROM [" & strSource & "];"
 
BTW I also found that I don't even need to put in the record source! i.e.:

Code:
strSQL = "SELECT [ID Number], [Field1] FROM [" & strSource & "];"
 
JoshuaAnthony said:
BTW I also found that I don't even need to put in the record source!

You do, however, if you are selecting from more than one table. ;)
 

Users who are viewing this thread

Back
Top Bottom