ODBC Connection Wrong Field Order

bbranco

New Member
Joined
Aug 19, 2017
Messages
3
I am working on a simple project, so I thought, to pull data from and ODBC data source into Excel.

I have been a programmer for too many years to mention, but new with VBA and Excel.

I have this query, and the columns are not in this order

SELECT product.num As titem, product.description, SPACE(1) as description1, product.price, SPACE(1) AS filler1 FROM `fishbowl_demo`.`product`

descriptiondescription1pricefiller1titem

<tbody>
</tbody>

That is the order they are appearing in.

I also have this in the VBA, to save the sheet and it does work, but that sheet also has the wrong order.

Option Explicit


Sub ProcessData()
Dim MainWBook As String
Dim NewWB As Workbook
Dim Ash As Worksheet
Dim attachment1 As String
Dim LastRowInSheet As Long


With Application
.EnableEvents = False
.ScreenUpdating = False
.PreserveColumnInfo = True
.PreserveFormatting = True
End With

Application.EnableCancelKey = xlDisabled
MainWBook = ThisWorkbook.Name


Workbooks(MainWBook).Sheets("Data").Range("A1").ListObject.QueryTable.Refresh BackgroundQuery:=False

Set NewWB = Workbooks.Add(xlWBATWorksheet)


Workbooks(MainWBook).Sheets("Data").Copy After:=Workbooks(NewWB.Name).Sheets(1)

Workbooks(NewWB.Name).Sheets("Data").ListObjects("Table_Query_from_SQL_TEMP").Unlink

attachment1 = "C:\Users\bbranco\Documents\Parfum1-Fishbowl\CA-Items\items-" & Format(Date, "MM-DD-YYYY") & ".xlsx"

Application.DisplayAlerts = False
Workbooks(NewWB.Name).SaveAs attachment1, xlOpenXMLWorkbook
Workbooks(NewWB.Name).Close
End Sub



Any help would be greatly appreciated!!!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
if you move the fields into the order you want, on the worksheet, that might solve it

IIRC, once the query is in the worksheet, the field order in the SQL makes no difference.
The field order in the SQL should be reflected in the worksheet when creating the query table.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,688
Members
448,978
Latest member
rrauni

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