sebble1984
New member
- Local time
- Today, 14:05
- Joined
- Sep 24, 2014
- Messages
- 9
Hi Guys,
I have wrote some code to export to excel, which work in access 2013 full version, but testing the system out in a runtime environment the system crashes because I am referencing the excel library in the VBA references.
I have now removed the reference and I am looking to change my code to Late binding, although I can not seem to get this working.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Dim UserDate As Date
Dim StartDate As Date
Dim EndDate As Date
Dim ForStartDate As Date
Dim ForEndDate As Date
Dim rsCount As Integer
If IsNull(Me.TxTStartDate.Value) Or IsNull(Me.TxTEndDate.Value) Then
MsgBox "Please Enter Start and End Dates for the Report to be Processed", vbInformation, "Utilergy"
Else
StartDate = Me.TxTStartDate.Value
EndDate = Me.TxTEndDate.Value
ForStartDate = Format(StartDate, "DD/MM/YYYY")
ForEndDate = Format(EndDate, "DD/MM/YYYY")
Dim strSQL As String
strSQL = "SELECT tblProjects.ProjectDateModfied, tblProjects.ProjectNumber, tblWorks.WorkNumber, tblOrders.OrderNumber, autotblProjectStatus.ProjectStatus," & vbCrLf
strSQL = strSQL & "tblWorks.WorkAddressNameNumber & ' ' & tblWorks.WorkAddressStreet & ' ' & tblWorks.WorkAddressTown & ' ' & tblWorks.WorkAddressPostcode, tblProjects.ProjectRecievedDate," & vbCrLf
strSQL = strSQL & "tblPreSiteSurveys.PreSiteSurveyDate, tblProjects.ProjectProvisionalStartDate, tblProjects.ProjectCompletionDate, tblProjects.ProjectActualStartDate," & vbCrLf
strSQL = strSQL & "tblProjects.ProjectActualCompletionDate, tblPreSiteSurveys.PreSiteSurveyStopTheClockDate, tblPreSiteSurveys.PreSiteSurveyStopTheClockReason," & vbCrLf
strSQL = strSQL & "tblPreSiteSurveys.PreSiteSurveyStopTheClockComment, tblAsBuiltDetails.AsBuiltDetailsDateSubmittedToENW, tblPreSiteSurveys.PreSiteSurveyContactName," & vbCrLf
strSQL = strSQL & "tblPreSiteSurveys.PreSiteSurveyContactNumber, tblAsBuiltDetails.AsBuiltDetailsDateJointed" & vbCrLf
strSQL = strSQL & "FROM (((tblPreSiteSurveys INNER JOIN (autotblProjectStatus INNER JOIN tblProjects ON autotblProjectStatus.[ProjectStatusID] = tblProjects.[ProjectStatus])" & vbCrLf
strSQL = strSQL & "ON tblPreSiteSurveys.[PreSiteSurveyID] = tblProjects.[FKPreSiteSurveyID]) INNER JOIN tblWorks ON tblProjects.[ProjectID] = tblWorks.[FKProjectID])" & vbCrLf
strSQL = strSQL & "LEFT OUTER JOIN tblAsBuiltDetails ON tblWorks.[WorkID] = tblAsBuiltDetails.[FKWorkID]) INNER JOIN tblOrders ON tblWorks.WorkID = tblOrders.FKWorkID" & vbCrLf
strSQL = strSQL & "WHERE tblProjects.ProjectDateModfied between #" & ForStartDate & "# AND #" & ForEndDate & "# ;"
'counts the number of records for Cell F7 of thhe spreadsheet
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
rsCount = rs.RecordCount
'Start a new workbook in Excel
Dim oApp As New Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oBook = oApp.Workbooks.Open("U:\x\Reports\UtilergyMasterUpdate.xlsm")
Set oSheet = oBook.Worksheets(1)
ForStartDate = Format(StartDate, "DD/MM/YYYY")
ForEndDate = Format(EndDate, "DD/MM/YYYY")
'Add the field names in row 1
' Dim i As Integer
' Dim iNumCols As Integer
' iNumCols = rs.Fields.Count
' For i = 1 To iNumCols
' oSheet.Cells(2, i).Value = rs.Fields(i - 1).Name
' Next
'Add the data starting at cell A2
oSheet.Range("A3").CopyFromRecordset rs
' oSheet.Range("E13").Value = ForStartDate
' oSheet.Range("E14").Value = ForEndDate
oApp.Visible = True
oApp.UserControl = True
'Close the Database and Recordset
rs.Close
How would this code be changed please for late binding rules?
Thanks in advance.:banghead:
I have wrote some code to export to excel, which work in access 2013 full version, but testing the system out in a runtime environment the system crashes because I am referencing the excel library in the VBA references.
I have now removed the reference and I am looking to change my code to Late binding, although I can not seem to get this working.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Dim UserDate As Date
Dim StartDate As Date
Dim EndDate As Date
Dim ForStartDate As Date
Dim ForEndDate As Date
Dim rsCount As Integer
If IsNull(Me.TxTStartDate.Value) Or IsNull(Me.TxTEndDate.Value) Then
MsgBox "Please Enter Start and End Dates for the Report to be Processed", vbInformation, "Utilergy"
Else
StartDate = Me.TxTStartDate.Value
EndDate = Me.TxTEndDate.Value
ForStartDate = Format(StartDate, "DD/MM/YYYY")
ForEndDate = Format(EndDate, "DD/MM/YYYY")
Dim strSQL As String
strSQL = "SELECT tblProjects.ProjectDateModfied, tblProjects.ProjectNumber, tblWorks.WorkNumber, tblOrders.OrderNumber, autotblProjectStatus.ProjectStatus," & vbCrLf
strSQL = strSQL & "tblWorks.WorkAddressNameNumber & ' ' & tblWorks.WorkAddressStreet & ' ' & tblWorks.WorkAddressTown & ' ' & tblWorks.WorkAddressPostcode, tblProjects.ProjectRecievedDate," & vbCrLf
strSQL = strSQL & "tblPreSiteSurveys.PreSiteSurveyDate, tblProjects.ProjectProvisionalStartDate, tblProjects.ProjectCompletionDate, tblProjects.ProjectActualStartDate," & vbCrLf
strSQL = strSQL & "tblProjects.ProjectActualCompletionDate, tblPreSiteSurveys.PreSiteSurveyStopTheClockDate, tblPreSiteSurveys.PreSiteSurveyStopTheClockReason," & vbCrLf
strSQL = strSQL & "tblPreSiteSurveys.PreSiteSurveyStopTheClockComment, tblAsBuiltDetails.AsBuiltDetailsDateSubmittedToENW, tblPreSiteSurveys.PreSiteSurveyContactName," & vbCrLf
strSQL = strSQL & "tblPreSiteSurveys.PreSiteSurveyContactNumber, tblAsBuiltDetails.AsBuiltDetailsDateJointed" & vbCrLf
strSQL = strSQL & "FROM (((tblPreSiteSurveys INNER JOIN (autotblProjectStatus INNER JOIN tblProjects ON autotblProjectStatus.[ProjectStatusID] = tblProjects.[ProjectStatus])" & vbCrLf
strSQL = strSQL & "ON tblPreSiteSurveys.[PreSiteSurveyID] = tblProjects.[FKPreSiteSurveyID]) INNER JOIN tblWorks ON tblProjects.[ProjectID] = tblWorks.[FKProjectID])" & vbCrLf
strSQL = strSQL & "LEFT OUTER JOIN tblAsBuiltDetails ON tblWorks.[WorkID] = tblAsBuiltDetails.[FKWorkID]) INNER JOIN tblOrders ON tblWorks.WorkID = tblOrders.FKWorkID" & vbCrLf
strSQL = strSQL & "WHERE tblProjects.ProjectDateModfied between #" & ForStartDate & "# AND #" & ForEndDate & "# ;"
'counts the number of records for Cell F7 of thhe spreadsheet
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
rsCount = rs.RecordCount
'Start a new workbook in Excel
Dim oApp As New Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
Set oBook = oApp.Workbooks.Open("U:\x\Reports\UtilergyMasterUpdate.xlsm")
Set oSheet = oBook.Worksheets(1)
ForStartDate = Format(StartDate, "DD/MM/YYYY")
ForEndDate = Format(EndDate, "DD/MM/YYYY")
'Add the field names in row 1
' Dim i As Integer
' Dim iNumCols As Integer
' iNumCols = rs.Fields.Count
' For i = 1 To iNumCols
' oSheet.Cells(2, i).Value = rs.Fields(i - 1).Name
' Next
'Add the data starting at cell A2
oSheet.Range("A3").CopyFromRecordset rs
' oSheet.Range("E13").Value = ForStartDate
' oSheet.Range("E14").Value = ForEndDate
oApp.Visible = True
oApp.UserControl = True
'Close the Database and Recordset
rs.Close
How would this code be changed please for late binding rules?
Thanks in advance.:banghead:
Last edited: