When using a database, you are in fact using two applications to create a final product. Microsoft Access is used to design the necessary objects for your product. This means that Microsoft Access is used for its visual display of objects. On the other hand, Microsoft Visual Basic is used to handle code that enhances the functionality of your application. The code you write is made of small instructions written in Visual Basic. These instructions are written in plain English, a language that the computer, that is, the operating system, doesn't understand. Visual Basic, as its own language among other computer languages, is internally equipped with a low level program called a compiler. This program takes your English language instructions and translates them in a language the computer can understand. The language the computer speaks is known as the machine language. You usually don't need to know anything about this language. After writing your code, at one time it is transmitted to the compiler. The compiler analyzes it first and checks its syntax. The words used in the program and the variables are checked for their declaration and use. The events and procedures are checked for their behavior. The expressions are checked for their accuracy. If something is wrong with the code, that is, if the compiler does not understand something in your code, it would display an error and stop. You must correct the mistake or else... As long as the compiler cannot figure out a piece of code in a module, it would not validate it. If the code is "admissible", the compiler would perform the assignments that are part of the code and give you a result based on its interpretation of the code. This means that the code can be accurate but produce an unreliable or false result. This is because the compiler is just another program: it does not think and does not correct mistakes although it can sometimes point them out.
To carry out an assignment, sometimes a procedure needs one or more values to work on. If a procedure needs a value, such a value is called an argument. While a certain procedure might need one argument, another procedure might need many arguments. The number and types of arguments of a procedure depend on your goal. If you are writing your own procedure, then you will decide how many arguments your procedure would need. You also decide on the type of the argument(s). For a procedure that is taking one argument, inside of the parentheses of the procedure, write the name of the argument followed by the As keyword followed by the type of data of the argument. Here is an example: Sub CalculateArea(Radius As Double) End Sub A procedure can take more than one argument. If you are creating such a procedure, between the parentheses of the procedure, write the name of the first argument followed by As, followed by the data type, followed by a comma. Add the second argument and subsequent arguments and close the parentheses. There is no implied relationship between the arguments; for example, they can be of the same type: Sub CalculatePerimeter(Length As Double, Height As Double) End Sub The arguments of your procedure can also be as varied as you need them to be. Here is an example: Sub DisplayGreetings(strFullName As String, intAge As Integer, dblDistance As Double) End Sub
There are various ways you can call a sub procedure. As we saw already, if a sub procedure does not take an argument, to call it, you can just write its name. If a sub procedure is taking an argument, to call it, type the name of the sub procedure followed by the name of the argument. If the sub procedure is taking more than one argument, to call it, type the name of the procedure followed by the name of the arguments, in the exact order they are passed to the sub procedure, separated by a comma. Here is an example: Private Sub txtResult_GotFocus() Dim dblHours As Double Dim dblSalary As Double dblHours = txtHours dblSalary = txtSalary CalcAndShowSalary dblHours, dblSalary End Sub Sub CalcAndShowSalary(Hours As Double, Salary As Double) Dim dblResult As Double dblResult = Hours * Salary txtResult = dblResult End Sub Alternatively, you can use the Call keyword to call a sub procedure. In this case, when calling a procedure using Call, you must include the argument(s) between the parentheses. using Call, the above GotFocus event could call the CalcAndShowSalary as follows: Private Sub txtResult_GotFocus() Dim dblHours As Double Dim dblSalary As Double dblHours = txtHours dblSalary = txtSalary Call CalcAndShowSalary(dblHours, dblSalary) End Sub
If you create a procedure that takes an argument, whenever you call that procedure, you must provide a value for that argument. If you fail to provide a value for the argument, when the application runs, you would receive an error. Imagine you create a function that will be used to calculate the final price of an item after discount. The function would need the discount rate in order to perform the calculation. Such a function would look like this: Function CalculateNetPrice(DiscountRate As Double) As Currency Dim OrigPrice As Double OrigPrice = CCur(txtMarkedPrice) CalculateNetPrice = OrigPrice - CLng(OrigPrice * DiscountRate * 100) / 100 End Function Since this function expects an argument, if you don't supply it, the following program would not compile: Function CalculateNetPrice(DiscountRate As Double) As Currency Dim OrigPrice As Double OrigPrice = CCur(txtMarkedPrice) CalculateNetPrice = OrigPrice - CLng(OrigPrice * DiscountRate * 100) / 100 End Function Private Sub cmdCalculate_Click() Dim dblDiscount# dblDiscount = CDbl(txtDiscountRate) txtPriceAfterDiscount = CalculateNetPrice(dblDiscount) End Sub If a procedure such as this CalculateNetPrice() function uses the same discount rate over and over again, instead of supplying an argument all the time, you can provide a default value for the argument. If you do this, you would not need to provide a value for the argument when you call the procedure. Such an argument is referred to as optional. To make an argument optional, in the parentheses of its procedure, start it with the Optional keyword. On the right side of the data type of the argument, type the assignment operator, followed by the desired default value that would be used for the argument if you fail to provide one or decide not to provide one. Based on this, the above CalculateNetPrice() function could be defined as: Function CalculateNetPrice(Optional DiscountRate As Double = 0.2) As Currency Dim OrigPrice As Double OrigPrice = CCur(txtMarkedPrice) CalculateNetPrice = OrigPrice - CLng(OrigPrice * DiscountRate * 100) / 100 End Function Private Sub cmdCalculate_Click() txtPriceAfterDiscount = CalculateNetPrice() End Sub Notice that, this time, you don't have to provide a value for the argument when calling the function: if you omit the value of the argument, the default value would be used. At another time, when calling the function, if you want to use a value that is different from the default value, you should make sure you provide the desired value. Consider the following call: Function CalculateNetPrice(Optional DiscountRate As Double = 0.2) As Currency Dim OrigPrice As Double OrigPrice = CCur(txtMarkedPrice) CalculateNetPrice = OrigPrice - CLng(OrigPrice * DiscountRate * 100) / 100 End Function Private Sub cmdCalculate_Click() Dim dblDiscount# dblDiscount = CDbl(txtDiscountRate) txtPriceAfterDiscount = CalculateNetPrice(dblDiscount) End Sub Instead of one, you can also create a procedure with more than one argument. You may want all, one, or more than one of these arguments to be optional. To do this, declare each optional argument with the Optional keyword and assign it the desired value. Consider the following example where two arguments are optional: Function CalculateNetPrice(OrigPrice As Currency, _ Optional TaxRate As Double = 0.0575, _ Optional DiscountRate As Double = 0.25) As Currency Dim curDiscountValue As Currency Dim curPriceAfterDiscount As Currency Dim curTaxValue As Currency Dim curNetPrice As Currency curDiscountValue = CLng(OrigPrice * DiscountRate * 100) / 100 curPriceAfterDiscount = OrigPrice - curDiscountValue curTaxValue = CLng(curPriceAfterDiscount * TaxRate * 100) / 100 txtDiscountValue = CStr(curDiscountValue) txtPriceAfterDiscount = CStr(curPriceAfterDiscount) txtTaxValue = CStr(curTaxValue) CalculateNetPrice = curPriceAfterDiscount + curTaxValue End Function Private Sub cmdCalculate_Click() Dim curMarkedPrice As Currency Dim dblDiscountRate# Dim dblTaxRate# curMarkedPrice = CCur(txtMarkedPrice) dblDiscountRate = CDbl(txtDiscountRate) dblTaxRate = CDbl(txtTaxRate) txtNetPrice = CalculateNetPrice(txtMarkedPrice, txtTaxRate, dblDiscountRate) End Sub If you create a procedure that takes more than one argument, when calling the procedure, make sure you know what argument is optional and which one is required. When calling a procedure that has more than one argument but only one argument is optional, you can provide a value for the required argument and omit the others. Here is an example: Function CalculateNetPrice(OrigPrice As Currency, _ Optional TaxRate As Double = 0.0575, _ Optional DiscountRate As Double = 0.25) As Currency Dim curDiscountValue As Currency Dim curPriceAfterDiscount As Currency Dim curTaxValue As Currency Dim curNetPrice As Currency curDiscountValue = CLng(OrigPrice * DiscountRate * 100) / 100 curPriceAfterDiscount = OrigPrice - curDiscountValue curTaxValue = CLng(curPriceAfterDiscount * TaxRate * 100) / 100 txtDiscountValue = CStr(curDiscountValue) txtPriceAfterDiscount = CStr(curPriceAfterDiscount) txtTaxValue = CStr(curTaxValue) CalculateNetPrice = curPriceAfterDiscount + curTaxValue End Function Private Sub cmdCalculate_Click() Dim curMarkedPrice As Currency curMarkedPrice = CCur(txtMarkedPrice) txtNetPrice = CalculateNetPrice(txtMarkedPrice) End Sub In reality, the Microsoft Visual Basic language allows you to create the procedure with the list of arguments as you see fit, as long as you make sure you clearly specify which argument is optional and which one is required. If you create a procedure that has more than one argument and at least one argument with a default value, if the optional argument is positioned to the left of a required argument, when calling the procedure, if you don't want to provide a value for the optional argument, enter a comma in its placeholder to indicate that there would have been a value for the argument but you prefer to use the default value. Remember that you must provide a value for any required argument. Consider the following example: Function CalculateNetPrice(OrigPrice As Currency, _ Optional TaxRate As Double = 0.0575, _ Optional DiscountRate As Double = 0.25) As Currency Dim curDiscountValue As Currency Dim curPriceAfterDiscount As Currency Dim curTaxValue As Currency Dim curNetPrice As Currency curDiscountValue = CLng(OrigPrice * DiscountRate * 100) / 100 curPriceAfterDiscount = OrigPrice - curDiscountValue curTaxValue = CLng(curPriceAfterDiscount * TaxRate * 100) / 100 txtDiscountValue = CStr(curDiscountValue) txtPriceAfterDiscount = CStr(curPriceAfterDiscount) txtTaxValue = CStr(curTaxValue) CalculateNetPrice = curPriceAfterDiscount + curTaxValue End Function Private Sub cmdCalculate_Click() Dim curMarkedPrice As Currency Dim dblDiscountRate# Dim dblTaxRate# curMarkedPrice = CCur(txtMarkedPrice) dblDiscountRate = CDbl(txtDiscountRate) txtNetPrice = CalculateNetPrice(curMarkedPrice, , dblDiscountRate) End Sub
When you call a procedure that takes more than one argument, you must pass the arguments in the right order. Consider the following function: Function ResumeEmployee$(salary As Currency, name As String, dHired As Date) Dim strResult$ strResult = name & ", " & CStr(dHired) & ", " & CStr(salary) ResumeEmployee = strResult End Function When calling this function, you must pass the first argument as a currency value, the second as a string, and the third as a date value. If you pass a value in the wrong position, the compiler would throw an error and the program would not work. This is what would happen if you call it as follows: Private Sub cmdResume_Click() Dim strFullName As String Dim dteHired As Date Dim curHourlySalary As Currency Dim strResume$ strFullName = [txtFullName] dteHired = CDate([txtDateHired]) curHourlySalary = CCur(txtHourlySalary) strResume = ResumeEmployee(strFullName, dteHired, curHourlySalary) txtResume = strResume End Sub While you must respect this rule, Microsoft Visual Basic provides an alternative. You don't have to pass the arguments in their strict order. Instead, you can assign the desired value to each argument as long as you know their names. To do this, when calling the function, to assign the desired value to an argument, on the right side of the sub procedure or in the parentheses of the function, type the name of the argument, followed by the := operator, followed by the (appropriate) value.
So far, when creating a procedure with one or more arguments, we simply assumed that, when calling the procedure, we would provide the desired value(s) for the argument(s). With this technique, the procedure receives the value of the argument and does what it wants with it. The argument itself is not changed. This technique is referred to as passing an argument by value. To reinforce this, you can type the ByVal keyword on the left side of the argument. Here is an example: Function CalculateTriangleArea#(ByVal Base As Double, ByVal Height As Double) CalculateTriangleArea = Base * Height / 2 End Function
We also saw that the main difference between a sub procedure and a function is that a function can return a value but a sub procedure cannot. Microsoft Visual Basic, like many other languages, provides an alternative to this. Not only can a sub procedure return a value but also it makes it possible for a procedure (whether a sub or a function) to return more than one value, a feature that even a regular function doesn't have. When creating a procedure with an argument, we saw that, by default, the procedure could not modify the value of the argument. If you want to procedure to be able to alter the argument, you can pass the argument by reference. To do this, type the ByRef keyword on the left side of the name of the argument. If you create a procedure that takes more than one argument, you can decide which one(s) would be passed by value and which one(s) would be passed by reference. There is no order that the arguments must follow.
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||
|