I have a textbox on a form for a Purchase order number.
Once the PO is enter one of the following is to happen.
1) The PO doesn't yet exist in the DB, continue to gather remaining data related to new PO.
2) The PO exists in the DB but is already closed. Ask if wanting to reopen and follow users request.
3) The PO exists in the DB and is open. Inform the user to enter a new number or allow to quit the data entry.
It is item 3 that is stumping me. I have tried the BeforeUpdate, AfterUpdate, Exit and LostFocus events. The two sticking points are that I would like the textbox to be emptied for the user to enter a new number or have the form close.
Example of code:
Private Sub PONum_BeforeUpdate(Cancel As Integer)
Dim db As dao.Database
Dim rst_POHead As dao.Recordset
Dim Response As Integer
Dim Style As String
Set db = CurrentDb
Set rst_POHead = db.OpenRecordset("tblPoHead", dbOpenDynaset)
Style = vbYesNo + vbDefaultButton1
rst_POHead.FindFirst "PONum = '" & Me!PONum & "'"
If rst_POHead.NoMatch = True Then
Me!PONum.BorderColor = RGB(0, 255, 0)
Else
If rst_POHead!Complete = True Then
Me!PONum.BorderColor = RGB(255, 0, 0)
Response = MsgBox("PO Already Exists but is closed. Do you wish to Reopen PO?", Style, "PO Exists")
If Response = vbYes Then
Cancel = False
With rst_POHead
.Edit
!Complete = False
.Update
End With
Me!PONum.BorderColor = RGB(0, 255, 0)
Else
Cancel = True
Me!PONum.Undo
Me!cmdChangePONum.Visible = False
Me!cmdReset.Visible = False
End If
Else
MsgBox "PO is already Open in the system. Enter a different number."
Me!PONum.BorderColor = RGB(166, 166, 166)
DoCmd.CancelEvent
Me!PONum.Undo
End If
End If
rst_POHead.Close
Set rst_POHead = Nothing
db.Close
Set db = Nothing
End Sub
Thanks for any input,
Lance