I have a command button that when clicked should export the current filtered records (on a form) to an excel template.
I found some code and modified it a little bit to work with what I am doing, but the only problem is that I cannot figure out a way to export only the current listed records. The form is based of a table, not a query. The form filters itself in the form query builder by foUser, so I can't just use the table in this export code, otherwise it will export thousands of records rather than just a few dozen.
Here is the code I am playing with:
Code:
On Error GoTo err_Handler
MsgBox ExportRequest, vbInformation, "Finished"
Application.FollowHyperlink CurrentProject.Path & "\ExportedSites.xlsx"
exit_Here:
Exit Sub
err_Handler:
MsgBox Err.Description, vbCritical, "Error"
Resume exit_Here
End Sub
Public Function ExportRequest() As String
On Error GoTo err_Handler
' Excel object variables
Dim appExcel As Excel.Application
Dim wbk As Excel.Workbook
Dim wks As Excel.Worksheet
Dim sTemplate As String
Dim sTempFile As String
Dim sOutput As String
Dim dbs As DAO.database
Dim rst As DAO.Recordset
Dim sSQL As String
Dim lRecords As Long
Dim iRow As Integer
Dim iCol As Integer
Dim iFld As Integer
Const cTabTwo As Byte = 1
Const cStartRow As Byte = 6
Const cStartColumn As Byte = 3
DoCmd.Hourglass True
' set to break on all errors
Application.SetOption "Error Trapping", 2
' start with a clean file built from the template file
sTemplate = CurrentProject.Path & "\ExportTemplate.xlsx"
sOutput = CurrentProject.Path & "\ExportedSites.xlsx"
If Dir(sOutput) <> "" Then Kill sOutput
FileCopy sTemplate, sOutput
' Create the Excel Applicaiton, Workbook and Worksheet and Database object
Set appExcel = New Excel.Application
Set wbk = appExcel.Workbooks.Open(sOutput)
Set wks = appExcel.Worksheets(cTabTwo)
sSQL = "select * from qryDummy"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(sSQL, dbOpenSnapshot)
If Not rst.BOF Then rst.MoveFirst
' For this template, the data must be placed on the 4th row, third column.
' (these values are set to constants for easy future modifications)
iCol = cStartColumn
iRow = cStartRow
Do Until rst.EOF
iFld = 0
lRecords = lRecords + 1
' Me.lblMsg.Caption = "Exporting record #" & lRecords & " to SalesOutput.xls"
'Me.Repaint
For iCol = cStartColumn To cStartColumn + (rst.Fields.Count - 1)
wks.Cells(iRow, iCol) = rst.Fields(iFld)
If InStr(1, rst.Fields(iFld).Name, "Date") > 0 Then
wks.Cells(iRow, iCol).NumberFormat = "mm/dd/yyyy"
End If
wks.Cells(iRow, iCol).WrapText = False
iFld = iFld + 1
Next
wks.Rows(iRow).EntireRow.AutoFit
iRow = iRow + 1
rst.MoveNext
Loop
'ExportRequest = "Total of " & lRecords & " rows processed."
'Me.lblMsg.Caption = "Total of " & lRecords & " rows processed."
exit_Here:
' Cleanup all objects (resume next on errors)
On Error Resume Next
Set wks = Nothing
Set wbk = Nothing
Set appExcel = Nothing
Set rst = Nothing
Set dbs = Nothing
DoCmd.Hourglass False
Exit Function
err_Handler:
ExportRequest = Err.Description
'Me.lblMsg.Caption = Err.Description
Resume exit_Here
End Function
Is there a way to replace this line here:
Code:
sSQL = "select * from qryDummy"
with something that will grab the current view on the form? Lets say the form has 100 records when you open it (filtered down from thousands using the foUsername). Then if the user can filter those records by something else, leaving only 10 records left in view; How can I get that sql string to select those records?
Thanks