LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 18
Default Problems with using SQL via VBA to extract db data and copy to a worksheet

I had posted this in microsoft.public.excel.querydao, but there does not
seem to be much activity there. So re-posting here.....
This is my 1st attempt to use a SQL query via VBA to extract data from an
Access db and copy it to an Excel sheet. Having some problems.

Using the excellent info in
http://www.dicks-clicks.com/excel/Ex....htm#CreateVBA as a guide, I
have my code as follows:

1) When the Excel file is opened, the Public Sub Workbook_Open()
subroutine runs and
- Calls a Sub to create the DBQ parameter and the DefaultDir parameters.
This works fine
- When it Tries to run the next statement, "Call CreateQT", it gives a
compile error that: Sub or Function not defined

Could someone please explain. I have attached the "Public Sub CreateQT()"
sub (partly reproduced below) to the "LinkedLineData" sheet where I want the
extracted data copied to (I understand this is required).

2) The code extract is:

Public Sub CreateQT()

Dim sConn As String
Dim sSql As String
Dim oQt As QueryTable

sConn = "ODBC;DSN=MS Access Database;"
sConn = sConn & "DBQ=" & ReportFileName & ";"
sConn = sConn & "DefaultDir=" & DataFileDirectory & ";"
sConn = sConn & "DriverId=281;FIL=MS
Access;MaxBufferSize=2048;PageTimeout=5;"

sSql = "SELECT "
sSql = sSql & "`Line Report`.datetime, "
sSql = sSql & "`Line Report`.groupNumber, "
:
:
sSql = sSql & "`Line Report`.totalLineBusyTime"
sSql = sSql & "FROM `" & DataFileDirectory & "\" & ReportFileName &
"`.`Line Report` `Line Report`"
sSql = sSql & "ORDER BY `Line Report`.datetime, `Line
Report`.groupNumber"

Set oQt = ThisWorkbook.Sheets("LinkedLineData").QueryTables. Add( _
Connection:=sConn, _
Destination:=Range("a1"), _
Sql:=sSql)

oQt.Refresh

End Sub




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
extract data from another worksheet G&GSolutions Excel Discussion (Misc queries) 0 February 25th 09 02:29 PM
Copy worksheet containing names to other workbook w/o problems? amsbam1 Excel Discussion (Misc queries) 1 December 3rd 08 04:23 PM
Extract data from one worksheet SKY Excel Worksheet Functions 5 April 27th 06 01:48 PM
Extract data from one Worksheet to another extract data fr. one worksheet to anothe Excel Worksheet Functions 1 August 15th 05 07:11 PM
extract data from worksheet sallu Excel Worksheet Functions 2 February 21st 05 06:50 AM


All times are GMT +1. The time now is 08:55 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"