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. -
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
-
-
Thank you for taking the time to read my post and any help you can provide.
5 5508 zmbd 5,501
Expert Mod 4TB
1) Show us the SQL from line 110 when this event happens.
2) Copy line 119 and insert after line 111, include this count with your SQL. Yes, I know, it will not return an accurate count, that's not what I'm after....
3) I'll often use the count value as the test for records rather than the bof/eof as there have been occations of goofyness with these pointers in the past. Because I don't need an accurate count at this point, I use If rs.count then if there is even 1 record in the record set then this will eveluate as true, 0 records evaluates as false within this context and then one can do the movefirst/last thing to get the correct count if needed... may not be the most logical use; however, it works (^_^)
ZMDB,thanks for your reply.
1) the SQL from line 110 is: - 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
-
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
-
WHERE (((tEmpPrjtHrs.VersionID) = 1) And((tEmpPrjtHrs.OrgID) Like '*') AND ((tEmpPrjtHrs.PosnNmbr) Like '*') AND ((tEmpPrjtHrs.WFSubprojectID) Like '*') And ((Nz([tEmpPrjtHrs]![Yr1Hours], 0)) <> 0) AND ((tEmpPrjtHrs.DvlpYr) = 2014) AND ((tEmpPrjtHrs.WFSbprjtType) Like '*')) Or (((tEmpPrjtHrs.VersionID) = 1) And((tEmpPrjtHrs.OrgID) Like '*') AND ((tEmpPrjtHrs.PosnNmbr) Like '*') AND ((tEmpPrjtHrs.WFSubprojectID) Like '*') And ((Nz([tEmpPrjtHrs]![Yr2Hours], 0)) <> 0) AND ((tEmpPrjtHrs.DvlpYr) = 2014) AND ((tEmpPrjtHrs.WFSbprjtType) Like '*'));
-
-
2) Moved Line 119 to after 111 and the count was 0. Still getting records when running the SQL in an Access Query.
Just to close the loop on this, I was able to resolve the issue by changing my wildcards to % and my Like to Alike. I know that I have to do this when I am pulling data from Oracle tables but, this data is being pulled from Access tables in a seperate backend database.
Does it make sense that we would need to use % instead of * to pull this data? Regardless of the sense this was the solution.
Thanks for looking at this problem for me.
zmbd 5,501
Expert Mod 4TB Sign in to post your reply or Sign up for a free account.
Similar topics
by: Bill |
last post by:
I currently connect to two servers. One is IIS, that runs my asp and
sql server, and the other is an AS400, that also cranks out data.
Sometimes I have to wait for data I submit to the as400 to...
|
by: msprygada |
last post by:
I am having a problem with getting a recordset to fill with data in an
Access Data Project from a SQL Server database.
Here is the code example that is in the Access help files that I can
get to...
|
by: jonman |
last post by:
Hello,
I'm a bit of a newbie when it comes to Access (and DB's in general).
I've got a form that allows the assembles a SQL string (that I've tested interactively, and proven that it returns...
|
by: Gem |
last post by:
Hi
I'm struggling with a query returning too many records.
I have 3 related tables.
tblPatients
tblOperations
tblTargets
Each patient in tblPatients can have more than one operation...
|
by: rghollenbeck |
last post by:
Here's my code so far:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim db As DAO.Database
Dim rs As DAO.RecordSet
Dim qdf As QueryDef
Set db =...
|
by: hopi |
last post by:
Hi,
Using this condition: AND instructions NOT LIKE '%RESUBMIT%'
I successfully exclude records where the 'instructions' field contains the word 'RESUBMIT' but it also excludes records...
|
by: lindabaldwin |
last post by:
Hi everyone,
This is what I'm trying to do. I making a database for diabetes insulin dosing. I need the current blood glocose (currentbg) to populate the next record in the previous blood...
|
by: Megalog |
last post by:
Hey all, I have a minor issue I'm trying to figure out (using Access 2007).
I'm using a DAO recordset to update a table ("Writeups"), with the results of a bunch of functions I've written up over...
|
by: zandiT |
last post by:
hello
i created a database that has a form which represents a template. this template is filled out every month. my problem is i don't want to have to copy and paste ever record wen the template is...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |