Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Excel VBA query

Posted on 2011-09-20
11
172 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 33

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 33

Expert Comment

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

I tried this and it worked OK.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 33

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 33

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 33

Accepted Solution

by:
Norie earned 500 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 33

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to bypass VBA Project password 5 52
how to reset cell styles? 15 26
Excel Sheet Data Finding 14 17
Using SUMPRODUCT with a Date Range 6 19
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

860 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