M
millardgroups
Can you help me understand a result I am getting from a recordset? My
problem is that when I loop through the recordset, some of the values
return as zero or null, even through they are have values in the query.
Here's the background: I have a query that creates summary values for a
report. I have code to that grabs the query results into a recordset,
then exports the values to Excel in a fomat that I need.
Running the code, the recordset returns the values in column 2, versus
the query in column 3:
Title rst qry
L3-D 0 17
L3-D 39 39
L3-D Ready Now 11 11
% L3-D Ready 28% 28%
L3-M 0 9
L3-M 14 14
L4 0 214
L4 Ready Now 0 57
% L4 27%
L4 Ready 1-2 Yr 0 79
% L4 1-2 Yr 37%
L5 121 121
L5 0 323
L5 Ready Now 0 96
% L5 Ready Now 30%
L5 Ready 1-2 Yr 0 112
% L5 1-2 Yr 35%
To test for errors, I changed the query to a make table query instead
of a select. I then changed my code to read from the new table instead
of from the query. The code then successfully pulls all the values
correctly... no zeros/nulls at all.
Can someone help me see what I'm doing wrong? Here's the VBA code:
Private Sub Command1_Click()
Dim rstReportPage1 As ADODB.Recordset
Dim fld As ADODB.Field
Dim intRow As Integer
Set rstReportPage1 = New ADODB.Recordset
'Fill the recordset with the query results
rstReportPage1.Open _
"qryReadinessAll", CurrentProject.Connection
' Launch Excel
Set objXL = CreateObject("Excel.Application")
' Create a new worksheet
objXL.Workbooks.Add
Set objWS = objXL.ActiveSheet
' Copy the data into Excel
For intRow = 0 To rstReportPage1.Fields.Count - 1
Set fld = rstReportPage1.Fields(intRow)
' Field names, placed in the worksheet's column 1
objWS.Cells(intRow + 1, 1) = fld.Name
' Field Values, placed in the worksheet's column 2
objWS.Cells(intRow + 1, 2) = fld.Value
' If the value is a percent, format the Excel field as percent
If (fld.Value < 1) And (fld.Value > 0) Then
objWS.Cells(intRow + 1, 2).NumberFormat = "0%"
End If
Next intRow
'Close the recordset
rstReportPage1.Close
' Make the worksheet visible
objXL.Visible = True
' Don't call the Excel Quit method, so the
' worksheet will hang around after this
' procedure exits
End Sub
Thanks!
Scott
problem is that when I loop through the recordset, some of the values
return as zero or null, even through they are have values in the query.
Here's the background: I have a query that creates summary values for a
report. I have code to that grabs the query results into a recordset,
then exports the values to Excel in a fomat that I need.
Running the code, the recordset returns the values in column 2, versus
the query in column 3:
Title rst qry
L3-D 0 17
L3-D 39 39
L3-D Ready Now 11 11
% L3-D Ready 28% 28%
L3-M 0 9
L3-M 14 14
L4 0 214
L4 Ready Now 0 57
% L4 27%
L4 Ready 1-2 Yr 0 79
% L4 1-2 Yr 37%
L5 121 121
L5 0 323
L5 Ready Now 0 96
% L5 Ready Now 30%
L5 Ready 1-2 Yr 0 112
% L5 1-2 Yr 35%
To test for errors, I changed the query to a make table query instead
of a select. I then changed my code to read from the new table instead
of from the query. The code then successfully pulls all the values
correctly... no zeros/nulls at all.
Can someone help me see what I'm doing wrong? Here's the VBA code:
Private Sub Command1_Click()
Dim rstReportPage1 As ADODB.Recordset
Dim fld As ADODB.Field
Dim intRow As Integer
Set rstReportPage1 = New ADODB.Recordset
'Fill the recordset with the query results
rstReportPage1.Open _
"qryReadinessAll", CurrentProject.Connection
' Launch Excel
Set objXL = CreateObject("Excel.Application")
' Create a new worksheet
objXL.Workbooks.Add
Set objWS = objXL.ActiveSheet
' Copy the data into Excel
For intRow = 0 To rstReportPage1.Fields.Count - 1
Set fld = rstReportPage1.Fields(intRow)
' Field names, placed in the worksheet's column 1
objWS.Cells(intRow + 1, 1) = fld.Name
' Field Values, placed in the worksheet's column 2
objWS.Cells(intRow + 1, 2) = fld.Value
' If the value is a percent, format the Excel field as percent
If (fld.Value < 1) And (fld.Value > 0) Then
objWS.Cells(intRow + 1, 2).NumberFormat = "0%"
End If
Next intRow
'Close the recordset
rstReportPage1.Close
' Make the worksheet visible
objXL.Visible = True
' Don't call the Excel Quit method, so the
' worksheet will hang around after this
' procedure exits
End Sub
Thanks!
Scott