Link to home
Start Free TrialLog in
Avatar of B Blyden
B Blyden

asked on

Exit sub without triggering AfterUpdate

Hello,

I have disabled the X on my userform and have an added a exit button on the form. I routine in the txtStudentID_AfterUpdate() field. If the user enters their studentID in that field and then deletes/backspace and tries to exit the form, the routine still runs. I just want it to allow the user to exit. They basically have to hit exit twice. I played around with this for a while and just can't figure out how to get around it. Don't want the user to msgbox when exiting.

 Private Sub txtEmpNumber_AfterUpdate()
'Connects to dba 
Dim rs As Object, strSQL As String

    ' connect to the Access database
    Set cn = New ADODB.Connection
    myConn = TARGET_DB
    With cn
     .Provider = "Microsoft.ACE.OLEDB.12.0"
     .Open myConn
   End With

'create recordset
Set rs = New ADODB.Recordset

src = "SELECT StudentName FROM qryStudentDB WHERE [StudentID]='" & Me.txtStudentID.Value & "'"

Set rs = New ADODB.Recordset

rs.Open src, cn, adOpenStatic

If rs.RecordCount = 0 Then
    rs.Close
    Set myConn = Nothing
    Set rs = Nothing
    MsgBox "No Records Found"
'    Me.txtStudentID.SetFocus
    Exit Sub
End If

txtStudentName.Text = rs(0)

Set rs = Nothing

End Sub

Open in new window


Exit Button

Private Sub cmdExit_Click()
Unload Me
 End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of B Blyden
B Blyden

ASKER

Just that simple.. I feel so stupid. Thanks Norie.