Sub routine fire on command

  • Thread starter Thread starter David
  • Start date Start date
D

David

Hello group,
The purpose of this code is to check that these two fields
are NOT blank or NUll before I continue with the code. I
have a group of subfunctions that use If Then Else. I
would like to send all of the FALSE results to a specific
subfuction titled SPECIAL below. The glitch is I only
want this subfunction to FIRE if one of my subroutine
FIREs it as a result of FALSE. The subfunction titled
SPECIAL fires everytime the routine is executed. If the
result is TRUE, the system displays the msgbox in the
subroutine and then displays a second msg box in the
SPECIAL subroutine. How can I work it so that the SPECIAL
subroutine only fires if one of the subfunctions results
are FALSE?

Private Sub Submit_Click()

GoTo My_Date_of_Request

My_Date_of_Request:
If [Date of Request] <> "" Then
GoTo My_Date_of_Delivery
Else
GoTo My_SPECIAL
Exit Sub
End If

My_Date_of_Delivery:
If [Date of Delivery] <> "" Then
MsgBox "Request Number DR04-" & [Data Request Number]
& "."
Else
GoTo My_SPECIAL
Exit Sub
End If


My_SPECIAL:
MsgBox "Enter Required Fields in RED. "
'I can place the above line of code after
'the ELSE for each subroutine above, but I want to
'add multiple commands and functions in
'this routine.

On Error GoTo Err_Submit

Exit_Submit:
Exit Sub

Err_Submit:
MsgBox "Enter Required Fields. "
Exit Sub

End Sub
 
Hi,

It's a bit difficult to follow the logic using GoTo's

Why not just use something like

Private Sub Submit_Click()

If [Date of Request] = "" Then
If [Date of Delivery] <> "" Then
MsgBox "Enter Required Fields in RED."
Exit Sub
Else
MsgBox "Request Number DR04-" & [Data Request Number] & "."
End If
End If

End Sub


This assumes that the date fields are text rather than actual dates.
I left out the usual error coding just to keep it simple,

Cheers,
Peter
 
When you exit the msgbox "Request Number DR04.... " the end if is executed
then it runs My_Special.
The Exit Sub is never executed before My_Special... try moving it after the
msgbox line.

Damon
 
Peter,
Thank you very much, I agree, my code was a bit messy.
Thank you for the alternate clean solution.
How can I create a subfunction, such as SPECIAL that will
only run if the below If statement is TRUE? I will have
many IF statements to validate all fields are completed
before progressing to more lines of code in the SPECIAL
subfunction.

If [Date of Request] = "" Then
If [Date of Delivery] <> "" Then
GoTo SPECIAL
Else
MsgBox "Request Number DR04-" & [Data Request
Number] & "."
End If
End If

SPECIAL:
MsgBox "Request Number DR04-" & [Data Request
Number] & "."
'and many other lines of code

End Sub
 
Hi,

I would avoid the GoTo's except for error coding.
You can still add lots more validation ater the 'If Then' statement
alternatively just refer to another routine e.g.

Private Sub Submit_Click()

If [Date of Request] = "" Then
If [Date of Delivery] <> "" Then
'lots more validation can still go here
SpecialRoutine
Else
MsgBox "Request Number DR04-" & [Data Request Number] & "."
End If
End If

End Sub

Sub SpecialRoutine
'more validation could go here
End Sub


Cheers
 
Back
Top