1

I am completely new to Visual Basic. I am working with a MYSQL data base and I want to use VB in excel so I can work with more complex queries. For some reason, when I try to join tables in vb, I get an error message. Can somebody tell me what is wrong with my code.

strSql = "SELECT COUNT(*)FROM `order`" & _
                 "JOIN user ON user.id = order.destination_id" & _
                 "WHERE payment_status = 'pay';"

    rs.Open strSql, oConn, adOpenDynamic, adLockPessimistic

    res = rs.GetRows

    rs.Close

    Range("A1", "A6") = res(0, 0)
0

1 Answer 1

2

your current query will produce this string,

SELECT COUNT(*)FROM `order`JOIN user ON user.id = order.destination_idWHERE payment_status = 'pay';
               ^           ^                                          ^

you lack space during your concatenation, in order to correct that, simply add space before double quote.

strSql = "SELECT COUNT(*) FROM `order`   " & _        
         "JOIN user ON user.id = order.destination_id   " & _
         "WHERE payment_status = 'pay';"
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot John! It worked. But what was wrong with my code. they are practically the same?

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.