Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get SQL to load result of SELECT into a variable

Status
Not open for further replies.

FeS2

Technical User
Aug 16, 2002
82
US
I am having one heck of a time to get the result of a sql query to load it's results into a string variable. I have tried the follow but get a Compile Error that says Expected Function or Variable, then shows my code with .runsql highlighted in blue. The strSQL will only ever result in a 7 digit phone number, w/ no dashes.

strSQL = "SELECT [AM Completions].[phone] WHERE [AM Completions].[phone] = Forms!Faxination1!Phone;"
strTest = (DoCmd.RunSQL(strSQL))
 
Have a look to the DLookUp function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
The RunSQL method is for action or data-definition queries, i.e. INSERT INTO, CREATE TABLE, DELETE, etc., not SELECT queries that will return a recordset. You *can* do it by opening a recordset, but PHV's suggestion is on target, DLookUp is perfect for this sort of thing.

Ken S.
 
How are ya FeS2 . . . . .

In parallel with [blue]PHV[/blue], try this:
Code:
[blue]   Dim strTest As String, Criteria As String
   
   Criteria = "[phone] = '" & Forms!Faxination1!Phone & "'"
   strTest = DLookup("[phone]", "[AM Completions]", Criteria)[/blue]

Calvin.gif
See Ya! . . . . . .
 
DLookup works great. But I am running into trouble now in an if statement. I started a question about if I should use an If or IIF. thread705-1066072 I'll ask the rest of the question there since it involves more of the If than DLookup. Thanks for everyones help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top