VBA Code to export query data from MS Access to Excel

  • Thread starter Thread starter shams100
  • Start date Start date
S

shams100

I'm looking for an example VBA Code to export or transfer query data
from MS Access
to Excel.

I use this code:

Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets("Sheet1")
xlSheet.Name = "sheetName"


With xlApp
With xlSheet.QueryTables.Add(Connection:=MyConnect, _
Destination:=Range("A1"))
.CommandText = "My SQl query"
.Name = "clearingHouse"
.FieldNames = True
.BackgroundQuery = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
End With


Evry time I run it I got this message error after it open Excel
application and create the sheet with the name:

Method Range of Object_ global faild

Does anyone know wehere i can find some example VBA code to accomplish
this.
Thanks,
 
What version of ACCESS are you using? What is the SP number that you last
installed?
 
ACCESS 2003 SP1 has not "lost" the ability to edit data in an EXCEL sheet,
but I admit that I'm not familiar with the code sequence that you posted --
it's much more EXCEL VBA than ACCESS VBA.

Have you tried using the TransferSpreadsheet method in ACCESS for creating a
new EXCEL workbook file from a query? Check it out in Help file. Post back
if that won't do what you need done.
 
Method Range of Object_ global faild
indicates that the problem is in the line
Destination:=Range("A1"))
(since that's the only place you use the Range method)

You might try:
Destination:=xlSheet.Range("A1"))

"1004" errors (of which Method Range of Object_ global failed is a member)
are almost always caused because code was not specific enough for VB to act
upon. Like specifying a range and assuming VB would "know" which sheet you
meant. There are exceptions to that generalization, but 95% of the time
that will be the cause.

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top