This is my first post at Tek-tips. I am a beginner in vba and this is my first attempt at a loop. My end product needs to be more complex, but starting with a simple version to learn what happens in a loop. My goal is to add multiple records at once to tblBottle.
tblBottle has 3 fields:
AutoID (primary key)
KitNum
BottleNum
In frmAddBottles there are 2 textfields and one btn
KitNum
QtyReceived
When I click the button it mostly works, but I get an extra record in tblButton with the KitNum but a blank BottleNum. If my KitNum was 123 and QtyReceived was 3 then tblBottle recordset looks like this:
AutoID, KitNum, BottleNum
1, 123,
2, 123, 1
3, 123, 2
4, 123, 3
Here is my code
Thanks for any help
tblBottle has 3 fields:
AutoID (primary key)
KitNum
BottleNum
In frmAddBottles there are 2 textfields and one btn
KitNum
QtyReceived
When I click the button it mostly works, but I get an extra record in tblButton with the KitNum but a blank BottleNum. If my KitNum was 123 and QtyReceived was 3 then tblBottle recordset looks like this:
AutoID, KitNum, BottleNum
1, 123,
2, 123, 1
3, 123, 2
4, 123, 3
Here is my code
Code:
'------------------------------------------------------------
' Add Multi Bottles on click
'
'------------------------------------------------------------
Private Sub btnAddMultiRecords_Click()
Dim rs As DAO.Recordset
Dim i As Integer
Dim regNumber As Integer
On Error GoTo HandleError
regNumber = Me.txtBottlesReceived
Set rs = CurrentDb.OpenRecordset("tblBottle")
With rs
For i = 1 To regNumber
.AddNew
![KitNum] = Me.KitNum
![BottleNum] = i
.Update
Next
End With
rs.Close
Set rs = Nothing
ExitHere:
Exit Sub
HandleError:
MsgBox Err.Description
Resume ExitHere
End Sub
Thanks for any help