0

I am having some trouble with a vba script in Excel which should be reading from a MySql database. The SQL query should only return one record but actually returns an empty resultset. The generated statement works fine when run through phpMyAdmin.

Here is my code:

Function getClientId(emailAddress As String)
    Dim rs As ADODB.Recordset
    Dim sql As String

    ConnectDB

    Set rs = New ADODB.Recordset

    sql = "SELECT client_id FROM clients WHERE email_address = '" & emailAddress & "' LIMIT 1"
    Debug.Print sql
    rs.Open sql, oConn

    Debug.Print rs.RecordCount

    If (rs.RecordCount = -1) Then
        getClientId = Null
    Else
        getClientId = rs(0)
    End If
    rs.Close
End Function

EDIT: My database connect function.

Function ConnectDB()
    On Error GoTo ErrHandler

    Set oConn = New ADODB.Connection
    oConn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _
    "SERVER=localhost;" & _
    "DATABASE=mydb;" & _
    "USER=user;" & _
    "PASSWORD=password;" & _
    "Option=3"

    'Debug.Print oConn

    Exit Function
ErrHandler:
    MsgBox Err.Description, vbCritical, Err.Source
End Function

The ConnectDB function is connecting ok as I am running other scripts with it. If anyone can see what I am doing wrong then any help would be appreciated.

Many thanks in advance.

Garry

6
  • What's the problem, if you don't mind me asking? Commented Jul 21, 2011 at 10:37
  • Youre writing "query should only return one record" but don't say what actually happens - does it return more than one record or does it return empty resultset? In case of later I quess the emailAddress contains value which doesn't exist in the database. Commented Jul 21, 2011 at 10:39
  • Sorry guys, the sql returns an empty resultset where it should return one record with one field, the client id. I have run the SQL through phpMyAdmin and recieved the one record as expected. Commented Jul 21, 2011 at 10:53
  • 1
    @ain SELECT client_id FROM clients WHERE email_address = '[email protected]' LIMIT 1 Commented Jul 21, 2011 at 11:20
  • 2
    Instead of checking the recordcount, check for EOF instead. Record sets don't always report the correct count directly after opening them. Commented Jul 21, 2011 at 15:30

3 Answers 3

2

MyODBC does not properly provide the RecordCount-Attribute.

Re: Problem with RecordCount with ASP & MySQL

rs.recordcount = -1 with myODBC

Re: ADO Connection RecordCount

So, if you really need the RecordCount, set CursorLocation Property to adUseClient. If not, just iterate through the RecordSet like this:

Do While Not rs.EOF
    '...do your magic
    rs.MoveNext
Loop
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks cularis, that was the answer. I changed the check to see if rs(0) contained a vaue greater than zero and it worked fine.
1

Use " (double quote) instead of ' (single quote), because you are querying through the ODBC driver.

Comments

0

This could be problem of driver you have selected. My suggestion is (and I may be wrong) - if you are connecting via ODBC it could have different set of commands.

1 Comment

Thanks for your reply Ondřej, I am using the MySQL ODBC 5.1 Driver. I have added the code to my post above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.