I have built a similar function:
varAddAnotherCode = (MsgBox("Price code " & strPriceCode & " added successfully." _
& vbNewLine & "Would you like to add another Price Code?", vbYesNo, "New Price Codes."))
If varAddAnotherCode = vbYes Then
With Me
.txtPriceCode = ""
.cboClass = ""
.txtDescription = ""
.txtPriceCode.SetFocus
End With
Else
DoCmd.Close acForm, Me.Name
End If
As you can see, I provide a confirmation for a new price code and allow the user to add another. I then clear the fields rather than re-opening the form. Maybe you could employ a similar method of manipulating your fields. Please note I am using an unbound form for data entry.
The full code of the event is here:
Private Sub cmdCommit_Click()
Dim intPC_ID As Integer
Dim strPriceCode, strClass As String
Dim strSQL_AddPriceCode As String
Dim varDescription As Variant
Dim strErrMessage As String
Dim blnError As Boolean
Dim varAddAnotherCode As Variant
intPC_ID = DMax("PC_ID", "PriceCodes") + 1
blnError = False
strErrMessage = ""
With Me
strPriceCode = Nz(.txtPriceCode, "Error")
strClass = Nz(.cboClass, "Error")
varDescription = Nz(.txtDescription, "Error")
End With
If Len(strPriceCode) > 50 Then
blnError = True
strErrMessage = "The Price Code must be fifty characters or less, including spaces"
ElseIf strPriceCode = "Error" Then
blnError = True
strErrMessage = "Please enter the Price Code"
ElseIf Nz(DLookup("PC_ID", "PriceCodes", "[PriceCode] = '" & strPriceCode & "' "), 0) > 0 Then
'Price Code already exists
blnError = True
strErrMessage = "This Price Code already exists, please try again."
End If
If strClass = "Error" Then
blnError = True
If Len(strErrMessage) < 1 Then
strErrMessage = "Please select a class from the drop down box."
Else
strErrMessage = strErrMessage & vbNewLine & "Please select a class from the drop down box."
End If
End If
If varDescription = "Error" Then
blnError = True
If Len(strErrMessage) < 1 Then
strErrMessage = "Please enter a description for this Price Code."
Else
strErrMessage = strErrMessage & vbNewLine & "Please enter a description for this Price Code."
End If
Else
Replace varDescription, Chr(34), ""
End If
If blnError = True Then
'present error messages
MsgBox strErrMessage, vbOKOnly, "Error."
Else
strSQL_AddPriceCode = "INSERT INTO PriceCodes (PC_ID,PriceCode,Class,Description)" _
& "SELECT " & intPC_ID & ", '" & strPriceCode & "', '" & strClass & "', " & Chr(34) & varDescription & Chr(34)
'DoCmd.OpenForm "sqltester"
'Form_sqltester.txtSQL = strSQL_AddPriceCode
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL_AddPriceCode
DoCmd.SetWarnings True
varAddAnotherCode = (MsgBox("Price code " & strPriceCode & " added successfully." _
& vbNewLine & "Would you like to add another Price Code?", vbYesNo, "New Price Codes."))
If varAddAnotherCode = vbYes Then
With Me
.txtPriceCode = ""
.cboClass = ""
.txtDescription = ""
.txtPriceCode.SetFocus
End With
Else
DoCmd.Close acForm, Me.Name
End If
End If
End Sub