I have code behind a timesheet form that I want to modify. I currently have an IF statement that allows the strTemplatePath to save timesheets depending on which company work is being performed. I need a way to modify the strTemplatePath so that it uses the CompanyName along with the date and .xlsm when it saves.
I tried this but it does not work:
strTemplatePath = CurrentProject.Path & "\TimeSheetTemplate & CompanyName & .xlsm"
This is the current code using an IF statement
I tried this but it does not work:
strTemplatePath = CurrentProject.Path & "\TimeSheetTemplate & CompanyName & .xlsm"
This is the current code using an IF statement
Code:
Private Sub subTransferDataToExcel()
On Error Resume Next
Dim sCriteria As String
Dim db As Database
Dim rst As Recordset
Dim objApp As Excel.Application
Dim objBook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim strTemplatePath As String
Dim newFile As String
Dim fDt As String
Dim strSavePath As String
Dim strCompany As String
sCriteria = " 1 = 1 "
If CompanyName <> "" Then
sCriteria = sCriteria & " AND qryTransferDataToExcel.CompanyName = """ & CompanyName & """"
End If
If BeginDate <> "" And EndDate <> "" Then
sCriteria = sCriteria & " AND qryTransferDataToExcel.DateWorked between #" & Format(BeginDate, "dd-mmm-yyyy") _
& "# and #" & Format(EndDate, "dd-mmm-yyyy") & "#"
End If
Set db = CurrentDb()
strCompany = CompanyName
[highlight #FCE94F]If strCompany = "CPSI" Then
strTemplatePath = CurrentProject.Path & "\TimeSheetTemplateCPSI.xlsm"
Else:
strTemplatePath = CurrentProject.Path & "\TimeSheetTemplateGLOBAL.xlsm"
End If[/highlight]
Set objApp = New Excel.Application
Set objBook = objApp.Workbooks.Add(strTemplatePath)
'Name of sheet you want to export to
Set objApp = objBook.Parent
Set objSheet = objBook.Worksheets("TimeSheet")
objBook.Windows(1).Visible = True
'Opens the recordset and sets the variable
Set rst = db.OpenRecordset("SELECT [Time Card Hours].DateWorked, [Sum Of BillableHours] FROM qryTransferDataToExcel WHERE " _
& sCriteria, dbOpenSnapshot)
With objSheet
.Select
'Clears the current contents in the workbook range
.Range("C16:D22").ClearContents
'rst Copies the recordset into the worksheet
.Range("C16").CopyFromRecordset rst
End With
rst.Close
objApp.Visible = True
'Saves filename with the date value in C22
fDt = Range("C22").Value
'Change the date format
newFile = "time sheet" & Format$(fDt, "mmddyyyy")
' Where to save the new workbook
strSavePath = "C:\Documents and Settings\Dominic Fino\My Documents\CPSI\TimeBilling2010\TimeSheets\"
ActiveWorkbook.SaveAs FileName:=strSavePath & newFile
Set rst = Nothing
Set db = Nothing
Set objSheet = Nothing
Set objBook = Nothing
Set objApp = Nothing
End Sub