View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
thiaga thiaga is offline
external usenet poster
 
Posts: 19
Default QueryTable Basics..

I am trying to assign a range of cells to a query table.
But am not able to do it..
The data is in a Sheet called "MasterData" not sure where to specify
that..
Also when i run the query, is it possible to get the data (In an array
or RecordSet) and do some formatting before it is displayed...

The below code returns a 1004 error..not sure why..

Thanks in Anticipation
Thiaga

Public Sub test21()
Dim dbqPath As String
Dim qtb As QueryTable
Const qtbName As String = "MyQueryTable"
Const strCnn As String = "ODBC;Driver={Microsoft Excel
Driver(*.xls)};DriverId=790;"
Dim strSQL As String
Dim targetSh As Worksheet

strSQL = "SELECT * FROM MyQueryTable"
dbqPath = ThisWorkbook.FullName
Set targetSh = ThisWorkbook.Sheets("Work Effort Summary")
Set rng = targetSh.Range("B15")
With targetSh
With .QueryTables.Add(Connection:=strCnn & "DBQ=" & dbqPath &
";", _
Destination:=rng, Sql:=strSQL)
.Name = qtbName
.FieldNames = True
.Refresh
End With
End With
End Sub