Updating ODBC Connection SQL Command via VBA

CsG++

New Member
Joined
Jul 8, 2013
Messages
6
I am trying to build a macro to help update my ODBC connection. I ran the macro recorder while I did it manually, but the code gives a

Run-time error '1004':
Application-defined or object-defined error

Below is a simplified snippet of the code:

Code:
    With ActiveWorkbook.Connections("ConnectionName"). _
        ODBCConnection
        .BackgroundQuery = False
        .CommandText = "SELECT * FROM C:\dir\file.csv"
        .CommandType = xlCmdSql
        .Connection = Array(Array( _
        "ODBC;DBQ=C:\dir;DefaultDir=C:\dir" _
        ), Array( _
        " ;Driver={Microsoft Text Driver (*.txt; *.csv)};DriverId=27;Extensions=None,asc,csv,tab,txt;FIL=text;MaxBufferS" _
        ), Array( _
        "ize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;" _
        ))
        .RefreshOnFileOpen = False
        .SavePassword = False
        .SourceConnectionFile = ""
        .ServerCredentialsMethod = xlCredentialsMethodIntegrated
        .AlwaysUseConnectionFile = False
    End With
    With ActiveWorkbook.Connections("ConnectionName")
        .Name = "ConnectionName"
        .Description = ""
    End With
    ActiveWorkbook.Connections("ConnectionName").Refresh
That's the gist of what the macro recorder gave me - except my query (<code>.CommandText</code>) is quite a bit longer. Also, the file.csv actually has a date component to it, which I am establishing as a variable, set by an InputBox at the beginning of the macro.

The error comes about at the SQL section (<code>.CommandText</code>). This is before I even make any changes from the recorder's output.

Any help is much appreciated.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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