Results 1 to 4 of 4

Thread: Returning values with SQL

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 1999
    Posts
    2

    Post

    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?

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Try this:

    Code:
    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
    [email protected]
    [email protected]



  3. #3
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105

    Post

    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

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

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 1999
    Posts
    2

    Post

    Thanks a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width