Dynamically adding columns to MS Query in Excel

cowinsm

New Member
Joined
Oct 3, 2008
Messages
49
I am trying to pull data from a database. I have a really large table on SQL Server that I query using MS Query. I would like to pull from columns based on a list from excel that changes based on another field.

Here is an example. The table in SQL Server contains columns A through Z. I would have an input range of the columns I would pull down from SQL Server and only these columns.

For example cells A1:A4 in Excel would have the following (the range for the pull would be A2:A10. A5:A10 would be blank for this example):

Columns
Column_B
Column_D
Column_G

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
The query would only pull these 3 columns. However, if I change the list and refresh, it would pull the new list of columns.

<o:p></o:p>
I currently pull down the entire table through MS Query but the problem is the table has about 300 columns and I really only need a third of them at any given time. I do not think I can do it dynamically through MS Query but I could be wrong.

<o:p></o:p>
I know about using parameters in criteria to limit the observations pulled dynamically using “[]”. However, I am not sure how to dynamically pull columns.

<o:p></o:p>
Please let me know if you have any ideas. I would be happy to use VBA, just not sure how to do it.

<o:p></o:p>
Thank you in advance for your help.

Scott
 
You need VBA for this. Your MS Query has a SQL string behind it, accessible via VBA, like this:

Worksheets("Sheet1").QueryTables(1).CommandText = "Select * From Table"

You need to loop through your sheet range, and parse all the desired columns into a text string, then put that text string together with the rest of your sql statement.

Do you have any VBA experience?
 
Upvote 0
Hey Chris,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

Thank you for such a quick response. I do have some VBA experience. I would classify myself as an intermediate VBA user. However, I have not ever used VBA to query tables.

<o:p></o:p>
I actually recorded a macro so I have the code to connect to the server and the select statement. I named my list so I will have a name range to call in the VBA. I will call it “ColumnList”. Here is the macro I am working on:
<o:p></o:p>

Sub Macro1()
ColList = Range("ColumnList")

With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"ODBC;DRIVER=SQL Server;SERVER=DC-SQL-SRVR;UID=XXXXX;Trusted_Connection=Yes;APP=2007 Microsoft Office system;WSID=DCA-PC17" _

, Destination:=Range("$ZV$4")).QueryTable
.CommandText = Array( _
"SELECT TABLE1.COLUMNA, TABLE1.COLUMNB" & Chr(13) & "" & Chr(10) & "FROM database.db_owner.TABLE1 TABLE1 " & Chr(13) & "" & Chr(10) )

.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_SQL_13"
.Refresh BackgroundQuery:=False
End With

End Sub
<o:p></o:p>

However, I am not sure how to call “ColList” in the above select statement?

I think there is a problem with the quotes when I call the function.
<o:p></o:p>

Thank you again for your help.

<o:p></o:p>
Scott
 
Upvote 0
You need a whole seperate bit of code to prep your column list first.

Code:
Dim rCell as Range
dim strSql as String

For Each rCell in Range("ColumnList")
    If rCell <> "" Then
           strSql = StrSql & rCell & ","
    End If    
Next rCell

strSql = left(StrSql,Len(StrSql)-1)

Now you have your comma delimited list of columns in variable strSQL, now throw that into your command SQL line.
 
Upvote 0
Hey Chris,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Thanks again for your help earlier. I have another question about the same code.
<o:p></o:p>
I ran into a little bit of a problem pulling in my columns because the column names were numbers (for example “001”) instead of characters, but I manage to figure out a work around. It was just figuring out the right amount of quotes to add.
<o:p></o:p>
I managed to get the code working for a small sample of columns. I thought I had it all worked out until I expanded to a larger list of columns. For some unforeseen reason my code works for 19 columns (date column + 18 macro called columns) without issue. However, when I add the 20<SUP>th</SUP> column in my macro I get an error:
<o:p></o:p>
“Run-time error ‘13’:
Type mismatch”
<o:p></o:p>
It crashes at the select statement (“.CommandText = Array( “Select….). I have no idea why the code would work for 1 to 19 columns but then crash if the 20<SUP>th</SUP> column is added. I tried a couple things:
<o:p></o:p>
· I thought I might have to many characters in my string so I tried to split the macro variable in two, but got the same error after the 19<SUP>th</SUP> column
· I hard coded the 20 columns and surprisingly, it worked (It is quoted out in my code).
· I hard coded the extra column both before and after the macro (strSql). It worked when I had 19 columns but failed when I had 20 columns on both tries.
<o:p></o:p>
I am not sure what is going on. Please let me know if you have any ideas.
<o:p></o:p>
Scott
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
Sub Macro1()
'
' Macro1 Macro
'
Dim rCell As Range
Dim strSql As String
<o:p></o:p>
For Each rCell In Range("ColumnList")
If rCell <> "" Then
strSql = strSql & "" & """" & rCell & "" & """" & ", "
' Range("fill") = strSql
End If
Next rCell
<o:p></o:p>
strSql = "" & " " & Right(Left(strSql, Len(strSql) - 2), Len(strSql) - 2) & " " & ""
Range("fill") = strSql

With Range("try_table").ListObject.QueryTable
.Connection = _
"ODBC;DRIVER=SQL Server;SERVER=DC-SQL-SRVR;UID=scowin;Trusted_Connection=Yes;APP=2007 Microsoft Office system;WSID=DCA-PC17"

' .CommandText = Array( _
' "SELECT date, " & " ""001"", ""006"", ""007"", ""012"", ""014"", ""020"", ""030"", ""032"", ""041"", ""042"", ""043"", ""043"", ""043"", ""043"", ""043"", ""043"", ""043"", ""043"", ""043"" " & Chr(13) & "" & Chr(10) & _
' "FROM database.table1 table 1 " _
' & Chr(13) & "" & Chr(10) & "ORDER BY table1.date" _
' )
<o:p></o:p>
<o:p></o:p>
.CommandText = Array( _
"SELECT date, " & strSql _
& Chr(13) & "" & Chr(10) & _
"FROM database.table1 table1 " _
& Chr(13) & "" & Chr(10) & "ORDER BY table1.date" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Try_table"
.Refresh BackgroundQuery:=False
End With
End Sub
 
Upvote 0
I'm not sure why you are using Array() for your command text. It's just a string. Get rid of the array and all the double quotes and just assign a string.

.commandtext = "select * from table"

see if that works.
 
Upvote 0
Perfect! You nailed it. I was only using an array because that is how it recorded the macro when I pulled the data.

Thank you again for your help.
 
Upvote 0

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