PC Review


Reply
Thread Tools Rate Thread

Code problem working with QueryTable

 
 
=?Utf-8?B?QmlsbHkgQg==?=
Guest
Posts: n/a
 
      28th Sep 2007
I am attempting to query a database and insert the data into ranges in the
worksheet. The database is of volunteers and training times. The desired
result is to show volunteers for 8:00, 9:00, etc. classes. The results would
look like this:

001 Tom Jones 8:00 A1
002 Bill Jennings 8:00 B2 etc

001 Tom Hones 9:00 C3
003 Ken Walin 9:00 etc

In the code below, I am trying to create the ranges; insert the data for the
hour starting at the range; create the next range two lines below the first
range. The number of individuals for each hour will change day by day hence
the reason I am trying to do this using ranges (for sorting etc later in the
code.)

Problem: either I can't use range names in the QueryTables argument or I
have a code problem. Help or direction would be appreciated. Thank you.

Private Sub TestingRange_Click()
Dim rngSortData8 As Range, rngSortData9 As Range
Dim wksList As Worksheet
Set wksList = ActiveWorkbook.Sheets("Sheet1")
Set rngSortData8 = wksList.Range("A3")
rngSortData8.Name = "Eight"

'Range("A20") for testing only
'Should be two rows down from end of first query result
Set rngSortData9 = wksList.Range("A20")
rngSortData9.Name = "Nine"

CreateQueryTables

End Sub
Sub CreateQueryTables()

Dim strCnn As String, strCmdTxt As String
strCnn = "ODBC;DBQ=F:\EducationPro\EducationPro-New.mdb;" & _
"Driver={Microsoft Access Driver (*.mdb)};DriverId=25;FIL=MS
Access;PageTimeout=15"
strCmdTxt = Empty


strCmdTxt = "SELECT [Volunteer].IDNumber, [Volunteer].Name," & _
"[Volunteer].[8:00]FROM [Volunteer]"

rngSortData8.Select

' Create the QueryTable on the ActiveSheet at the range stated.
'Insert query results starting at range name Eight\
'*****************************************
'I get an error in the Range("Eight") but not if I use Range("A2')
With QueryTables.Add(Connection:=strCnn, Destination:=Range("Eight"))
If .QueryType = xlOLEDBQuery Then .CommandType = xlCmdDefault
.CommandText = strCmdTxt
.RefreshStyle = xlOverwriteCells
.HasAutoFormat = False
.RefreshOnFileOpen = False
.Refresh
End With

'*****************************************
'I need to determine the last row of the data inserted from above query
'move down two rows and either create the named range "nine" to insert
new
'query results or insert results using the cell reference that would
'correspond to the need.
'*****************************************
strCmdTxt = "SELECT [Volunteer].IDNumber, [Volunteer].Name," & _
"[Volunteer].[9:00]FROM [Volunteer]"

rngSortData9.Select

' Create the QueryTable on the ActiveSheet at the range stated.
'Insert query results starting at range name Nine
With QueryTables.Add(Connection:=strCnn, Destination:=Range("Nine"))
If .QueryType = xlOLEDBQuery Then .CommandType = xlCmdDefault
.CommandText = strCmdTxt
.RefreshStyle = xlOverwriteCells
.HasAutoFormat = False
.RefreshOnFileOpen = False
.Refresh
End With

End Sub
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      29th Sep 2007
I got it to work with the following two changes

1) Aded active sheet to query
With ActiveSheet.QueryTables.Add(Connection:=Strcnn,
Destination:=Range("Eight"))
2) The sheet name was in the name Eight.

I think you have to make usre the sheet in the QueryTable and the name range
must match.

"Billy B" wrote:

> I am attempting to query a database and insert the data into ranges in the
> worksheet. The database is of volunteers and training times. The desired
> result is to show volunteers for 8:00, 9:00, etc. classes. The results would
> look like this:
>
> 001 Tom Jones 8:00 A1
> 002 Bill Jennings 8:00 B2 etc
>
> 001 Tom Hones 9:00 C3
> 003 Ken Walin 9:00 etc
>
> In the code below, I am trying to create the ranges; insert the data for the
> hour starting at the range; create the next range two lines below the first
> range. The number of individuals for each hour will change day by day hence
> the reason I am trying to do this using ranges (for sorting etc later in the
> code.)
>
> Problem: either I can't use range names in the QueryTables argument or I
> have a code problem. Help or direction would be appreciated. Thank you.
>
> Private Sub TestingRange_Click()
> Dim rngSortData8 As Range, rngSortData9 As Range
> Dim wksList As Worksheet
> Set wksList = ActiveWorkbook.Sheets("Sheet1")
> Set rngSortData8 = wksList.Range("A3")
> rngSortData8.Name = "Eight"
>
> 'Range("A20") for testing only
> 'Should be two rows down from end of first query result
> Set rngSortData9 = wksList.Range("A20")
> rngSortData9.Name = "Nine"
>
> CreateQueryTables
>
> End Sub
> Sub CreateQueryTables()
>
> Dim strCnn As String, strCmdTxt As String
> strCnn = "ODBC;DBQ=F:\EducationPro\EducationPro-New.mdb;" & _
> "Driver={Microsoft Access Driver (*.mdb)};DriverId=25;FIL=MS
> Access;PageTimeout=15"
> strCmdTxt = Empty
>
>
> strCmdTxt = "SELECT [Volunteer].IDNumber, [Volunteer].Name," & _
> "[Volunteer].[8:00]FROM [Volunteer]"
>
> rngSortData8.Select
>
> ' Create the QueryTable on the ActiveSheet at the range stated.
> 'Insert query results starting at range name Eight\
> '*****************************************
> 'I get an error in the Range("Eight") but not if I use Range("A2')
> With QueryTables.Add(Connection:=strCnn, Destination:=Range("Eight"))
> If .QueryType = xlOLEDBQuery Then .CommandType = xlCmdDefault
> .CommandText = strCmdTxt
> .RefreshStyle = xlOverwriteCells
> .HasAutoFormat = False
> .RefreshOnFileOpen = False
> .Refresh
> End With
>
> '*****************************************
> 'I need to determine the last row of the data inserted from above query
> 'move down two rows and either create the named range "nine" to insert
> new
> 'query results or insert results using the cell reference that would
> 'correspond to the need.
> '*****************************************
> strCmdTxt = "SELECT [Volunteer].IDNumber, [Volunteer].Name," & _
> "[Volunteer].[9:00]FROM [Volunteer]"
>
> rngSortData9.Select
>
> ' Create the QueryTable on the ActiveSheet at the range stated.
> 'Insert query results starting at range name Nine
> With QueryTables.Add(Connection:=strCnn, Destination:=Range("Nine"))
> If .QueryType = xlOLEDBQuery Then .CommandType = xlCmdDefault
> .CommandText = strCmdTxt
> .RefreshStyle = xlOverwriteCells
> .HasAutoFormat = False
> .RefreshOnFileOpen = False
> .Refresh
> End With
>
> End Sub

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
QueryTable Problem Alan Microsoft Excel Programming 4 2nd Apr 2009 10:47 PM
QueryTable causes Heisenbug (difference between running and debugging code) [SOLVED] Amedee Van Gasse Microsoft Excel Programming 0 21st Jun 2007 11:03 AM
QueryTable date problem Tom Microsoft Excel Programming 0 3rd May 2006 02:43 PM
Add QueryTable in code Jim Hughes Microsoft Excel Programming 4 13th Mar 2005 08:28 AM
QueryTable Refresh Event Code Tim Microsoft Excel Misc 0 2nd Sep 2004 06:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:18 AM.