VBA parameter query syntax error

KG Old Wolf

Board Regular
Joined
Sep 20, 2009
Messages
65
All,

I need a fresh set of eyes... I just can't see the issue with the first "with" line of code (connection string). I am attempting to write my first "parameter-driven" query in VBA. Thanks for any suggestions....

Rich (BB code):
Sub qry_Divsions()
'
'
'   Parameter driven query to extract specific client Division locations
'
Dim qry_Practice_Abbr As String
qry_Practice_Abbr = Range("prm_Practice_Abbr")
'
'
With ActiveSheet.Query.Tables.Add(Connection:=Array( _
"ODBC;DSN=CMS2;Description=CMS2; _
APP=Microsoft Office 2010;DATABASE=cms_data; _
Trusted_Connection=Yes), Destination:=Range("$A$1"))
        '
        .CommandText = Array( _
        "SELECT Division.DivisionAbbr, Division.FeeSchedUpdate, Division.projDate, Practice.PracticeAbbr, Practice.PracticeName, Practice.FiscalYearEnd" & Chr(13) & "" & Chr(10) & "FROM cms_data.dbo.Division Division, cms_data.dbo.Practi" _
        , "ce Practice" & Chr(13) & "" & Chr(10) & "WHERE Division.PracticeID = qry_Practice_Abbr")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = False
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_Query_from_CMS2"
        .Refresh BackgroundQuery:=False
        '
    End With
'
'
End Sub
 
Last edited by a moderator:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
It should be QueryTables.Add rather than Query.Tables.Add
 
Upvote 0
What is the error now?
 
Upvote 0
You have a couple of line continuation characters in the middle of your connection string and unmatches quotes.

Try:-
[CODE]With ActiveSheet.QueryTables.Add(Connection:=Array( _
"ODBC;DSN=CMS2;Description=CMS2;" _
& "APP=Microsoft Office 2010;DATABASE=cms_data;" _
& "Trusted_Connection=Yes"), Destination:=Range("$A$1"))
[/CODE]


I think that's right... dashing out... catch you later...
 
Upvote 0
Rory....

The error remains in the same line of code... the block from the first WITH

With ActiveSheet.QueryTables.Add(Connection:=Array( _
"ODBC;DSN=CMS2;Description=CMS2;APP=Microsoft Office 2010;DATABASE=cms_data; _
Trusted_Connection=Yes"),Destination:=Range("$A$1"))


Much appreciated,
Ken
 
Upvote 0
Ruddles' suggestion worked. I now have an issue with the way I am trying to introduce the parameter but that is something I will research further before troubling anyone further.

thanks for your time and help.

Much appreciated,
Ken
 
Upvote 0

Forum statistics

Threads
1,217,415
Messages
6,136,506
Members
450,017
Latest member
Alvi

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