Link to home
Start Free TrialLog in
Avatar of wlwebb
wlwebbFlag for United States of America

asked on

Access - Loop not creating expected number of new recs in a CurrentDb.Execute INSERT INTO

Hello All,
I am having trouble with a CurrentDb.Execute INSERT INTO that isn't creating the correct number of new records.
This is a tabbed form with multiple tabs each having a subform.

On the first tab I have an Unbound textbox to count the number of records in a query.  That textbox is named [txtCntActiveCurrencyDenominations]
The Control Source for that box is =DCount("[qry_Denomination_Currency_Active]![DenominationID]","[qry_Denomination_Currency_Active]","[qry_Denomination_Currency_Active]![DenominationID]")

The SQL of that query which is counts is
SELECT Denomination.DenominationID, Denomination.Denomination, Denomination.DenominationMultiple, Denomination.DenominationTypeID, Denomination.Active, DCount("DenominationID","qry_Denomination_Currency_Active","DenominationID <= " & [DenominationID]) AS CurrencyActiveDenominationSeq
FROM Denomination
WHERE (((Denomination.DenominationTypeID)=2) AND ((Denomination.Active)=-1));

Open in new window


Now then what I am expecting to happen in this case is that it will insert new records with a DenomintionID of 2, 3, 4, 5, 6 & 7
However, it is Inserting 2, 3, 4, 5 & 6  (NOT 7)

The code that Executes all of this is
Private Sub GenerateLVLMachineLines2(IsMachinePull As Boolean)
Dim bytCounter As Byte, lngMyRptgSeqID As Long, lngMyShiftRptgLVLCtlID As Long, i As Integer, z As Integer
Dim strSQL As String

lngMyRptgSeqID = GetMyShiftSeqID()
lngMyShiftRptgLVLCtlID = GetMyShiftRptgLVLCtlID()

    i = 1   ' This counter is for Inserting Active Currency Seq to Machine Pull tbl
    z = DMax("ShiftRptgMachPullCountCtlID", "ShiftReportingMachinePullCountCtl", "ShiftRptgMachPullCountCtlID")



    Do Until bytCounter = Me.txtNbrMachines
            CurrentDb.Execute "INSERT INTO ShiftReportingLVL (ShiftRptgLVLCtlID, LVLPositionNbr, RptgSeqID, MachinePulled) VALUES (" & lngMyShiftRptgLVLCtlID & "," & bytCounter + 1 & "," & lngMyRptgSeqID & "," & True & ")", dbFailOnError
            bytCounter = bytCounter + 1
    Loop
    
    Do Until i = Forms![frm_DataReporting]![LVLReportingTypeSelect]![txtCntActiveCurrencyDenominations]
        strSQL = "INSERT INTO ShiftReportingMachinePullDetails (ShiftRptgMachPullCountCtlID, DenominationID) SELECT " & z & ", DenominationID FROM [qry_Denomination_Currency_Active] WHERE [CurrencyActiveDenominationSeq]= " & i
        CurrentDb.Execute strSQL, dbFailOnError
        i = i + 1
    Loop
                                            
End Sub

Open in new window

Avatar of wlwebb
wlwebb
Flag of United States of America image

ASKER

Just as an FYI, when I look at the form that unbound text box Dcount result is 6.
ASKER CERTIFIED SOLUTION
Avatar of IrogSinta
IrogSinta
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In addition, change these for clarity:
line 6
z = DMax("ShiftRptgMachPullCountCtlID", "ShiftReportingMachinePullCountCtl")
textbox controlsource
=DCount("*","[qry_Denomination_Currency_Active]")
Avatar of wlwebb

ASKER

Irog......I think you meant line 9 but,
Didn't understand that last comment.......
Do I add textbox controlsource as a new line of code???? or am I replacing line 9
You're right, I meant replace line 9.  The 2nd code I gave you was a shortened version of what you have in your textbox named txtCntActiveCurrencyDenominations.
Avatar of wlwebb

ASKER

Ok... I understand........Thanks
Avatar of wlwebb

ASKER

Thank you as always!!!  And thank you for the additional comment!