stopping code executing if cancel button pressed on msgbox

ninjaimp

Well-known member
Joined
Jun 13, 2007
Messages
80
Programming Experience
1-3
Hi

I have an if statement which checks if a value has benn entered and if it hasnet it pops up with a message box and tells them.

Now if the if statement is in the Sub as the code that is executing it works fine, but i put it into a seperate sub and call it from within the original sub:

VB.NET:
  If AppVariables.CustName = "" Then
            MsgBox("You have to configure this report before it can be run." & (Chr(13)) & (Chr(10)) & "Go to Configuration > Configure to resolve this", MsgBoxStyle.Critical)
            Return
        End If

Now since i have put it into the seperate sub and the ok/cancel button is pressed it still continues with the rest of the code and of course fails as not all information has been entered.

I wondered if anyone could help

Regards
 
Change the logic around just a little bit:
VB.NET:
If AppVariables.CustName = String.Empty Then
  Messagebox.Show("You have to configure this report before it can be run." & Environment.NewLine & "Go to Configuration > Configure to resolve this")
Else
  'Rest of the code, which is skipped if AppVariables.CustName is blank/empty
End If
 
hi

thanks for your response.

I have the logic working, my only issue is that becuase i will be checking on more than one value i was putting it nto its own procedure which was then being called from the main script. The problem is that when i use the 'Return'. it returns the process back to the originating script and continues to process it.

How can i get it to stop completely when the string is empty?

many thanks
 
Seems to me there are a lot of different ways to do it... personally, I would change the Sub to a Function and return a boolean indicating if the program is to continue. Your calling code would be something like "If functionName() = False Then Exit [Current Proceedure Type]".
 
the function route sounds like a good idea - hadn't thought of it before, but will give it a go.

many thanks - will update when done.
 
I would avoid using the Exit statement for a number of reasons.

To do this with using functions and avoiding the exit statement you can do something similar to this:
VB.NET:
If Var1Check = True AndAlso Var2Check = True AndAlso Var3Check = True Then
  'Run your code
Else
  Messagebox.Show("Not all of the criteria has been met")
End If
Var1Check, Var2Check, Var3Check are the generic names I used for the functions, you would want to use the names of the actual functions you make.
 
hi

just to update, have got it working.

I created a seperate function:


Public Function CheckValues() As Boolean
If AppVariables.CustName = String.Empty Or AppVariables.CustAddress = String.Empty Then
CheckValues = False
Else
CheckValues = True
End If
End Function



Which is called by the main sub like this:

Call CheckValues()

If CheckValues() = False Then
MsgBox("You have to configure this report before it can be run." & (Chr(13)) & (Chr(10)) & "Go to Configuration > Configure to resolve this", MsgBoxStyle.Critical)
Return
End If


Works like a charm - many thanks for your help

Regards
 
Back
Top