koolwaters
Active Member
- Joined
- May 16, 2007
- Messages
- 403
Hi,
I am trying to export an Access query into an Excel template.
The query is exported starting in cell B4.
My code works for the most part but I need to export the query to columns B-K, skip columns L-R (as these contain formulas ) and then export to column S.
My challenge is that i am not sure how to get the last field in my query exported, starting at S4 in the template. The code below is what I am using but it puts the last field of the query in column L but I want it in column S.
Additionally, I would like to retain the formatting in the template and save the template as an .xlsx workbook using the month portion of the date in cell C1 but I was playing around with the code to no avail.
Thanks for your feedback.
I am trying to export an Access query into an Excel template.
The query is exported starting in cell B4.
My code works for the most part but I need to export the query to columns B-K, skip columns L-R (as these contain formulas ) and then export to column S.
My challenge is that i am not sure how to get the last field in my query exported, starting at S4 in the template. The code below is what I am using but it puts the last field of the query in column L but I want it in column S.
Code:
Public Sub ExportReleases()
Dim cnn As ADODB.Connection
Dim MyRecordset As New ADODB.Recordset
Dim MySQL As String, stPath As String
Dim Xl As Object, XlBook As Object, XlSheet As Object
Dim db As DAO.Database
Set cnn = CurrentProject.Connection
Dim myStartDate As Date, myEndDate As Date
On Error Resume Next
myStartDate = Me.StartDate
myEndDate = Me.EndDate
MyRecordset.ActiveConnection = cnn
If Not IsNothing(Me.StartDate) Then
If Not IsDate(Me.StartDate) Then
MsgBox "You must enter a valid 'From' date.", vbExclamation, gstrAppTitle
Me.StartDate.SetFocus
Exit Sub
End If
End If
If Not IsNothing(Me.EndDate) Then
If Not IsDate(Me.EndDate) Then
MsgBox "You must enter a valid 'To' date.", vbExclamation, gstrAppTitle
Me.EndDate.SetFocus
Exit Sub
End If
If Not IsNothing(Me.StartDate) Then
If Me.EndDate < Me.StartDate Then
MsgBox "'To' Date must not be earlier than 'From' Date.", _
vbExclamation, gstrAppTitle
Me.EndDate.SetFocus
Exit Sub
End If
End If
End If
DoCmd.SetWarnings False
MySQL = "SELECT * FROM qrptBOS " & _
"WHERE BOSReceived between #" & myStartDate & "# and #" & myEndDate & "#;"
MyRecordset.Open MySQL
stPath = GetFEPath & "\Excel Reports\BOS.xltx"
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(stPath)
Xl.Visible = True
XlBook.Windows(1).Visible = True
Set XlSheet = XlBook.Worksheets("BOS")
XlSheet.Range("B4").CopyFromRecordset MyRecordset
XlSheet.Range("C1") = myEndDate
MyRecordset.Close
MyRecordset.Close
Set cnn = Nothing
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing
DoCmd.SetWarnings True
End Sub
Additionally, I would like to retain the formatting in the template and save the template as an .xlsx workbook using the month portion of the date in cell C1 but I was playing around with the code to no avail.
Thanks for your feedback.
Last edited: