5/5/11

How to Access VBA Programming for Absolute Beginners

Microsoft Access is a database program included in the Microsoft Office suite of applications. With Microsoft Access, create a database to suit your needs using Microsoft's Visual Basic for Applications (VBA) programming language, which is included with every copy of Microsoft Access. VBA is a complex programming language that can perform a variety of tasks and functions. There are a few basic guidelines that are applicable to every VBA macro, or piece of executable code that you write.
    • 1

      Start Microsoft Access, then click the "New Blank Database" icon, then click "Create."

    • 2

      Click the "Database Tools" tab on the Ribbon, then click the "Visual Basic" button. This will open the Visual Basic Editor (VBE).

    • 3

      Select "Insert" and then click "Module" to open a blank window.

    • 4

      Type the code into the module window. Every section of VBA code starts with either "Sub" or "Function" and ends with "End Sub or End Function." A subroutine is a piece of code that will return a value, where a function is a piece of code that will not. What code you write depends upon what data is in your database and what you want to do to the data. For example, you could use the following code to calculate a person's age:

      Function age(vb_Dte As Variant)

      Dim v_Age_Calc As Variant

      v_Age_Calc = DateDiff("yyyy", vb_Dte, Now)

      If Date < DateSerial(Year(Now), _

      Month(vB_Date), Day(vb_Dte)) Then v_Age_Calc = v_Age_Calc - 1

      age = CInt(v_Age_Calc)

      End Function

    • 5

      Press "F5" to run the function.

  • No comments: