PDA

Click to See Complete Forum and Search --> : Returning values with SQL


Rik
Sep 29th, 1999, 08:56 PM
Hi there. Does anyone know how to execute an SQL select statement on a MDB database, and return a value to a VB variable?

For example, If I was to execute 'select UserName from users where UserId = 123' on the database, how could I get the result into a VB variable?

Serge
Sep 29th, 1999, 10:45 PM
Try this:


Dim db As Database
Dim rs As Recordset
Dim strUserName As String

Set db = Workspaces(0).OpenDatbase("C:\MyDB.mdb")
Set rs = db.OpenRecordset("Select UserName From Users Where UserID = 123", dbOpenSnapshot)

If rs.EOF And rs.BOF Then
MsgBox "User doesn't exist"
Else
strUser = rs(0)
End If



strUser now holds the name of the user.

Regards,

------------------

Serge

Software Developer
Serge_Dymkov@vertexinc.com
Access8484@aol.com

JHausmann
Sep 30th, 1999, 02:26 AM
Hmm, how interesting. That indicates that the default for recordsets is "fields".

I always use:

strUser = rs.fields(0)

If you don't care about the returns from the recordset select, you can do the following conditional in place of Serge's


if isnull(rs.fields(0)) then
msgbox "user doesn't exist"
else
strUser = rs.fields(0)
end if

Rik
Sep 30th, 1999, 06:46 PM
Thanks a lot!