I am continuing to run into this issue so, I am hoping someone else has run into it and knows how to resolve it.
I have a command on a form that runs a SQL statment in a recordset and exports it to Excel. This works 99% of the time, the other 1% of the time the SQL statment loads records (1889 records) when run as an Access query but does not load records when opened as a recordset (rst.BOF=true And rst.EOF=true). I included the full code to aide in understanding, but it stops running at Line 115 after it determines the recordset is empty.
I have tried changing my Cursor Type for the recordset with the same results. The last time I ran into this I resolved it by making a temporary table and having the recordset pull from the temporary table then deleting the temporary table, and I can go that route again if I have to but I would like to understand what is causing this and how to resolve it in the future.
Thank you for taking the time to read my post and any help you can provide.
I have a command on a form that runs a SQL statment in a recordset and exports it to Excel. This works 99% of the time, the other 1% of the time the SQL statment loads records (1889 records) when run as an Access query but does not load records when opened as a recordset (rst.BOF=true And rst.EOF=true). I included the full code to aide in understanding, but it stops running at Line 115 after it determines the recordset is empty.
I have tried changing my Cursor Type for the recordset with the same results. The last time I ran into this I resolved it by making a temporary table and having the recordset pull from the temporary table then deleting the temporary table, and I can go that route again if I have to but I would like to understand what is causing this and how to resolve it in the future.
Code:
Dim conn As ADODB.Connection
Dim stPath As String
Dim rst As ADODB.Recordset
Dim sSQL As String
Dim strErrMsg As String
Dim stName As String
Dim oxl As Excel.Application
Dim sFilename As String
Dim varXLSName As Variant
Dim sSelect As String
Dim sFrom As String
Dim sWhere As String
Dim sOrder As String
Dim stDvlpYr As Double
Dim stWrkGrp As String
Dim stEmp As String
Dim stPrjtID As String
Dim stType As String
Dim stVrsn As String
If IsNull([Forms]![frmEstRpt_Analysis]![cmdDvlpYr]) Then
MsgBox "Please select the development year before exporting to Excel", vbExclamation + vbOKOnly, "Missing Required Information"
Exit Sub
Else
stDvlpYr = [Forms]![frmEstRpt_Analysis]![cmdDvlpYr]
End If
If IsNull([Forms]![frmEstRpt_Analysis]![cmbWrkGrp]) Then
stWrkGrp = "*"
Else
stWrkGrp = [Forms]![frmEstRpt_Analysis]![cmbWrkGrp]
End If
If IsNull([Forms]![frmEstRpt_Analysis]![cmbName]) Then
stEmp = "*"
Else
stEmp = [Forms]![frmEstRpt_Analysis]![cmbName]
End If
If IsNull([Forms]![frmEstRpt_Analysis]![cmbWFSbprjtType]) Then
stType = "*"
Else
stType = [Forms]![frmEstRpt_Analysis]![cmbWFSbprjtType]
End If
If IsNull([Forms]![frmEstRpt_Analysis]![cmbProjectID]) Then
stPrjtID = "*"
Else
stPrjtID = [Forms]![frmEstRpt_Analysis]![cmbProjectID]
End If
If ([Forms]![frmEstRpt_Analysis]![cmdDvlpYr]) < 2014 Then
stVrsn = 3
Else
stVrsn = 1
End If
' stName = "qrptEst_LaborHoursAnalysis"
' sFilename = "LaborHours" & "_" & Year(Date) & Month(Date) & Day(Date)
' strErrMsg = ""
' Set the string to the path of your database
stPath = CurrentDb.Name
Debug.Print stPath
' Open connection to the database
Set conn = New ADODB.Connection
conn.Provider = "Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & stPath & ";"
conn.Open
sSelect = "SELECT tCPSubprojects.SubTotalGroup" _
& ", tDvlpYr.Yr1" _
& ", tDvlpYr.Yr2" _
& ", tEmpPrjtHrs.VersionID AS Version" _
& ", tEmpPrjtHrs.OrgID" _
& ", [tEmpPrjtHrs]![OrgID] & IIf([ztlkpCPLowOrgCrossWalk]![LowOrgDescription] Is Null,Null,'-' & [ztlkpCPLowOrgCrossWalk]![LowOrgDescription]) AS Workgroup" _
& ", tEmpPrjtHrs.PosnNmbr" _
& ", tEmpPrjtHrs.EmpNmbr" _
& ", tEmpPrjtHrs.Employee" _
& ", IIf([tCPSubprojects]![SubTotalGroup]='OH Details',Null,[tEmpPrjtHrs]![WFSbprjtType]) AS [Project Type]" _
& ", tEmpPrjtHrs.WFSubprojectID AS [Project ID]" _
& ", tEmpPrjtHrs.WFSbprjtTitle AS Project" _
& ", Nz([tEmpPrjtHrs]![Yr1Hours],0) AS Yr1Hours" _
& ", Nz([tEmpPrjtHrs]![Yr2Hours],0) AS Yr2Hours" _
& ", [tEmpPrjtHrs]![Yr1Hours]*[tEmpPrjtHrs]![Yr1LoadedRate] AS Yr1Amt" _
& ", [tEmpPrjtHrs]![Yr2Hours]*[tEmpPrjtHrs]![Yr2LoadedRate] AS Yr2Amt" _
& ", [tEmpPrjtHrs]![Yr1Hours]/([tVersion]![VrsnHrs]*[tEmpPrjtHrs]![FTEPrct]) AS Yr1Prct" _
& ", [tEmpPrjtHrs]![Yr2Hours]/([tVersion]![VrsnHrs]*[tEmpPrjtHrs]![FTEPrct]) AS Yr2Prct" _
& ", tEmpPrjtHrs.Assumptions" _
& ", tEmpPrjtHrs.DvlpYr" _
& ", tEmpPrjtHrs.WFSbprjtType"
sFrom = "FROM tDvlpYr INNER JOIN (tCPSubprojects INNER JOIN (ztlkpCPLowOrgCrossWalk " _
& " INNER JOIN (tVersion INNER JOIN tEmpPrjtHrs ON (tVersion.VersionID = tEmpPrjtHrs.VersionID) " _
& " AND (tVersion.DvlpYr = tEmpPrjtHrs.DvlpYr))" _
& " ON ztlkpCPLowOrgCrossWalk.SummitLowOrg = tEmpPrjtHrs.OrgID) ON (tCPSubprojects.WFSubprojectID = tEmpPrjtHrs.WFSubprojectID) " _
& " AND (tCPSubprojects.DvlpYr = tEmpPrjtHrs.DvlpYr)) ON tDvlpYr.DvlpYr = tVersion.DvlpYr"
sWhere = "WHERE (((tEmpPrjtHrs.VersionID) = " & stVrsn & ")" _
& " And((tEmpPrjtHrs.OrgID) Like '" & stWrkGrp & "')" _
& " AND ((tEmpPrjtHrs.PosnNmbr) Like '" & stEmp & "')" _
& " AND ((tEmpPrjtHrs.WFSubprojectID) Like '" & stPrjtID & "')" _
& " And ((Nz([tEmpPrjtHrs]![Yr1Hours], 0)) <> 0) " _
& " AND ((tEmpPrjtHrs.DvlpYr) = " & stDvlpYr & ") " _
& " AND ((tEmpPrjtHrs.WFSbprjtType) Like '" & stType & "'))" _
& " Or (((tEmpPrjtHrs.VersionID) = " & stVrsn & ")" _
& " And((tEmpPrjtHrs.OrgID) Like '" & stWrkGrp & "')" _
& " AND ((tEmpPrjtHrs.PosnNmbr) Like '" & stEmp & "')" _
& " AND ((tEmpPrjtHrs.WFSubprojectID) Like '" & stPrjtID & "')" _
& " And ((Nz([tEmpPrjtHrs]![Yr2Hours], 0)) <> 0) " _
& " AND ((tEmpPrjtHrs.DvlpYr) = " & stDvlpYr & ") " _
& " AND ((tEmpPrjtHrs.WFSbprjtType) Like '" & stType & "'))"
' Open recordset
Set rst = New ADODB.Recordset
sSQL = sSelect & vbCrLf & sFrom & vbCrLf & sWhere & ";"
' DoCmd.Hourglass True
Debug.Print sSQL
rst.Open sSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
If (rst.BOF And rst.EOF) Then
MsgBox "No Data Available to Export", vbOKOnly, "No Data"
DoCmd.Hourglass False
Exit Sub
End If
rst.MoveLast
Debug.Print rst.RecordCount
lngRow = rst.RecordCount + 1
' Create an instance of Excel and add a workbook
Set xlApp = New Excel.Application
Set xlWb = xlApp.Workbooks.Add
Set xlWs = xlWb.Worksheets("Sheet1")
'Display Excel and give user control of Excel's lifetime
xlApp.Visible = True
xlApp.UserControl = True
' Copy field names to the first row of the worksheet
fldCount = rst.Fields.Count
For iCol = 1 To fldCount
xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
Next
rst.MoveFirst
' Copy the recordset to the worksheet, starting in cell A2
xlWs.Range("A2", "XFD" & lngRow).CopyFromRecordset rst
' Auto-fit the column widths and row heights
xlApp.Selection.CurrentRegion.Columns.AutoFit
xlApp.Selection.CurrentRegion.Rows.AutoFit
' xlWs.Range("L2", "L" & lngRow).NumberFormat = "$#,###;($#,###)[Red];-;-" 'Format the Amounts
' xlWs.Range("M2", "M" & lngRow).NumberFormat = "#,###;(#,###)[Red];-;-" 'Format the Hours
'Clean up
rst.Close
Set rst = Nothing
conn.Close
Set conn = Nothing
Comment