First thing you need to do is change all the date fields in table to Date/Time type instead of text.
The query is not needed. Code can pull records direct from table.
Need lines to Set the Excel application and recordset variables.
The SQL needs ; punctuation.
All code modules should have these two lines in header:
Option Compare Database
Option Explicit
I don't have your Excel template so can't test with that.
Code:
Option Compare Database
Option Explicit
Public Sub CreateExcelInfo()
Dim oExcel As New Excel.Application
Dim WB As New Excel.Workbook
Dim WS As Excel.Worksheet
Dim rng As Excel.Range
Dim objConn As New ADODB.Connection
Dim objRs As New ADODB.Recordset
Const sFileNameTemplate As String = "C:\Users\MBOBO\Desktop\on-time\Book2.xlsm"
Dim sSQL As String
Set objConn = CurrentProject.Connection
Set objRs = New ADODB.Recordset
Set oExcel = New Excel.Application
sSQL = "SELECT * FROM Yard_TB WHERE [Appointment Date] BETWEEN #" & [Forms]![Main]![Start_Date_MF] & "# AND #" & [Forms]![Main]![End_Date_MF] & "#;"
With oExcel
.Visible = True
Set WB = .Workbooks.Add(sFileNameTemplate)
With WB
Set WS = WB.Worksheets("Sheet2")
With WS
objRs.Open sSQL, objConn, adOpenStatic, adLockReadOnly
Set rng = .Range("A3") 'Starting point of the data range
rng.CopyFromRecordset objRs
objRs.Close
End With
End With
.Quit
End With
Set objConn = Nothing
Set objRs = Nothing
End Sub