how to exit a form in this scenario?

  • Thread starter Thread starter ping
  • Start date Start date
P

ping

basically i got this code below that checks whether an
identity card number that a user inputs is valid. if not
valid, it'll msg "IC number invalid!" and recursor the
control again.
My Problem now is, i've got an command button(created by
wizard,except i added in docmd.close) at the footer of the
form to go to switchboard. if user decides to exit after
keying an invalid identity number and clicks the button,
the same msg "IC number invalid!" would pop out(i belief
it has something to do with the button trying to save the
record before closing). user wouldnt be able to exit
unless he keyed in a valid ic number.
any ideas to get around this?
Or is there a way to code the exit button priority over
the rest? it doesnt matter if all controls on current
record cant be saved.

btw, IC is a primary key.

======this is the code that checks validity==========
Private Sub Ic_BeforeUpdate(Cancel As Integer)
'check to see if ic number is valid, if null value or
false, get the user to retype again.

Dim found As String
found = Nz(DLookup("ic", "personnel_list", "ic = '" _
& Forms!Booking_Out!Ic & "'"), "False")
If found <> "False" Then
'do nothing
Else
MsgBox "IC number invalid!"
ic.SelStart = 0
ic.SelLength = Len(ic)
Cancel = True
End If


=============this is the exit button=============
Private Sub Command6_Click()
On Error GoTo Err_Command6_Click
DoCmd.Close
Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "MainSW"
DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_Command6_Click:
Exit Sub
 
Back
Top