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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run simple select statements in VBA (MS Access)

Status
Not open for further replies.

rk0000

Technical User
Mar 15, 2002
33
IN
I am very new to VBA and MS Access. I need run a select statement and get the data into some string.

SQL = "Select CustomerName from Customers"

SQL = SQL & " WHERE CustomerID = '" & StrCustID & "' "

I need to run this query and get the value of CustomerName in to some string say (strName). How can i do that?

Thanks
 
Hi
This looks very like a case for Dlookup:
DLookup Function Examples
faq705-4978
[ponder]
 
Remember that DLookUp() - infact DAnything() is a very slow process.

For JUST ONE data value it is marginally faster than the alternative, however, if you need more than one field from the same domain then open a recordset

Code:
Dim db as Database
Set db = Currentdb
Dim rst As DAO.Recordset

Set rst = Db.OpenRecordSet("SELECT Field1, Field2, Field3 " _
                         & "FROM tblName " _
                         & "WHERE Field4 = 'foo'")

txtControl1 = rst!Field1
txtControl2 = rst!Field2
txtControl3 = rst!Field3

rst.Close
Set rst = Nothing
set db = Nothing


'ope-that-'elps.



G LS
spsinkNOJUNK@yahoo.co.uk
Remove the NOJUNK to use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top