Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Importing data from Access to Excel, but I need to vary the table from Access

I've created a macro to import data from Access. The database I am
using contains over 30 tables and I'd like to be able to automate the
import process. I can't seem to create a variable that will substitute
for the table name so that I can choose which table I import at any
given time. Here is an example of the code I have generated.

With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y( _
"ODBC;DSN=MS Access Database;DBQ=D:\Documents and
Settings\Class2007\My Documents\Wave Data.mdb;DefaultDir=D:\Documents
and Settings\" _
), Array( _
"Class2007\My Documents;DriverId=25;FIL=MS
Access;MaxBufferSize=2048;PageTimeout=5;" _
)), Destination:=Range("A1"))
.CommandText = Array( _
"SELECT `127`.ID, `127`.Year, `127`.Month, `127`.Day,
`127`.Hour, `127`.Depth, `127`.Hmo, `127`.DTp, `127`.Atp, `127`.tmean,
`127`.wvdmn, `127`.wv, `127`.wsp, `127`.wdir" & Chr(13) & "" & Chr(10)
& "FROM `D:\Documents and Setting" _
, _
"s\Class2007\My Documents\Wave Data`.`127` `127`" & Chr(13) &
"" & Chr(10) & "WHERE (`127`.Year= " & Year1 & ")" & Chr(13) & "" &
Chr(10) & "ORDER BY `127`.Month, `127`.Day, `127`.Hour" _
)
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False

End With

Anywhere that it says '127' I'd like to make it a variable to use an
input to change it. It takes too much time to manually change the
table name each time.

Thanks,

Liz L.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Importing data from Access to Excel, but I need to vary the table from Access

s = "Wave Data.mdb"

With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y( _
"ODBC;DSN=MS Access Database;DBQ=D:\Documents and
Settings\Class2007\My Documents\" & s &";DefaultDir=D:\Documents
and Settings\" _
), Array( _
"Class2007\My Documents;DriverId=25;FIL=MS
Access;MaxBufferSize=2048;PageTimeout=5;" _
)), Destination:=Range("A1"))
.CommandText = Array( _
"SELECT `127`.ID, `127`.Year, `127`.Month, `127`.Day,
`127`.Hour, `127`.Depth, `127`.Hmo, `127`.DTp, `127`.Atp, `127`.tmean,
`127`.wvdmn, `127`.wv, `127`.wsp, `127`.wdir" & Chr(13) & "" & Chr(10)
& "FROM `D:\Documents and Setting" _
, _
"s\Class2007\My Documents\" & s & "`.`127` `127`" & Chr(13) &
"" & Chr(10) & "WHERE (`127`.Year= " & Year1 & ")" & Chr(13) & "" &
Chr(10) & "ORDER BY `127`.Month, `127`.Day, `127`.Hour" _
)
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False

End With

--
Regards,
Tom Ogilvy

"Liz L." wrote in message
oups.com...
I've created a macro to import data from Access. The database I am
using contains over 30 tables and I'd like to be able to automate the
import process. I can't seem to create a variable that will substitute
for the table name so that I can choose which table I import at any
given time. Here is an example of the code I have generated.

With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y( _
"ODBC;DSN=MS Access Database;DBQ=D:\Documents and
Settings\Class2007\My Documents\Wave Data.mdb;DefaultDir=D:\Documents
and Settings\" _
), Array( _
"Class2007\My Documents;DriverId=25;FIL=MS
Access;MaxBufferSize=2048;PageTimeout=5;" _
)), Destination:=Range("A1"))
.CommandText = Array( _
"SELECT `127`.ID, `127`.Year, `127`.Month, `127`.Day,
`127`.Hour, `127`.Depth, `127`.Hmo, `127`.DTp, `127`.Atp, `127`.tmean,
`127`.wvdmn, `127`.wv, `127`.wsp, `127`.wdir" & Chr(13) & "" & Chr(10)
& "FROM `D:\Documents and Setting" _
, _
"s\Class2007\My Documents\Wave Data`.`127` `127`" & Chr(13) &
"" & Chr(10) & "WHERE (`127`.Year= " & Year1 & ")" & Chr(13) & "" &
Chr(10) & "ORDER BY `127`.Month, `127`.Day, `127`.Hour" _
)
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False

End With

Anywhere that it says '127' I'd like to make it a variable to use an
input to change it. It takes too much time to manually change the
table name each time.

Thanks,

Liz L.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Importing data from Access to Excel, but I need to vary the table from Access

I don't want the variable to be the database but the table itself. So
the number 127 within the SELECT command, all of the data is within the
database file of "Wave Data.mdb" but in different tables.

Thanks,
Liz L.


Tom Ogilvy wrote:
s = "Wave Data.mdb"

