Hi
Im trying to use the following code to copy the values in a Query to a cell range in Excel when a button is clilcked.
Private Sub Command120_Click()
'We'll start by creating a recordset named MyRecordset.
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
Dim MyRecordset As New ADODB.Recordset
MyRecordset.ActiveConnection = cnn
'Build the SQL Statement.
Dim MySQL As String
MySQL = "SELECT Query1.Tracker"
MySQL = MySQL & "FROM Query1"
MySQL = MySQL & "WHERE (((Query1.Tracker) Is Not Null));"
MyRecordset.Open MySQL
'Now MyRecordset contains records to be exported.
'Now for the Excel rigmarole.
'Note: You must change the path and filename below
'to an actual Excel .xlsx file on your own computer.
MySheetPath = "C:\Users\u549032.RRLOCAL\Documents\SABRe deployment governance PSSU.xlsx"
'Set up object variables to refer to Excel and objects.
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
'Open an instance of Excel, open the workbook.
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
'Make sure everything is visible on the screen.
Xl.Visible = True
XlBook.Windows(1).Visible = True
'Define the topmost sheet in the workbook as XlSheet.
Set XlSheet = XlBook.Worksheets(2)
'Copy the recordset to worksheet starting at cell B3.
XlSheet.Range("AC45:AC98").CopyFromRecordset MyRecordset
'Clean up and end with worksheet visible on the screen.
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing
End Sub
When the button is clicked I get the following
Run-time error '-2147217900 (80040e14)':
Syntax error (missing operator) in query expression
'Query1.TrackerFROM Query1WHERE (((Query1.Tracker) Is Not
Null))'.
Bit stumped why it does not work as I copied the code straight out of my Training Book?
The debug window shows where I have highlighted red as yellow background
Hope this makes sense.lol
Any help appreciated
Thanks
Im trying to use the following code to copy the values in a Query to a cell range in Excel when a button is clilcked.
Private Sub Command120_Click()
'We'll start by creating a recordset named MyRecordset.
Dim cnn As ADODB.Connection
Set cnn = CurrentProject.Connection
Dim MyRecordset As New ADODB.Recordset
MyRecordset.ActiveConnection = cnn
'Build the SQL Statement.
Dim MySQL As String
MySQL = "SELECT Query1.Tracker"
MySQL = MySQL & "FROM Query1"
MySQL = MySQL & "WHERE (((Query1.Tracker) Is Not Null));"
MyRecordset.Open MySQL
'Now MyRecordset contains records to be exported.
'Now for the Excel rigmarole.
'Note: You must change the path and filename below
'to an actual Excel .xlsx file on your own computer.
MySheetPath = "C:\Users\u549032.RRLOCAL\Documents\SABRe deployment governance PSSU.xlsx"
'Set up object variables to refer to Excel and objects.
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
'Open an instance of Excel, open the workbook.
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
'Make sure everything is visible on the screen.
Xl.Visible = True
XlBook.Windows(1).Visible = True
'Define the topmost sheet in the workbook as XlSheet.
Set XlSheet = XlBook.Worksheets(2)
'Copy the recordset to worksheet starting at cell B3.
XlSheet.Range("AC45:AC98").CopyFromRecordset MyRecordset
'Clean up and end with worksheet visible on the screen.
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing
End Sub
When the button is clicked I get the following
Run-time error '-2147217900 (80040e14)':
Syntax error (missing operator) in query expression
'Query1.TrackerFROM Query1WHERE (((Query1.Tracker) Is Not
Null))'.
Bit stumped why it does not work as I copied the code straight out of my Training Book?
The debug window shows where I have highlighted red as yellow background
Hope this makes sense.lol
Any help appreciated
Thanks