View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
arunkhemlai arunkhemlai is offline
external usenet poster
 
Posts: 16
Default Extracting from Access

Hi,

I assume that TUV.xls is where you put the code.

As I use "SaveAs" to create the jkl.xls file, be aware that every copy of
the file contains the vba code.

HTH -- arunkhemlai

' --------------------------------------------
Option Explicit

Sub ImportFromAccess()

Dim dbname, dbpath, conn

' ** Here put your access filename.
dbname = "Northwind.mdb"

' ** Path to your access file.
dbpath = "C:\Program Files\Microsoft Office\OFFICE11\SAMPLES\"

conn = _
"ODBC;" + _
"DSN=MS Access Database;" + _
"DBQ=" + dbpath + dbname + ";" + _
"DefaultDir=" + dbpath + ";" + _
"DriverId=25;" + _
"FIL=MS Access;"

With ActiveSheet.QueryTables.Add(Connection:=conn,
Destination:=Range("A1"))

' ** Change the query to what you want.
.CommandText = _
"SELECT Categories.CategoryID, Categories.Description " + _
"FROM `" + dbpath + dbname + "`.Categories Categories"

.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

Dim outname, outpath

' ** Output file name and path.
outname = "jkl.xls"
outpath = "c:\"

ActiveWorkbook.SaveAs Filename:=outpath + outname, FileFormat:=xlNormal

End Sub

' -------------------------------------





"Daniel Bonallack" wrote:

Can anyone help with the code to perform the steps below?

Starting from an Excel file called TUV.xls...

1. Open an Access database called XYZ.mdb
2. Run a query called ABC
3. Copy the results to an Excel file called JKL.xls

Is this possible?
If you can help me, I won't bother anyone on this forum for at least an hour.

Thanks
Daniel