Reset form if condition is met

Solola

Board Regular
Joined
Sep 23, 2003
Messages
73
I have a data-form and I have vba (2 separate if statements) to assess 2 date fields to determine if data entry can continue. Essentially, if both date fields are "n/a", or if the cancel_date is n/a and the appt_date is in the future, I want to throw an error and clear the form. My problem is that my vba is clearing the form regardless of the outcome of the if statements. Even if both dates are in the past, I'm stilling getting my message and a cleared form. I'm guessing I've put this together wrong (I know just enough vba to be very dangerous, but I'm not always comfortable with what I'm doing).

I'm using Access 2007.

Code:
Private Sub TCRequestDte_GotFocus()
If Forms![frmTDMEntry].appt_date = "n/a" And Forms![frmTDMEntry].cancel_date = "n/a" Then GoTo err_form_clear
If Forms![frmTDMEntry].cancel_date = "n/a" And Forms![frmTDMEntry].appt_date > Now() Then GoTo err_form_clear
err_form_clear:
    MsgBox "Form entry not allowed.  This workup is still active." & Chr(13) & "Alert the SC that this form must be resubmitted when the workup is complete." & Chr(13) & "Your form will be reset.", vbInformation, "Invalid Form"
    Dim Ctl As Control
    On Error Resume Next
    For Each Ctl In Me.Controls
    Ctl.Value = Null
    Next Ctl
    RID.SetFocus
End Sub

Please help?
Laura
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You don't have an exit point before your err_form_clear label. So, the code just flows right through that. You would put
Code:
Exit Sub
err_form_clear:
 
Upvote 0
Believe this will work.

Private Sub TCRequestDte_GotFocus()

If IsNull(Forms![frmTDMEntry].appt_date) And IsNull(Forms![frmTDMEntry].cancel_date) Then
GoTo err_form_clear
ElseIf IsNull(Forms![frmTDMEntry].cancel_date) And Forms![frmTDMEntry].appt_date > Now() Then
GoTo err_form_clear
Else
Exit Sub
End If

err_form_clear:
MsgBox "Form entry not allowed. This workup is still active." & Chr(13) & "Alert the SC that this form must be resubmitted when the workup is complete." & Chr(13) & "Your form will be reset.", vbInformation, "Invalid Form"
Dim Ctl As Control
On Error Resume Next
For Each Ctl In Me.Controls
Ctl.Value = Null
Next Ctl
rid.SetFocus

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,903
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top