I tried finding previous posts regarding this issue, but I couldn't find something that worked for me. Im trying to prevent duplicate records based on a PO # (RA_PO_Nbr), entered from a form. I'm trying to throw a 3022 error when the user attempts to enter a record that already exists, but I dont necessarily have to do it this way. Any help would be greatly appreciated.
Thanks,
Ray
Code:
Private Sub cmdSubmit_Click()
On Error GoTo cmdSubmit_Click_Error
Dim db As Database
Dim rst As DAO.Recordset
Dim strSQL As String
strSQL = "CompanyPOTable"
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL)
Me.txtPOAmt.SetFocus
If Me.txtPOAmt.Text = "" Then
MsgBox "You must enter a PO amount!", vbInformation, "Invalid PO Amount"
Exit Sub
End If
With rst
.AddNew
![RA_PO_Nbr] = [Forms]![frmNewPO]![txtPONbr]
![RA_Surcharge] = [Forms]![frmNewPO]![txtCompany_Surcharge]
![RA_Net_Merch] = [Forms]![frmNewPO]![txtCompany_Net_Merch]
![RA_PO_Amt] = [Forms]![frmNewPO]![txtPOAmt]
![OriginalEntryDate] = [Forms]![frmNewPO]![txtOrigDate]
![PO_Comments] = [Forms]![frmNewPO]![txtPOComments]
.Update
End With
[Forms]![frmNewPO]![txtPONbr] = ""
[Forms]![frmNewPO]![txtCompany_Surcharge] = ""
[Forms]![frmNewPO]![txtCompany_Net_Merch] = ""
[Forms]![frmNewPO]![txtPOAmt] = ""
[Forms]![frmNewPO]![txtPOComments] = ""
[Forms]![frmNewPO]![txtPONbr].SetFocus
cmdSubmit_Click_Error:
If Err.Number = 3022 Then
MsgBox "You have entered a PO Number that already exists in the table!"
Else
If Err.Number = 3058 Then
MsgBox "You must enter a PO number!"
End If
End If
End Sub
Thanks,
Ray