doctorswamp
Technical User
Hi all
Have working code to send data from a table SampleTimesheet to an Excel template, as at bottom of this message.
I need to do the same thing using data from a query rather than a table. I've adapted the code but get VB errors when I replace this line
Set rst = CurrentDb.OpenRecordset("SampleTimesheet")
with an SQL statement derived from a query which links companyData and TimeSheetHistory tables, asks for a month number then extracts timesheet data.
The adapted version I tried is
Set rst = CurrentDb.OpenRecordset("SELECT TimesheetHistory.Date,
CompanyData.LTRef,
CompanyData.Company,
CompanyData.Contact,
CompanyData.CNo,
TimesheetHistory.Project,
TimesheetHistory.Adviser, TimesheetHistory.TimeSheetSource,
TimesheetHistory.Type,
TimesheetHistory.Reason,
TimesheetHistory.Notes
FROM TimesheetHistory INNER JOIN CompanyData ON TimesheetHistory.LTRef = CompanyData.LTRef
WHERE (((TimesheetHistory.Date)>#4/1/2005#) AND ((DatePart("m",[Date]))=[Enter Month No]))
ORDER BY TimesheetHistory.Date;")
but parts of this immediately come up in red. I've inserted some returns to make it easier to read.
Any suggestions for fixing this will be much appreciated.
----------------------------------
The working code for the simpler version was
Private Sub Timesheet_Click()
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim rst As Recordset
Dim iRow As Integer
Set objXL = New Excel.Application
objXL.Visible = True
Set objWkb = objXL.Workbooks.Open("c:\timesheet.xlt") ' Define the template to use
Set objSht = objWkb.Worksheets("Timesheet")
iRow = 6
Set rst = CurrentDb.OpenRecordset("SampleTimesheet")
rst.MoveFirst
Do While Not rst.EOF
' Insert the field names and row numbers of the fields to be inserted
objSht.Cells(iRow, 1).Value = rst!Date
objSht.Cells(iRow, 2).Value = rst!LTRef
objSht.Cells(iRow, 3).Value = rst!Company
objSht.Cells(iRow, 4).Value = rst!Contact
objSht.Cells(iRow, 5).Value = rst!Project
objSht.Cells(iRow, 6).Value = rst!CNo
'objSht.Cells(iRow, 7).Value = rst!Duration
'objSht.Cells(iRow, 8).Value = rst!TimeSheetSource
'objSht.Cells(iRow, 9).Value = rst!Project
'objSht.Cells(iRow, 10).Value = rst!Project
'objSht.Cells(iRow, 11).Value = rst!Project
iRow = iRow + 1
rst.MoveNext
Loop
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
rst.Close
End Sub
Have working code to send data from a table SampleTimesheet to an Excel template, as at bottom of this message.
I need to do the same thing using data from a query rather than a table. I've adapted the code but get VB errors when I replace this line
Set rst = CurrentDb.OpenRecordset("SampleTimesheet")
with an SQL statement derived from a query which links companyData and TimeSheetHistory tables, asks for a month number then extracts timesheet data.
The adapted version I tried is
Set rst = CurrentDb.OpenRecordset("SELECT TimesheetHistory.Date,
CompanyData.LTRef,
CompanyData.Company,
CompanyData.Contact,
CompanyData.CNo,
TimesheetHistory.Project,
TimesheetHistory.Adviser, TimesheetHistory.TimeSheetSource,
TimesheetHistory.Type,
TimesheetHistory.Reason,
TimesheetHistory.Notes
FROM TimesheetHistory INNER JOIN CompanyData ON TimesheetHistory.LTRef = CompanyData.LTRef
WHERE (((TimesheetHistory.Date)>#4/1/2005#) AND ((DatePart("m",[Date]))=[Enter Month No]))
ORDER BY TimesheetHistory.Date;")
but parts of this immediately come up in red. I've inserted some returns to make it easier to read.
Any suggestions for fixing this will be much appreciated.
----------------------------------
The working code for the simpler version was
Private Sub Timesheet_Click()
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim rst As Recordset
Dim iRow As Integer
Set objXL = New Excel.Application
objXL.Visible = True
Set objWkb = objXL.Workbooks.Open("c:\timesheet.xlt") ' Define the template to use
Set objSht = objWkb.Worksheets("Timesheet")
iRow = 6
Set rst = CurrentDb.OpenRecordset("SampleTimesheet")
rst.MoveFirst
Do While Not rst.EOF
' Insert the field names and row numbers of the fields to be inserted
objSht.Cells(iRow, 1).Value = rst!Date
objSht.Cells(iRow, 2).Value = rst!LTRef
objSht.Cells(iRow, 3).Value = rst!Company
objSht.Cells(iRow, 4).Value = rst!Contact
objSht.Cells(iRow, 5).Value = rst!Project
objSht.Cells(iRow, 6).Value = rst!CNo
'objSht.Cells(iRow, 7).Value = rst!Duration
'objSht.Cells(iRow, 8).Value = rst!TimeSheetSource
'objSht.Cells(iRow, 9).Value = rst!Project
'objSht.Cells(iRow, 10).Value = rst!Project
'objSht.Cells(iRow, 11).Value = rst!Project
iRow = iRow + 1
rst.MoveNext
Loop
Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
rst.Close
End Sub