How to use your new SubBut the Sub is not doing much good where it is. We need a way to use that code. To use our new Sub, we have to tell Visual Basic to execute the code. We do that simply by referring to the Sub by name.
So click inside the button code, just before the End Sub of the button. Type the following:
Call ErrorCheck()You don't have to use the "Call" word. Try taking that word out altogether, and then testing the programme again. The programme should run exactly the same. But using the "Call" word makes your code easier to read, and tells you that you are executing your own Subroutine on this line.
Your coding window should now look like this:
Run your programme and test it out. You should get the Message Box again, when nothing is in the Textbox.Add another button to your form. Double click the new button to get at the code. Then type Call ErrorCheck() as the code for it. Run your programme again, and click your new button. You should get the Message box popping up, when nothing is entered into the Textbox.
The point about this is that you have created your own code segment. You can use this segment of code whenever you like, just by referring to it by name. Of course, you can have your code check more than one Textbox. You can check as many as you like. And for whatever you like. You can include the code you wrote to check for a correct email address, as well. But all that error checking code is no longer clogging up your button code.
A function is more or less the same thing as a Sub - a segment of code you create yourself, and that can be used whenever you want it. The difference is that a Function returns a value, while a Sub doesn't. When you Called a Sub you did this:
Call AddNumbers(first, second)Visual Basic will go off and execute that code for you, and then drop down to the next line. The Sub AddNumbers is not a value, it's not equal to anything. It's not like a normal variable where you assign something to it. It's just the name of your Subroutine.
A Function is different. It is a value, will be equal to something, and you do have to assign a value to it. You create a Function in the same way you did a Sub, but this time your code will be like this:
Private Function ErrorCheck ( ) As Boolean
First, we've changed the word "Sub" to "Function"; second we've added "As" something, in this case "As Boolean". The name we called our Function is ErrorCheck, and ErrorCheck is now just like a variable. And just like a variable, we use one of the Types. We can use "As Integer", "As Long", "As Double", "As String", or any of the variable types.
Let's write some code, and try an example.
Add a new button and a textbox to your form. Change the Name of the textbox to txtFunction. Double click your button and add the following code to it (add it after the End Sub of the button, but before the End Class):
Private Function CheckError ( ) As Boolean
Dim TextBoxData As String
TextBoxData = Trim(txtFunction.Text)
If TextBoxData = "" Then
MsgBox("Blank Text Box detected")
CheckError = True
End IfEnd Function
This is almost the same code from our Sub called ErrorCheck, in a previous section. The difference is the one added line - CheckError = True. Remember that CheckError is now like a variable. In this case it was a Boolean variable. So if there's nothing in the Textbox, we have set CheckError to True.
Again, this code is not doing much good by itself. We need a way to use it. This time, because we've set up a Function, we have to assign the value of the function to a variable. Like this:
Dim IsError As Boolean
IsError = CheckError ()Here, we are saying "Run the function called CheckError. When you have finished, assign the value of CheckError to the variable called IsError".
Once that code is executed we can then use the variable IsError and test its value. If it's true, then we know that the user did not enter anything into the Textbox; if it's False, then we know that they did. The benefit of using a Function to check for our errors is that we can halt the programme if IsError = True. Like this:
If IsError = True then
End IfSo double click your button and add the following:
Dim IsError As Boolean
IsError = CheckError ()
If IsError = True thenExit Sub
MsgBox("IsError = False")
End IfRun your programme again. Click the button when the textbox is blank, and see what happens. Then enter some text into the textbox, and click your button again.