No. The problem is still there.
When I re-checked my codes, I mistakenly placed a Breakpoint BEFORE the line
[lcSQL = "TRANSFORM Sum(NetSales) AS >SumOfNetSales"]. That's why I was able
to get the correct recordcount. Probably I was too excited then.
Replacing RS with rsTransform does not work [Dim rsTransform As New
ADODB.Recordset].
The reason why I took a long time to update this post was because initially,
when I thought replacing 'rsTransform' with 'RS' would work after testing the
vba code in my Test.mdb database, I went to my production .mdb database and
replace all the 'RS' with 'rsTransform'. There are several hundred lines of
codes.
When I checked my report, the control figures did not tally. So I back
tracked to look at the vba codes in Test.mdb. The recordcount is still ONE
less.
I'm posting my latest codes here. Pls ignore the name 'rsTransform2'.
Because 'rsTransform' did not work, I was testing my luck on 'rsTransform2'.
@@@@@@@@@@@
1) General Declarations section:
Option Compare Database
Option Explicit
Dim Cn As ADODB.Connection
Dim Cn2 As ADODB.Connection
'Dim RS As New ADODB.Recordset
'==================
'To be deleted later.
'Dim rsTransform As New ADODB.Recordset
Dim rsTransform2 As New ADODB.Recordset
'==================
Dim Conn As String, Conn2 As String
Dim CPw1 As ADODB.Command
Dim QSQL As String
Dim gcMsgHdr$
Dim gcMsg$
Dim i As Long, j As Long
Dim fnYN As Long
2) ProcessOENetSale procedure():
Private Sub ProcessOENetSales()
Dim lcSQL$
Dim lcORDERENTRY$
Dim lctbNS306A$, lctbNS306B$
Dim lcDept$, lcBr$, lcNetSales$, lcPcls$, lcInvDate$, lcDeptID$
Dim lnFldCount As Long, lcFldName$
Dim lc201$, lc202$, lc203$, lc204$, lc205$, lc206$, lc207$, lc208$, lc209$,
lc210$, lc211$, lc212$
Dim lc2Dept$, lc2DeptID$, lc2Pcls$, lc2Br$
'=======================
'This section is for testing purpose only. To be deleted later.
Dim lnRecordCount As Long
'=======================
gcMsgHdr = "Procedure: ProcessOENetSales"
lcORDERENTRY = "ORDER ENTRY"
lctbNS306A = "tbNS306A"
lctbNS306B = "tbNS306B"
'=================================================
'JCQNS10 query.
lcSQL = "SELECT Left([RaCustTxnLines].[INTERFACE_LINE_ATTRIBUTE2],3) AS
Dept, "
lcSQL = lcSQL & " Mid([RaCustTxnLines].[INTERFACE_LINE_ATTRIBUTE2],5,2) AS
Br, "
lcSQL = lcSQL & " Sum(RaCustTxnLines.revenue_amount) AS NetSales,
tbItemMs.Pcls, "
lcSQL = lcSQL & " left([LAST_UPDATE_DATE],6) AS InvDate, tbItemMs.DeptID"
lcSQL = lcSQL & " FROM RaCustTxnLines LEFT JOIN tbItemMs ON "
lcSQL = lcSQL & " (RaCustTxnLines.INTERFACE_LINE_ATTRIBUTE10 =
tbItemMs.DeptID) "
lcSQL = lcSQL & " AND (RaCustTxnLines.INVENTORY_ITEM_ID = tbItemMs.ItemID)"
lcSQL = lcSQL & " WHERE (((RaCustTxnLines.INTERFACE_LINE_CONTEXT) = '" &
lcORDERENTRY & "'))"
lcSQL = lcSQL & " GROUP BY
Left([RaCustTxnLines].[INTERFACE_LINE_ATTRIBUTE2],3),"
lcSQL = lcSQL & " Mid([RaCustTxnLines].[INTERFACE_LINE_ATTRIBUTE2],5,2),"
lcSQL = lcSQL & " tbItemMs.Pcls, left([LAST_UPDATE_DATE],6), tbItemMs.DeptID"
'---------------------
Set rsTransform2 = New ADODB.Recordset
rsTransform2.Open lcSQL, Conn2, adOpenForwardOnly, adLockReadOnly
If rsTransform2.BOF Or rsTransform2.EOF Then
rsTransform2.Close
Beep
MsgBox "No record!"
Exit Sub
End If
Cn2.Execute "Delete * from " & lctbNS306A
rsTransform2.MoveFirst
Do While Not rsTransform2.EOF()
lcDept = ReplSglDblQte("" & rsTransform2("Dept"))
lcBr = ReplSglDblQte("" & rsTransform2("Br"))
If IsNumeric(rsTransform2("NetSales")) Then
lcNetSales = rsTransform2("NetSales")
Else
lcNetSales = "0"
End If
lcPcls = ReplSglDblQte("" & rsTransform2("Pcls"))
If Len(Trim(rsTransform2("InvDate"))) = 0 Then
lcInvDate = ""
Else
lcInvDate = rsTransform2("InvDate")
End If
lcDeptID = ReplSglDblQte("" & rsTransform2("DeptID"))
lcSQL = "insert into " & lctbNS306A & " (Dept,Br, NetSales, Pcls,
InvDate, DeptID) values ('"
lcSQL = lcSQL & lcDept & "','" & lcBr & "',"
lcSQL = lcSQL & lcNetSales & ",'" & lcPcls & "','"
lcSQL = lcSQL & lcInvDate & "','" & lcDeptID & "')"
Cn2.Execute lcSQL
i = i + 1
lblStatus.Caption = "No. of records appended (" & lctbNS306A & ") : " &
Str(j)
DoEvents
rsTransform2.MoveNext
Loop
rsTransform2.Close
'Beep
'For testing purpose, I'm commenting the Msgbox line below.
'MsgBox "Cross-tab tbNS308 table in progress. Press any key."
'==========================
'This section is for testing purpose only. To be deleted later.
'I have commented out the MsgBox line.
'I have added 2 breakpoints at these 2 lines:
'(a) lcSQL = "TRANSFORM Sum(NetSales) AS SumOfNetSales"
'(b) lnRecordCount = RS.RecordCount
'
'==> TEST 1
'If I place a breakpoint at the line below [lcSQL = "TRANSFORM Sum(NetSales)
AS SumOfNetSales"] and then press F5 to continue,
'the lnRecordCount = 96 (correct recordcount) (at line [lnRecordCount =
RS.RecordCount].
'==> TEST 2
'If I DO NOT place a breakpoint at the line below [lcSQL = "TRANSFORM
Sum(NetSales) AS SumOfNetSales"],
'the lnRecordCount = 95 (correct recordcount LESS 1 record) (at line
[lnRecordCount = RS.RecordCount].
'
'Looks like vba.ado prefers a little rest before continuing with its journey!
'-----------------
'DoEvents
lcSQL = "TRANSFORM Sum(NetSales) AS SumOfNetSales"
lcSQL = lcSQL & " SELECT DeptID, Dept, Pcls, Br"
lcSQL = lcSQL & " FROM " & lctbNS306A
lcSQL = lcSQL & " GROUP BY DeptID, Dept, Pcls, Br"
lcSQL = lcSQL & " ORDER BY Right([InvDate],2)"
lcSQL = lcSQL & " PIVOT Right([InvDate],2)"
'------------------
'Set RS = New ADODB.Recordset
Set rsTransform2 = New ADODB.Recordset
'Dim rsTransform2 As New ADODB.Recordset
'RS.Open lcSQL, Conn2, adOpenKeyset, adLockReadOnly
rsTransform2.Open lcSQL, Conn2, adOpenKeyset, adLockReadOnly
'RS.Open lcSQL, Conn2, adOpenForwardOnly, adLockReadOnly
If rsTransform2.BOF Or rsTransform2.EOF Then
rsTransform2.Close
Beep
MsgBox "No record!"
Exit Sub
End If
'=============================
'This section is for testing purpose only. To be deleted later.
'I'm placing a Breakpoint at line [lnRecordCount = RS.RecordCount].
rsTransform2.MoveLast
lnRecordCount = rsTransform2.RecordCount
rsTransform2.MoveFirst
'=============================
Cn2.Execute "Delete * from " & lctbNS306B
lnFldCount = rsTransform2.Fields.Count
j = 0
rsTransform2.MoveFirst
Do While Not rsTransform2.EOF()
lc201 = "0"
lc202 = "0"
lc203 = "0"
lc204 = "0"
lc205 = "0"
lc206 = "0"
lc207 = "0"
lc208 = "0"
lc209 = "0"
lc210 = "0"
lc211 = "0"
lc212 = "0"
For i = 0 To lnFldCount - 1
lcFldName = rsTransform2.Fields(i).Name
Select Case lcFldName
Case "Dept"
lc2Dept = "" & rsTransform2.Fields(i).Value
Case "DeptID"
lc2DeptID = "" & rsTransform2.Fields(i).Value
Case "Pcls"
lc2Pcls = "" & rsTransform2.Fields(i).Value
Case "Br"
lc2Br = "" & rsTransform2.Fields(i).Value
Case "01"
If IsNull(rsTransform2.Fields(i).Value) Then
lc201 = "0"
Else
lc201 = rsTransform2.Fields(i).Value
End If
Case "02"
If IsNull(rsTransform2.Fields(i).Value) Then
lc202 = "0"
Else
lc202 = rsTransform2.Fields(i).Value
End If
Case "03"
If IsNull(rsTransform2.Fields(i).Value) Then
lc203 = "0"
Else
lc203 = rsTransform2.Fields(i).Value
End If
Case "04"
If IsNull(rsTransform2.Fields(i).Value) Then
lc204 = "0"
Else
lc204 = rsTransform2.Fields(i).Value
End If
Case "05"
If IsNull(rsTransform2.Fields(i).Value) Then
lc205 = "0"
Else
lc205 = rsTransform2.Fields(i).Value
End If
Case "06"
If IsNull(rsTransform2.Fields(i).Value) Then
lc206 = "0"
Else
lc206 = rsTransform2.Fields(i).Value
End If
Case "07"
If IsNull(rsTransform2.Fields(i).Value) Then
lc207 = "0"
Else
lc207 = rsTransform2.Fields(i).Value
End If
Case "08"
If IsNull(rsTransform2.Fields(i).Value) Then
lc208 = "0"
Else
lc208 = rsTransform2.Fields(i).Value
End If
Case "09"
If IsNull(rsTransform2.Fields(i).Value) Then
lc209 = "0"
Else
lc209 = rsTransform2.Fields(i).Value
End If
Case "10"
If IsNull(rsTransform2.Fields(i).Value) Then
lc210 = "0"
Else
lc210 = rsTransform2.Fields(i).Value
End If
Case "11"
If IsNull(rsTransform2.Fields(i).Value) Then
lc211 = "0"
Else
lc211 = rsTransform2.Fields(i).Value
End If
Case "12"
If IsNull(rsTransform2.Fields(i).Value) Then
lc212 = "0"
Else
lc212 = rsTransform2.Fields(i).Value
End If
End Select
Next
lcSQL = "insert into " & lctbNS306B & " (Dept, DeptID, Pcls, Br,"
lcSQL = lcSQL &
"MS01,MS02,MS03,MS04,MS05,MS06,MS07,MS08,MS09,MS10,MS11,MS12) VALUES ('"
lcSQL = lcSQL & lc2Dept & "','" & lc2DeptID & "','"
lcSQL = lcSQL & lc2Pcls & "','" & lc2Br & "',"
lcSQL = lcSQL & lc201 & "," & lc202 & "," & lc203 & ","
lcSQL = lcSQL & lc204 & "," & lc205 & "," & lc206 & ","
lcSQL = lcSQL & lc207 & "," & lc208 & "," & lc209 & ","
lcSQL = lcSQL & lc210 & "," & lc211 & "," & lc212 & ")"
Cn2.Execute lcSQL
j = j + 1
lblStatus.Caption = "No. of records appended (" & lctbNS306B & ") : " &
Str(j)
DoEvents
rsTransform2.MoveNext
Loop
rsTransform2.Close
End Sub
@@@@@@@@@@@
John Chee said:
In my ProcessOENetSales() procedure, I replace all the RS with rsTransform.
It just works like magic. Now it's giving the correct recordcount of 96
without me putting the Msgbox command.
It looks like access.vba does not like RS; it prefers a longer name like
rsTransform.
Thanks a lot, John. You solve my problem.