Accessories for Programming(Access)

Embed Size (px)

Citation preview

  • 8/13/2019 Accessories for Programming(Access)

    1/16

    Accessories for Programming

    Introduction

    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. n the other hand, Microsoft !isual

    "asic is used to handle code that enhances the functionality of your application.

    The #ompiler

    The code you write is made of small instructions written in !isual "asic. These instructions are

    written in $nglish, a language that the computer, that is the operating system, doesn%t

    understand. !isual "asic, as its own language among other computer languages, is internally

    e&uipped with a low level program called a compiler. This program ta'es your $nglish

    language instructions and translates them in a language the computer can understand. Thelanguage the computer spea's is 'nown as the machine language. (ou usually don%t need to

    'now anything about this language.

    After writing your code, at one time it is transmitted to the compiler. The compiler analy)es it first,

    chec's its synta*, the words used in the program, the variables are chec'ed for their declaration and

    use. The events and procedures are chec'ed for their behavior. The e*pressions are chec'ed 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. (ou must correct the mista'e 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 thin' and

    does not correct mista'es although it can sometimes point them out. -or this reason, you should 'now

    what you are doing.

    Writing Procedures With Arguments

    To carry out an assignment, sometimes a procedure needs one or more values to wor' 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. (ou also decide on the type of the arguments/. -or a procedure that is ta'ing one

    argument, inside of the parentheses of the procedure, write the name of the argument followed by the

    As'eyword followed by the type of data of the argument. 0ere is an e*ample

    Sub CalculateArea(Radius As Double)End Sub

    A procedure can ta'e 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 Asfollowed by the data

    type, followed by a comma. Add the second argument and subse&uent arguments and close theparentheses. There is no implied relationship between the arguments1 for e*ample, they can be of the

  • 8/13/2019 Accessories for Programming(Access)

    2/16

    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. 0ere is an e*ample

    Sub DisplayGreetings(strFullName As String, intAge As Integer, dblDistance AsDouble)End Sub

    Practical 2earning Writing Procedures With Arguments

    3. 4witch to the #ode $ditor. #lic' an empty area at the end of the e*isting code and create the

    following procedure

    Sub SolveEllipse(SmallRadius As Double, LargeRadius As Double) Dim dblCircum As Double Dim dblArea As Double

    dblCircum (SmallRadius ! LargeRadius) " # dblArea SmallRadius " LargeRadius " $%&'&

    t*tEllipseCircum+erence dblCircum t*tEllipseArea dblAreaEnd Sub

    5. To create an e*ample of function that ta'es an argument, add the following function at the end

    of the e*isting code

    unction CubeArea(Side As Double) As Double CubeArea Side " Side " -End unction

    6. To use different e*amples of functions that ta'e one or two arguments, type the following

    functions

    unction Cube.olume(Side As Double) As Double Cube.olume Side " Side " SideEnd unction

    unction /o*Area(dblLengt0 As Double, 1 dbl2eig0t As Double, 1 dbl3idt0 As Double) As Double Dim Area As Double

    Area # " ((dblLengt0 " dbl2eig0t) ! 1 (dbl2eig0t " dbl3idt0) ! 1 (dblLengt0 " dbl3idt0) 1 ) /o*Area AreaEnd unction

    unction /o*.olume(dblLengt0 As Double, 1 dbl2eig0t As Double, 1 dbl3idt0 As Double) As Double

  • 8/13/2019 Accessories for Programming(Access)

    3/16

    Dim .olume As Double .olume dblLengt0 " dbl2eig0t " dbl2eig0t /o*.olume .olumeEnd unction

    #alling Procedures That 0ave Arguments

    We saw already how to call a procedure that does not ta'e any argument. Actually, there are various

    ways you can call a sub procedure. As we saw already, if a sub procedure does not ta'e an argument,

    to call it, you can just write its name. If a sub procedure is ta'ing an argument, to call it, type the name

    of the sub procedure, followed by space, followed by the name of the argument. If the sub procedure is

    ta'ing more than one argument, to call it, type the name of the procedure followed by the name of the

    arguments, in the e*act order they are passed to the sub procedure, separated by a comma. 0ere is an

    e*ample

    Private Sub t*tResult1Gotocus()

    Dim dbl2ours As Double Dim dblSalary As Double

    dbl2ours t*t2ours dblSalary t*tSalary

    CalcAndS0o4Salary dbl2ours, dblSalaryEnd Sub

    Sub CalcAndS0o4Salary(2ours As Double, Salary As Double) Dim dblResult As Double

    dblResult 2ours " Salary

    t*tResult dblResultEnd Sub

    Alternatively, you can use the 'eyword Callto call a sub procedure. In this case, when calling a

    procedure using Call, you must include the arguments/ between the parentheses. using Call, the

    above GotFocusevent could call the CalcAndShowSalaryas follows

    Private Sub t*tResult1Gotocus() Dim dbl2ours As Double Dim dblSalary As Double

    dbl2ours t*t2ours

    dblSalary t*tSalary

    Call CalcAndS0o4Salary(dbl2ours, dblSalary)End Sub

    Practical 2earning #alling Procedures With Arguments

    3. To call the above procedures that ta'e arguments, on the bject combo bo*, select

    cmdECalculateand implement its OnClickevent as follows

    Private Sub cmdECalculate1Clic5() Dim Radius& As Double

    Dim Radius# As Double Radius& t*tEllipseRadius& Radius# t*tEllipseRadius#

  • 8/13/2019 Accessories for Programming(Access)

    4/16

  • 8/13/2019 Accessories for Programming(Access)

    5/16

  • 8/13/2019 Accessories for Programming(Access)

    6/16

    unction Calculate6etPrice(DiscountRate As Double) As Currency Dim 7rigPrice As Double

    7rigPrice CCur(t*t8ar5edPrice) Calculate6etPrice 7rigPrice 9 CLng(7rigPrice " DiscountRate "&::) ; &::End unction

    Private Sub cmdCalculate1Clic5() Dim dblDiscountetPrice/ 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.

    4uch an argument is referred to as optional.

    To ma'e an argument optional, in the parentheses of its procedure, start it with the Optional

    'eyword. n 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 fail to provide one or decide not to

    provide one. "ased on this, the above #alculate>etPrice/ function could be defined as

    unction Calculate6etPrice(7ptional DiscountRate As Double :%#) AsCurrency Dim 7rigPrice As Double

    7rigPrice CCur(t*t8ar5edPrice) Calculate6etPrice 7rigPrice 9 CLng(7rigPrice " DiscountRate "&::) ; &::End unction

    Private Sub cmdCalculate1Clic5()t*t6etPrice Calculate6etPrice()

    End Sub

  • 8/13/2019 Accessories for Programming(Access)

    7/16

    >otice 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 ma'e sure

    you provide the desired value. #onsider the following call

    unction Calculate6etPrice(7ptional DiscountRate As Double :%#) AsCurrency

    Dim 7rigPrice As Double

    7rigPrice CCur(t*t8ar5edPrice) Calculate6etPrice 7rigPrice 9 CLng(7rigPrice " DiscountRate "&::) ; &::End unction

    Private Sub cmdCalculate1Clic5() Dim dblDiscount, 1 7ptional DiscountRate As Double :%#) AsCurrency Dim curDiscount.alue As Currency Dim curPriceA+terDiscount As Currency Dim cur=a*.alue As Currency Dim cur6etPrice As Currency

    curDiscount.alue CLng(7rigPrice " DiscountRate " &::) ; &:: curPriceA+terDiscount 7rigPrice 9 curDiscount.alue cur=a*.alue CLng(curPriceA+terDiscount " =a*Rate " &::) ; &::

    t*tDiscount.alue CStr(curDiscount.alue)

    t*tPriceA+terDiscount CStr(curPriceA+terDiscount) t*t=a*.alue CStr(cur=a*.alue)

  • 8/13/2019 Accessories for Programming(Access)

    9/16

    Calculate6etPrice curPriceA+terDiscount ! cur=a*.alueEnd unction

    Private Sub cmdCalculate1Clic5() Dim cur8ar5edPrice As Currency

    cur8ar5edPrice CCur(t*t8ar5edPrice) t*t6etPrice Calculate6etPrice(t*t8ar5edPrice)End Sub

    In reality, the Microsoft !isual "asic language allows you to create the procedure with the list of

    arguments as you see fit, as long as you ma'e sure you clearly specify which argument is optional and

    which one is re&uired. 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 re&uired

    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. ?emember that you must provide a value for any re&uired

    argument. #onsider the following e*ample

    unction Calculate6etPrice(7rigPrice As Currency, 1 7ptional =a*Rate As Double :%:>, 1 7ptional DiscountRate As Double :%#) AsCurrency Dim curDiscount.alue As Currency Dim curPriceA+terDiscount As Currency Dim cur=a*.alue As Currency

    Dim cur6etPrice As Currency

    curDiscount.alue CLng(7rigPrice " DiscountRate " &::) ; &:: curPriceA+terDiscount 7rigPrice 9 curDiscount.alue cur=a*.alue CLng(curPriceA+terDiscount " =a*Rate " &::) ; &::

    t*tDiscount.alue CStr(curDiscount.alue) t*tPriceA+terDiscount CStr(curPriceA+terDiscount) t*t=a*.alue CStr(cur=a*.alue) Calculate6etPrice curPriceA+terDiscount ! cur=a*.alueEnd unction

    Private Sub cmdCalculate1Clic5()

    Dim cur8ar5edPrice As Currency Dim dblDiscountRate

  • 8/13/2019 Accessories for Programming(Access)

    10/16

    Dim dbl=a*Rateew button on the toolbar of the ;atabase window

    6. @nder the ption $*plicit line, type the following

    =ype Employee Date2ired As Date ull6ame As String Fs8arried As /oolean 2ourlySalary As DoubleEnd =ype

    7. To save the module, on the 4tandard toolbar, clic' the 4ave button

    8. 4et the name to mod?outines and clic' D

    9. ?eturn to Microsoft Access and open the $mployee form in ;esign !iew

  • 8/13/2019 Accessories for Programming(Access)

    16/16

    33. #lose the form. When as'ed whether you want to save, clic' (es

    35. #lose Microsoft Access