Using SQL to get data into a variable

wingforward

Registered User.
Local time
Today, 15:24
Joined
Nov 24, 2009
Messages
27
I'm trying to use a simple Select statement to get data into a variable to set a field and I have two questions. Is this possible? What am I doing wrong?


'Start Code
Dim strCodeID as String
strCode ID = Me.CodeID

Dim SQLCmd as String
SQLCmd = "SELECT Description FROM tblCodes WHERE CodeID = " & strCodeID & ";"

Dim strResult as String
strResult = DoCmd.RunSQL (SQLCmd)

Me.Description = strResult

'End Code
 
Have a look at the DLookup() function. That's what you need.
 
That's a whole lot simpler than what I was trying. Thanks.
 
Yes Dlookup is quick and easy. But, just so you know...

The SQL statement should look like

"SELECT Description FROM tblCodes WHERE CodeID = '" & strCodeID & "';"

Assuming that CodeID is a text field you need to surround your value with single quotes. FYI a date value needs pound signs i.e. #12/21/2012#.

Cheers:cool:
 

Users who are viewing this thread

Back
Top Bottom