Here we go, take a try at this (modifying as need be):
1. Create an Excel workbook that will be grabbing the data from Access. Import the access table you want to use as the standard table for this operation as the data source - under external data sources in Excel.
2. With Excel set to import the data, set the option for "refresh on open"
3. Save this workbook as a template - be sure to save it in a location that you and/or others who may need it can get to it. And if it were me, I'd close the file when ready, and check the file attribute, "hidden", and perhaps "read only" when you've finished editing it. This will keep anyone else from accidentally editing it or deleting it.
4. Set up your code that can run each time, and give you the results you need without a lot of headache. For your purposes, I don't think it really matters what the table name is, you just need to get it out. Therefore, I'd use a static table name, and simply refresh the data in said table on each occasion via code. It's easier than it sounds, and probably will run faster anyway.
For the example code, I'm going to name the table,
tblExport
Code:
Private Sub ExportMyData()
Dim strSQL As String 'one SQL variable to rule them all!
Dim strXl As String 'file path to your Excel Template
Dim appXl as Excel.Application
Set appXl = New Excel.Application
strXl = "C:\MyExcelTemplate.xlt"
If Forms![Log In Form]![Record or Report2] = 3 And Forms![Log In Form]![Start Date Count] = 1 Then
[GREEN]'First, run some SQL to clear out any existing data in the export table.[/GREEN]
strSQL = "TRUNCATE TABLE tblExport"
DoCmd.SetWarnings FALSE
DoCmd.RunSQL strSQL
DoCmd.SetWarnings TRUE
[GREEN]'Next, run your SQL to populate the table.[/GREEN]
strSQL = "INSERT INTO tblExport SELECT a.* FROM [Record_Report Query with Start Date6] a"
DoCmd.SetWarnings FALSE
DoCmd.RunSQL
DoCmd.SetWarnings TRUE
End If
[GREEN]'Open the Excel Template which will create a new Excel file in the process, but already have your data in place.[/GREEN]
With appXl
.Visible = True
.Workbooks.Open (strXl)
End With
[GREEN]'Clean up variables - yeah, the last 2 are probably not necessary, but I just like cleaning up as much as possible.[/GREEN]
Set appXl = Nothing
strXl = vbNullString
strSQL = vbNullString
End Sub
Of course, the Excel file path would be different to whatever you want to use. Also, you can save the new workbook in code if you want, but if you're going to open it anyway, you can also leave that part out.
Also, you CAN run this as a Function if you prefer, but if you're not feeding any values to it in code, I don't see the purpose/need. But to each his/her own.