With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y( _
"ODBC;DSN=MS Access Database;DBQ=D:\Documents and
Settings\Class2007\My Documents\" & s &";DefaultDir=D:\Documents
and Settings\" _
), Array( _
"Class2007\My Documents;DriverId=25;FIL=MS
Access;MaxBufferSize=2048;PageTimeout=5;" _
)), Destination:=Range("A1"))
.CommandText = Array( _
"SELECT `127`.ID, `127`.Year, `127`.Month, `127`.Day,
`127`.Hour, `127`.Depth, `127`.Hmo, `127`.DTp, `127`.Atp, `127`.tmean,
`127`.wvdmn, `127`.wv, `127`.wsp, `127`.wdir" & Chr(13) & "" & Chr(10)
& "FROM `D:\Documents and Setting" _
, _
"s\Class2007\My Documents\" & s & "`.`127` `127`" & Chr(13) &
"" & Chr(10) & "WHERE (`127`.Year= " & Year1 & ")" & Chr(13) & "" &
Chr(10) & "ORDER BY `127`.Month, `127`.Day, `127`.Hour" _
)
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False

End With

--
Regards,
Tom Ogilvy

"Liz L." wrote in message
oups.com...
I've created a macro to import data from Access. The database I am
using contains over 30 tables and I'd like to be able to automate the
import process. I can't seem to create a variable that will substitute
for the table name so that I can choose which table I import at any
given time. Here is an example of the code I have generated.

With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y( _
"ODBC;DSN=MS Access Database;DBQ=D:\Documents and
Settings\Class2007\My Documents\Wave Data.mdb;DefaultDir=D:\Documents
and Settings\" _
), Array( _
"Class2007\My Documents;DriverId=25;FIL=MS
Access;MaxBufferSize=2048;PageTimeout=5;" _
)), Destination:=Range("A1"))
.CommandText = Array( _
"SELECT `127`.ID, `127`.Year, `127`.Month, `127`.Day,
`127`.Hour, `127`.Depth, `127`.Hmo, `127`.DTp, `127`.Atp, `127`.tmean,
`127`.wvdmn, `127`.wv, `127`.wsp, `127`.wdir" & Chr(13) & "" & Chr(10)
& "FROM `D:\Documents and Setting" _
, _
"s\Class2007\My Documents\Wave Data`.`127` `127`" & Chr(13) &
"" & Chr(10) & "WHERE (`127`.Year= " & Year1 & ")" & Chr(13) & "" &
Chr(10) & "ORDER BY `127`.Month, `127`.Day, `127`.Hour" _
)
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False

End With

Anywhere that it says '127' I'd like to make it a variable to use an
input to change it. It takes too much time to manually change the
table name each time.

Thanks,

Liz L.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Importing data from Access to Excel, but I need to vary the table from Access

mm = "127"

"s\Class2007\My Documents\Wave Data`.`" & mm & "` `127`"


--
Regards,
Tom Ogilvy
"Liz L." wrote in message
oups.com...
I've created a macro to import data from Access. The database I am
using contains over 30 tables and I'd like to be able to automate the
import process. I can't seem to create a variable that will substitute
for the table name so that I can choose which table I import at any
given time. Here is an example of the code I have generated.

With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y( _
"ODBC;DSN=MS Access Database;DBQ=D:\Documents and
Settings\Class2007\My Documents\Wave Data.mdb;DefaultDir=D:\Documents
and Settings\" _
), Array( _
"Class2007\My Documents;DriverId=25;FIL=MS
Access;MaxBufferSize=2048;PageTimeout=5;" _
)), Destination:=Range("A1"))
.CommandText = Array( _
"SELECT `127`.ID, `127`.Year, `127`.Month, `127`.Day,
`127`.Hour, `127`.Depth, `127`.Hmo, `127`.DTp, `127`.Atp, `127`.tmean,
`127`.wvdmn, `127`.wv, `127`.wsp, `127`.wdir" & Chr(13) & "" & Chr(10)
& "FROM `D:\Documents and Setting" _
, _
"s\Class2007\My Documents\Wave Data`.`127` `127`" & Chr(13) &
"" & Chr(10) & "WHERE (`127`.Year= " & Year1 & ")" & Chr(13) & "" &
Chr(10) & "ORDER BY `127`.Month, `127`.Day, `127`.Hour" _
)
.Name = "Query from MS Access Database"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False

End With

Anywhere that it says '127' I'd like to make it a variable to use an
input to change it. It takes too much time to manually change the
table name each time.

Thanks,

Liz L.



Reply
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
Importing data from Excel to Access Linda Excel Discussion (Misc queries) 0 November 20th 07 04:35 PM
Importing Data from Access into Excel vnvkatz Excel Discussion (Misc queries) 3 June 9th 05 05:02 PM
problem with importing data from access to excel NEVARLEN Excel Programming 0 May 21st 04 11:43 PM
Importing Access data to Excel Chaprastee Excel Programming 1 May 10th 04 10:51 PM
Importing Access Data and formatting it in Excel....! paul donnelly Excel Programming 1 November 25th 03 10:01 AM


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

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"