VBA: Using QueryTables.Add and ODBC to query MySQL. Problem: Not all columns are being returned in excel!

hohead

New Member
Joined
Dec 13, 2010
Messages
2
Alright, I've setup a little macro in excel based on VBA code which runs a query against a MySQL database. This setup has been working great for us without any problems until now.

It seems the SQL query I'm trying to run is breaking QueryTables somehow. This query I'm trying to run is complex and involves a "UNION ALL". This SQL query runs perfectly fine in MySQL Query Browser.

However, when I try and run this query using QueryTables.Add and ODBC, some of the columns are not being returned.

Specifically, I have found that the only columns not being returned are ones which do not have matching UNION "Field Types".

aka: On the top part of my UNION, the first column returned is an INT, and on the bottom of my UNION (the second query), the first column is a VARCHAR. For some reason MySQL Query Browser is perfectly okay showing these results, but QueryTables can't handle it, and drops these columns.

ANY help would be VERY APPRECIATED!!

Here is my VBA code:

Code:
Sub SQLGetNewData()

Dim sConn As String
Dim sSql As String
Dim sDateFrom As String
Dim sDateTo As String
Dim oQt As QueryTable

'Sets the database connection
sConn = "ODBC;DSN=Database_Name;UID=username;PWD=password"

'get query parameters
Worksheets("Data").Activate

'build the query.
' I have not included the original complex SQL query here, and I have created a "VIEW" in MySQL to replicate the query. Because I have created a VIEW, I simply need to query this view just like it's a table.

sSql = "SELECT * from view_revenue "


Worksheets("Query Info").Activate
[B7].Formula = sSql

'clear the data area
Worksheets("Data").Activate
Range("a1").CurrentRegion.ClearContents

'clear existing data connections
For Each oQt In ActiveSheet.QueryTables
    oQt.Delete
Next oQt
If ActiveWorkbook.Connections.Count > 0 Then
    i = 1
    Do While ActiveWorkbook.Connections.Count > 0
        ActiveWorkbook.Connections.Item(i).Delete
        i = i - 1
    Loop
End If



'get new data
With ActiveSheet.QueryTables.Add(Connection:=sConn, _
        Destination:=Range("A1"), Sql:=sSql)
    .Refresh BackgroundQuery:=False
    .RefreshPeriod = 0
    .RefreshOnFileOpen = False
    
End With


Worksheets("Data").Activate

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
It's probably the ODBC driver that doesn't support this kind of UNION. I wasn't aware that MySQL was different - it's actually MySQL that's behaving "unusually" here as the normal SQL standard for UNION queries is that the fields must have matching (or compatible) datatypes.

It is possible that your MySQL native query is implicitly converting the INT's to Character datatypes.

Possibly you could re-write your query to do the same (explicitly):
Code:
SELECT CAST(FieldName AS VARCHAR) FROM TABLE1
UNION ALL
SELECT FieldName FROM TABLE2
I do not know the actual syntax you would use to convert INT to Character so you'll have to play with that. Cast() is a SQL-92 Standard and might work with ODBC. But ODBC might not recognize platform-dependent MySQL functions so you may need to play around with it.

Another possibility might be to try to find if there is a better driver out there somewhere for you (I really don't know).

ξ
 
Upvote 0
Thank you for your reply.

Yeah, I have already tried using CAST as a work-around. It does work, but I was hoping for a better solution.

I tried updating my ODBC driver, but this didn't help. Athough I did find that turning on this option allowed the columns to be displayed, just without any values:

bSTwR.png


Thanks again for your reply! I guess I'll use CAST as a solution.
bSTwR
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top