How to get Access VBA to export query with Criteria into Excel file

Bobfred

New Member
Joined
Aug 18, 2015
Messages
10
Hi

I am trying to make a VBA module in Access to export a query from Access into an existing Excel file and worksheet.

From looking around on Youtube I have found the code below.

This works fine for queries where no input is required. However when I run my query a box pops up asking you to enter the Latitude followed by a box asking for Longitude. Once this criteria is input it will then run the query.

However the right you have to input values into the query seems to break the below code.

Any suggestions on how I can amend the below code so when I run the VBA Module it will run the query asking me for Lat and Long. Once I have input the values it will then export the data to an existing file called All and best.xlsm and paste it on the worksheet All??



Public Sub ExportToExcel()


Dim XL As Excel.Application
Dim wbTarget As Workbook


Dim qdfLocation As QueryDef
Dim rsLocation As Recordset


DoCmd.OpenQuery ("By Location - use coord calc")


Set qdfLocation = CurrentDb.QueryDefs("By Location - use coord calc")


Set rsLocation = qdfLocation.OpenRecordset()


Set XL = CreateObject("Excel.Application")


Set wbTarget = XL.Workbooks.Open("C:\Users\Bob\Desktop\All and best.xlsm")


wbTarget.Worksheets("All").Cells.ClearContents


wbTarget.Worksheets("All").Cells(1, 1).CopyFromRecordset rsLocation


wbTarget.Save


wbTarget.Close


Set wbTarget = Nothing
Set XL = Nothing


Set qdfLocation = Nothing


End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Can you show the SQL in the query. I assume this is where the Parameter values are added.
 
Upvote 0
The SQL in the background of the query is..

WHERE (((Measurements.Date)>Now()-730) AND ((Measurements.Latitude) Between [Enter Ymin] And [Enter Ymax]) AND ((Measurements.Longitude) Between [Enter Xmin] And [Enter Xmax]));
 
Upvote 0
Also the original post had a line in there that should be DoCmd.OpenQuery ("By Location - use coord calc").


So the correct code is:

Public Sub ExportToExcel()


Dim XL As Excel.Application
Dim wbTarget As Workbook


Dim qdfLocation As QueryDef
Dim rsLocation As Recordset




Set qdfLocation = CurrentDb.QueryDefs("By Location - use coord calc")


Set rsLocation = qdfLocation.OpenRecordset()


Set XL = CreateObject("Excel.Application")


Set wbTarget = XL.Workbooks.Open("C:\Users\csurv\Desktop\All and best serving.xlsm")


wbTarget.Worksheets("All").Cells.ClearContents


wbTarget.Worksheets("All").Cells(1, 1).CopyFromRecordset rsLocation


wbTarget.Save


wbTarget.Close


Set wbTarget = Nothing
Set XL = Nothing


Set qdfLocation = Nothing


End Sub
 
Upvote 0
It seems to error on the line

'Set rsLocation = qdfLocation.OpenRecordset()'

which makes me think its because of the Criteria the query is asking for
 
Upvote 0
Can you upload a copy to dropbox or something like that and place a link into your thread I can take a look from there.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top