No, sbfrmItemInventory is a pop-up form that is associated
with the main form, so the reference is accurate.
Yes, the code is on sbfrmItemInventory, on a command
button.
The entire code is:
Private Sub cmdSeparate_Click()
Me!txtNoItems = Forms!sbfrmItem!Qty
Me!txtNoItems.SetFocus
CreateRecords (Me!txtNoItems)
End Sub
Private Function CreateRecords(noRecsString As String)
On Error GoTo main_Err
Dim A, noRecs As Integer
noRecs = Val(noRecsString) - 1
If noRecs = 1 Then
Exit Function
End If
If noRecs < 1 Then
MsgBox "Error - you must enter a quantity under Item
Entry."
Exit Function
End If
If noRecs > 50 Then
MsgBox "A safety mechanism prevents the creation of
this many records. Try a smaller number."
Exit Function
End If
DoCmd.RunCommand acCmdSelectRecord
For A = 1 To noRecs
DoCmd.RunCommand acCmdCopy
DoCmd.GoToRecord , , acNewRec
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdPasteAppend
Next A
Exit Function
main_Err:
MsgBox Error$
End Function
I was confused by your prefix "sbfrm", which most people use to
designate a subform. But if you're using that for a main form, popup or
no, that's not the source of your problem.
However, the code snippet you originally posted as having the problem is
not present in the code you have now posted. My guess is that was your
attempt to simplify the problem, but it was misleading. I'm going to
assume that the code you posted more recently is the true code. I do
see a few problems in this code, so I'll post -- untested -- a modified
version of the code that I'd like you to try:
'----- start of suggested code -----
Private Sub cmdSeparate_Click()
If Me.NewRecord then
If Me.Dirty Then
RunCommand acCmdSaveRecord
Else
MsgBox "Can't duplicate an empty record!"
Exit Sub
End If
End If
Me!txtNoItems = Forms!sbfrmItem!Qty
Me!txtNoItems.SetFocus '** DG Note -- don't see why you need this.
CreateRecords (Me!txtNoItems)
End Sub
Private Function CreateRecords(noRecsString As String)
On Error GoTo main_Err
Dim A As Integer
Dim noRecs As Integer
noRecs = Val(noRecsString) - 1
If noRecs = 1 Then
Exit Function
End If
If noRecs < 1 Then
MsgBox "Error - you must enter a quantity under Item Entry."
Exit Function
End If
If noRecs > 50 Then
MsgBox "A safety mechanism prevents the creation of " & _
"this many records. Try a smaller number."
Exit Function
End If
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdCopy
For A = 1 To noRecs
DoCmd.GoToRecord , , acNewRec
DoCmd.RunCommand acCmdPasteAppend
Next A
main_Exit:
Exit Function
main_Err:
MsgBox Error$
Resume main_Exit
End Function
'----- end of suggested code -----