I keep getting this error 2147217900 (80040e14) Invalid Sql statement; Expected Delete,Insert,Procedure,Select or Update when trying to transfer records to Excel workbook. Here’s my code run from a command button.
Dim strMonth As String
Dim rst As ADODB.Recordset
Dim xlApp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlbook = xlApp.Workbooks.Open("d:\cams\CAMSWeeklyTop10.xls") '.Add
Set xlsheet = xlbook.Worksheets("Summary")
Set rst = New ADODB.Recordset
strWeekEnding = Format(Date - Weekday(Date, vbSunday), "DD/MM")
strMonth = Format(Date, "mmmm")
rst.Open _
Source:="WeeklyTop10", _
ActiveConnection:=CurrentProject.Connection
xlApp.DisplayAlerts = False
With xlsheet
With .Cells(3, 1)
.Value = rst.Fields(0).Name
.Font.Bold = True
End With
With .Cells(3, 2)
.Value = rst.Fields(1).Name
.Font.Bold = True
End With
With .Cells(3, 3)
.Value = rst.Fields(2).Name
.Font.Bold = True
End With
.Range("A4").CopyFromRecordset rst
.Columns.AutoFit
.SaveAs "d:\cams\CAMSWeeklyTop10.xls"
End With
xlApp.Workbooks.Close
xlApp.DisplayAlerts = True
Set rst = Nothing
Set xlsheet = Nothing
Set xlbook = Nothing
Set xlApp = Nothing
MsgBox "Done"
The query I'm trying to use uses two other queries that get a parameter passed to them from the Form with the command button on it. I pass what week of the year is based on a date selection. If I run the queries by them selves they work but if I use
rst.Open _
Source:="WeeklyTop10", _
ActiveConnection:=CurrentProject.Connection
it won’t work. Oh and if I go to the queries and put in a week in the criteria in Desing view it will work but I need to be able to change the week.Any suggestions or ideas would be greatly appreciated.
Sna1maa14
Dim strMonth As String
Dim rst As ADODB.Recordset
Dim xlApp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlbook = xlApp.Workbooks.Open("d:\cams\CAMSWeeklyTop10.xls") '.Add
Set xlsheet = xlbook.Worksheets("Summary")
Set rst = New ADODB.Recordset
strWeekEnding = Format(Date - Weekday(Date, vbSunday), "DD/MM")
strMonth = Format(Date, "mmmm")
rst.Open _
Source:="WeeklyTop10", _
ActiveConnection:=CurrentProject.Connection
xlApp.DisplayAlerts = False
With xlsheet
With .Cells(3, 1)
.Value = rst.Fields(0).Name
.Font.Bold = True
End With
With .Cells(3, 2)
.Value = rst.Fields(1).Name
.Font.Bold = True
End With
With .Cells(3, 3)
.Value = rst.Fields(2).Name
.Font.Bold = True
End With
.Range("A4").CopyFromRecordset rst
.Columns.AutoFit
.SaveAs "d:\cams\CAMSWeeklyTop10.xls"
End With
xlApp.Workbooks.Close
xlApp.DisplayAlerts = True
Set rst = Nothing
Set xlsheet = Nothing
Set xlbook = Nothing
Set xlApp = Nothing
MsgBox "Done"
The query I'm trying to use uses two other queries that get a parameter passed to them from the Form with the command button on it. I pass what week of the year is based on a date selection. If I run the queries by them selves they work but if I use
rst.Open _
Source:="WeeklyTop10", _
ActiveConnection:=CurrentProject.Connection
it won’t work. Oh and if I go to the queries and put in a week in the criteria in Desing view it will work but I need to be able to change the week.Any suggestions or ideas would be greatly appreciated.
Sna1maa14