Oday Salim
Oday Salim

Reputation: 1147

Checking a function before executing current function

I have created two separate functions on VBA.

Ideally, a user will have 2x buttons assigned to these macros. One button to validate the data myCheck() and the other button to submit the data onSubmit().

myCheck() function is a Boolean and is set up to be either true of false as indicator whether to run onSubmit().

So, if a user presses onSubmit() prior to validating the data, it should not submit the data as myCheck() was already initialised to False. See code below:

Function myCheck() As Boolean

    myCheck = False

    'Do checking operation
    If chkTEP = True And chkNULL = True Then
        Output = MsgBox("Validation complete", vbInformation, "Validation")    
        myCheck = True    
    ElseIf chkTEP = False And chkNULL = True Then
        Output = MsgBox("Double check your TEP combination", vbCritical, "Validation")    
        myCheck = False    
    ElseIf chkTEP = True And chkNULL = False Then
        Output = MsgBox("There is a blank cell", vbCritical, "Validation")    
        myCheck = False    
    Else:
        Output = MsgBox("Please make sure spreadhseet is filled correctly", vbCritical, "Validation")    
        myCheck = False    
    End If

End Function

As you can see from above, the function myCheck can either result in TRUE or FALSE.

The next step after this is to run another function onSubmit(). If myCheck is FALSE, then onSubmit will not work.. etc.

Sub onSubmit()

    If myCheck = False Then
        GoTo FuncEnder    
        Else
        'execute onSubmit function here    
    FuncEnder:

    End If

End Sub

What I discovered is once onSubmit() is executed, it runs the entire function myCheck(), rather than just checking its stored Boolean value.

Upvotes: 2

Views: 720

Answers (2)

Mahhdy
Mahhdy

Reputation: 592

Declare a Public variable of myCheck in your modules. Made is possible to check if the your first check button being pressed or not. For instance mybutton.tag = true.

Public myCheck as boolean

then everything should be OK. It runs as it will be always false.

Sub mybutton_click()

    myCheck  ' calls the mycheck function

    if mycheck then 
       mybutton.tag="true"
    else
       mybutton.tag = ""
    end if

end sub

then

on your second sub:

Sub onSubmit()

    if mybutton.tag <> "true" then 
       msgbox" You need to press Mycheck button First!"
       exit sub
    else
        If myCheck = False Then end sub   

        'execute onSubmit function here    

    End If

End Sub

Upvotes: 0

urdearboy
urdearboy

Reputation: 14580

Your button needs to be linked to the 'Execute' macro. It will first test your check and only execute submit when <> False. There is no need for 2 buttons when you can link all this together with one. You will get the same desired output with one less step needed by the user. I also changed your functions here to "Public Function"

Function 1

Public Function myCheck() As Boolean

'YOUR FUNCTION HERE

End Function

Function 2

Public Function onSubmit()

'YOUR FUNCTION HERE

End Function

Macro

Sub Execute()

If myCheck = False Then Exit Sub
Else
onSubmit

End Sub

Upvotes: 1

Related Questions