Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA query

Posted on 2011-09-20
11
Medium Priority
?
191 Views
Last Modified: 2012-05-12
I am using the attached code to read a table from a MySQL database and would expect it to return all the table data into the worksheet, however it only returns the first column (primary key). Can anyone advise me please?

Excel 2007 & Mysql 5.5 with MySQL ODCB connector ver 5.1
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 5.1 Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=3" 
    Set rs = New ADODB.Recordset
    sqlstr = "SELECT * FROM Clients;"
    rs.Open sqlstr, conn, adOpenOptimistic
    With Worksheets(3).Cells 
        .ClearContents
        .CopyFromRecordset rs
    End With

Open in new window

0
Comment
Question by:drwltd
  • 6
  • 5
11 Comments
 
LVL 35

Expert Comment

by:Norie
ID: 36566455
Is the worksheet completely empty with no hidden rows/columns?

Does the same happen when you try with a new worksheet?
Set wsNew = Worksheets.Add
Set conn = New ADODB.Connection
conn.Open "DRIVER={MySQL ODBC 5.1 Driver}" _
& ";SERVER=" & server_name _
& ";DATABASE=" & database_name _
& ";UID=" & user_id _
& ";PWD=" & password _
& ";OPTION=3" 
    Set rs = New ADODB.Recordset
    sqlstr = "SELECT * FROM Clients;"
    rs.Open sqlstr, conn, adOpenOptimistic

   wsNew.Range("A1").CopyFromRecordSet rs

Open in new window

0
 

Author Comment

by:drwltd
ID: 36566537
Hi imnorie,

No hidden rows . columns and it does just the same on a new worksheet that I have created.
0
 
LVL 35

Expert Comment

by:Norie
ID: 36566557
Have you checked the query/table?

I tried this and it worked OK.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:drwltd
ID: 36566574
I have run the query in mysql and it works fine. I get the same in Excel when querying 2 different tables
0
 
LVL 35

Expert Comment

by:Norie
ID: 36566616
You mean that when you query other tables you only get one field backed?

Try this which loops through the recordset to place the data.
Dim rng As Range


    Set conn = New ADODB.Connection
    conn.Open "DRIVER={MySQL ODBC 5.1 Driver}" _
              & ";SERVER=localhost" _
              & ";DATABASE=excel" _
              & ";UID=root" _
              & ";PWD=narflek" _
              & ";OPTION=3"
    Set rs = New ADODB.Recordset
    sqlstr = "SELECT * FROM invoices;"
    rs.Open sqlstr, conn, adOpenOptimistic


    Set rng = Worksheets(3).Range("A1")

    For I = 0 To rs.Fields.Count - 1

        rng.Offset(, I) = rs.Fields(I).Name
    Next I

    Set rng = rng.Offset(1)

    rs.MoveFirst

    While Not rs.EOF

        For I = 0 To rs.Fields.Count - 1
            rng.Offset(, I) = rs.Fields(I)
        Next I

        Set rng = rng.Offset(1)

        rs.MoveNext
    Wend
    
    rs.Close

    Set rs = Nothing

Open in new window

0
 

Author Comment

by:drwltd
ID: 36566682
rs.fields.Count has a value of zero so again it only displays the first column (Primary key) but the query when run outside of Excel returns results as expected!
0
 
LVL 35

Expert Comment

by:Norie
ID: 36566747
I really don't understant that and I can't find any reference to it.

I did think it might be a driver thing but as far as I can see there haven't been any major changes since last year.

What happens when you go through Data>Get External Data...?
0
 

Author Comment

by:drwltd
ID: 36566788
I made an error and have run your test again, this time it returned the 28 field names and without the .name it returned the data values? That's a big step forward - I am going to do some more testing
0
 
LVL 35

Accepted Solution

by:
Norie earned 2000 total points
ID: 36566829
In the first loop .Name is used to return the field name rather than the value.

In the second loop .Value should really be used, ie rs.Fields(i).Value.

0
 

Author Comment

by:drwltd
ID: 36566901
Yes I didn't realise that you had two loops only the first was visible in the window!
Using your code it works, I can see all the fields. Thanks for your help will award points
0
 
LVL 35

Expert Comment

by:Norie
ID: 36566917
I find the Select All button useful when copying code.:)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question