MS Access Tutorial
MS Access Tutorial
What is a form?
A form is an Access object. It generally serves three purposes: 1. To allow users to perform data entry. Data can be inserted, updated, or deleted from a table using a Form object. 2. To allow users to enter custom information, and based on that information perform a task. For example, you may want to ask a user for parameters before running a report. 3. To allow users a method of navigating through the system. For example, you may create a form where a user can select a form to load, a report to run, etc.
Within your Access database, you can view all of your form objects in the Database Window. See below.
Creating a Form
Let's take a look at creating a new form "from scratch". In the database window, select Forms along the left toolbar. Then click on the New button.
The New Form window should appear. For the purposes of this tutorial, highlight the option called Design View and click on the OK button.
We are now viewing our form in design view. You should see a blank form that looks like this:
Under the View menu, select Properties. You should now see the properties window for the Form object.
Let's take a few moments to explore some of the more commonly used properties for a Form object.
Caption Property
The Caption property is the value that displays in the title bar of your form. We'll set the Caption property to display "Add/Edit Suppliers".
Now when we view the Form as an end-user would, it would look like this:
As you can see there is a title bar at the top of the form and it displays the value that we set in the Caption property.
Single Form
The Single Form option allows a user to view one record at a time in the form. Below, we have a sample form that displays one record from the Suppliers table. At the bottom of the form, you will see (what we call) record selectors. This allows you to navigate from one record to another. You can also see that there are 29 records that you can navigate to in this form.
Continuous Forms
Another option is called Continuous Forms. This option allows you to see multiple records in the form at once. The scroll bar on the right allows you to scroll through the records. At the bottom of the form, you are still given the record selectors to navigate through the records. In this sample, there are 77 records that can be viewed in this form.
Datasheet
Our third option is called Datasheet. This option makes the form appear as if you are looking at the actual table. In the datasheet mode, you can resize the columns or even reorder the columns by highlighting and dragging the column to another location. You are still given the scroll bar on the right to navigate, as well as the record selectors at the bottom of the form.
That covers the more commonly used properties on a Form object. For the purposes of this tutorial, we've set the Form properties as follows: Record Source = Suppliers Caption = Add/Edit Suppliers Default View = Single Form
Once you do this, you should have a small window appear on your screen that looks like this.
This window lists all of the fields that are available for your form to use, based on the Record Source property. To add one of these fields to your form, highlight the object in the window, and then drag it to the location in the form where you wish to add this object. We've chosen to add the following three objects to our form.
The first thing that you'll probably want to do is resize your objects. To do this, click the object that you wish to resize.
You should notice small black boxes around the perimeter of the object. Move your mouse pointer over one of these "resizing boxes". When your mouse pointer displays double-headed arrow, hold down the left mouse button and drag the object to the desired size. If you wish to move the object, move your mouse pointer until it displays a hand icon. Then hold down the left mouse button and drag the object to the desired location.
Saving form
One final step is to save our Form object. You can do this at any time during creation of the form, but for the purposes of this tutorial, we are saving this as our last step. Click on the Save button in the toolbar. (disk icon) A "Save As" window should appear as follows:
Select a name for the form and click on the OK button. We usually always prefix our form name with "frm". It is just a naming convention that we use. Now when you return to the Database Window, you will see your form in the list.
Combo boxes generally serve the following purposes. 1. Standardize the values that a user can enter, since the user is presented with a list of values to choose from. 2. Make user selections faster and easier. 3. Allow the user to view multiple columns within a single combo box, providing a vast amount of information at a single glance.
For the purposes of this tutorial, let's take a look at how to create and configure a combo box object. We've created a form called frmProducts that displays Product ID and Product Name. See picture below.
Next, select the combo box object from the toolbar and click on the form where you'd like the combo box to appear. The Combo Box Wizard will most likely appear.
Click on the Cancel button. We'll be configuring the combo box without the help of the wizard. You should now see your combo box object appear on the form.
Name property
We'll set the Name property to CategoryName, since this is the value that we want to display in our combo box. It's recommended that you set your Name property to something more meaningful. Try not to use the default name that Access gives it, such as Combo0, Combo1, etc.
3. The third option is Field List. You wish to populate your combo box with an actual list of field names from a table. For the purposes of this tutorial, we will choose option #1. We'll set our Row Source Type property to Table/Query.
A Show Table window will appear. Highlight the table that you wish to use in your query and then click on the Add button. Then click on the Close button.
In this example, we've added the Categories table. You should see your table(s) in the Query Builder window.
Next, select the field(s) that you want to display in your combo box. In this example, we want to display the CategoryName, sorted in ascending order.
Click on the X in the top right corner of the Query Builder window. You should get the following prompt. Click on the Yes button.
Your Row Source property should now contain the SQL statement that you built in the Query Builder. If you wish to edit the query in the future, you can click on the button (with the three dots) to the right of the Row Source property.
The Bound Column property indicates which column to bind to for the control. If you'll remember, we specified that our combo box is bound to the CategoryName field in the Products table. The Bound Column property tells us which column in our combo box to bind to the CategoryName field. Since our combo box only displays one column, we set this property to 1.
The user will be forced to re-select a valid entry from the combo box. If the property is set to No, then the user can either select a value from the list or enter of value of his/her own. Please note that if a user enters a value that is not in the list, the value will not be automatically added to the list. This takes more extensive code that will not be addressed in this tutorial.
Select the label object on the form to view its properties. Set the Caption property to "Category Name:"
This is particularly useful if you have more objects (ie: text boxes, combo boxes, etc) than is possible to display on a regular Access form without having to scroll up or down. A Tab Control can help you display these objects on your Form by displaying tabbed pages. You can then group objects on the various pages. In this example, we have tab pages for:
Then left-click on the Form where you'd like the Tab Control to appear.
The Name property is object name for the tab page, while the Caption property is the value that will display in the label section of the tab page. In this example, we're going to call the tab page "PageGeneral" and we'll change the caption to display "General".
In this example, we've created an OrderID and an OrderDate field on the General tab.
One thing to note about tab pages is that the objects that you create on a tab page, only appear on that page. As you can see, if you select Page2 of the Tab Control, you no longer see the OrderID and OrderDate fields.
Now when you view your Tab Control, you can see that Page3 displays as the first tab page.
You can also reorder your tab pages, by right-clicking and selecting "Page Order" from the popup menu.
When the Page Order window appears, move the order of your tab pages by highlighting the page and clicking on either the "Move Up" or "Move Down" button.
Now when you view your Tab Control, you can see that Page2 has been deleted.
As you customize your Tab Control, you may find that you have more tab pages than can display on your screen and arrows appear to the right of your page labels (see picture below).
If you'd rather have the page captions appear on multiple rows, select the Tab Control object (not a page, but the whole Tab Control). In the Property window for the Tab Control, change the Property called "Multi Row" to Yes.
Now when you view your Tab Control, you can see that the tab captions appear in multiple rows.
This tutorial will demonstrate how to create pass-through queries as well as how to integrate pass-through queries with other queries in your Access database.
When the ODBC Data Source Administrator window appears, select the User DSN tab. Then click on the Add button.
When the Create New Data Source window appears, select the ODBC driver that you wish to use. In this example, we've selected the driver called "Microsoft ODBC for Oracle". Click on the Finish button.
Next, enter the Data Source Name, Description, User Name, and Server for the ODBC connection. Then click on the OK button.
Now, when you return to the ODBC Data Source Administrator window, you should see the ODBC connection that you've just created.
When the Show Table window appears, click on the Close button.
You should now have a blank query that looks like the one below.
In this example, we've entered the following SQL statement: SELECT supplier_id, supplier_name, address_line1, address_line2, city, state FROM suppliers Now, you'll notice that we left out the semicolon at the end of the SQL statement. Some of the external databases, do not like the SQL statement terminated with a semicolon, so you may have to try your SQL statement with a semicolon and without a semicolon, to see which it accepts as proper syntax.
Your query should now display "Pass-Through Query" in the title bar on the Query window. See below.
When the Query Properties window appears, click on the property called "ODBC Connect Str". A button with 3 dots to the right of the property should appear. Click on this button.
When the "Select Data Source" window appears, highlight your ODBC connection that you configured at the beginning of this tutorial. Then click on the OK button.
When the ODBC login window appears, enter your password and click on the OK button.
You will be asked if you wish to save the password in the connection string. Since the connection string is visible (and not protected) within your Access database, we recommend you not save your password for security reasons. In our example, we will select No to this prompt.
Now when we return to the Query Properties window, you can see the ODBC connection string that you've just built. (Note that the password is not contained in this string, so you will be prompted to enter a password when you run your pass-through query.)
You can execute the query by selecting the Run button on the Toolbar.
Now, let's take a look at how we can integrate our pass-through query with our other Access queries.
When the Make Table window appears, enter the name of the table that you wish to create. Click on the OK button.
Now, when you run your Make-Table query, your new table will show in the Database window and it will be populated with data from your external database.
When the Append window appears, enter the name of the table that you wish to append records to. Click on the OK button.
Now, when you run your Append query, your table will be populated with data from your external database. Congratulations! You've now:
Created an ODBC connection Configured your first pass-through query Learned how to integrate a pass-through query with other queries in Access.
What is a report?
A report is an Access object. It is used to display your data in an organized manner so that users can print it.
Within your Access database, you can view all of your report objects in the Database Window. See below.
Creating a Report
Let's take a look at creating a new report "from scratch". In the database window, select Reports along the left toolbar. Then click on the New button.
The New Report window should appear. For the purposes of this tutorial, highlight the option called Design View and click on the OK button.
We are now viewing our report in design view. You should see a blank report that looks like this:
Under the View menu, select Properties. You should now see the properties window for the Report object.
Let's take a few moments to explore some of the more commonly used properties for a Report object.
In this example, we are going to invoke the Query builder by clicking on the button with the three dots to the right of the Record Source property.
When the Show Table window appears, select the table(s) that you'd like to use to populate your report. In our example, we've selected the Suppliers table. Then click on the Add button.
Once you've added all of the tables that you need, click on the Close button.
When the Query Builder window appears, you can now build your SQL statement that will be used to populate your report. In this example, we've chosen to select all fields from the Suppliers table, but only those records where the SupplierID is less than 10. You can now click on the X button on the top right of the window.
When prompted with the message box, click on the Yes button.
Now when you return back to the Properties window for the Report object, you should see your SQL in the Record Source property.
Caption Property
The Caption property is the value that displays in the title bar of your report. We'll set the Caption property to display "Suppliers Report".
Now when we view the Form as an end-user would, it would look like this:
As you can see there is a title bar at the top of the report and it displays the value that we set in the Caption property. That covers the more commonly used properties on a Report object. For the purposes of this tutorial, we've set the Report properties as follows: Record Source = SELECT Suppliers.* FROM Suppliers WHERE ((([Suppliers]. [SupplierID])<10));
Once you do this, you should have a small window appear on your screen that looks like this.
This window lists all of the fields that are available for your report to use, based on the Record Source property. To add one of these fields to your report, highlight the object in the window, and then drag it to the location in the report where you wish to add this object. Return to Tu We've chosen to add the following four objects to our report. These have been added to the Detail section of the report.
The first thing that you'll probably want to do is resize your objects. To do this, click the object that you wish to resize.
You should notice small black boxes around the perimeter of the object. Move your mouse pointer over one of these "resizing boxes". When your mouse pointer displays a double-headed arrow, hold down the left mouse button and drag the object to the desired size. If you wish to move the object, move your mouse pointer until it displays a hand icon. Then hold down the left mouse button and drag the object to the desired location.
You should also resize your Detail section of your report. To do this, move your mouse pointer over the bottom of the detail section until your pointer displays a double-headed arrow, then hold down the left mouse button and drag until the Detail section is the desired size.
Next, you probably want to set up a Page Header. This will be the text that displays at the top of each of your report pages. To do this, select the Label icon from the Toolbox. This is the icon that has the capital and small letter A's.
Then left-click in the Page Header section of your report and drag to size the Label object. Enter the text that you'd like to see displayed on each page. In our example, we've typed "Suppliers Report".
You can resize the text using the text font and size options in the Toolbar.
Next, you might want to add page numbers to the bottom of your report. To do this, under the Insert menu, select Page Numbers.
When the Page Numbers window appears, you can select how you'd like the page numbering to appear. In this example, we've chosen to see "Page N of M" as the format, the page numbers will appear at the bottom of the page (ie: Footer section), and the numbers with appear in the righthand side on the footer.
Now, when you view your report, you can see that Access has inserted the Page Numbers in the Page Footer section.
Saving report
One final step is to save our Report object. You can do this at any time during creation of the report, but for the purposes of this tutorial, we are saving this as our last step. Click on the Save button in the toolbar. (disk icon) A "Save As" window should appear as follows:
Select a name for the report and click on the OK button. Now when you return to the Database Window, you will see your report in the list.
What is VBA?
VBA standards for Visual Basic for Applications and is the language embedded within your Access database. You use VBA whenever you do one of the following: 1. 2. 3. 4. 5. Create a new function. Create a new subroutine. Define a global variable. Place code behind an event procedure such as the "On Click" event of a command button. Execute the RunCode action in a macro.
These are just some of the examples of when you might be running VBA code.
This is an example of what the Microsoft Visual Basic window looks like. Next, let's take a few moments to analyze the various sections in the Microsoft Visual Basic window.
Project Explorer
The Project Explorer can usually be found in the top left portion of the Microsoft Visual Basic window. It is a hierarchical listing of the objects recognized by VBA. In this example, there are two "Microsoft Access Class Objects" - one is a form called frmProducts and the other is a report called rptCategories_Report. There is also one Module called Module1. These are all objects that you've created in your Access database. If the Project Explorer is not visible when you open the Microsoft Visual Basic window, you can make it visible by selecting Project Explorer under the View menu.
Properties Window
The Properties window is usually found directly below the Project Explorer. It displays the properties for the object currently highlighted in the Project Explorer. In the example above, it is displaying the properties for the module called Module1. If the Project Explorer is not visible when you open the Microsoft Visual Basic window, you can make it visible by selecting Properties Window under the View menu.
Code Window
The Code window is usually found to the right of the Project Explorer. It displays the VBA code for the object currently highlighted in the Project Explorer. In the example above, it is displaying the VBA code for the module called Module1.
If the Code window is not visible when you open the Microsoft Visual Basic window, you can make it visible by selecting Code under the View menu.
Immediate Window
The Immediate window is usually found below the Code window. It is an essential element of the debugger found within the VBA environment. It lets you:
Type code and press ENTER to view the results of the code. When in debug mode, it lets you view the value of a variable in its current state. This will be discussed in the tutorial on Debugging VBA Code.
If the Immediate window is not visible when you open the Microsoft Visual Basic window, you can make it visible by selecting Immediate Window under the View menu.
Similarly, a Report will only appear in the Project Explorer if the Report's "Has Module" property is set to "Yes".
Now, you're probably asking yourself, "How come some of the forms and reports in my Access database have the "Has Module" property set to "Yes" and others don't?" Well, anytime that you invoke the Code Builder in a Form/Report, Access automatically sets Form/Report's "Has Module" property to "Yes".
Therefore, if you want to reference one of your Forms or Reports in the VBA environment, you may need to manually go to the Form/Report's Properties window and set the "Has Module" property to "Yes". Congratulations! You've just completed the tutorial on understanding the VBA environment.
These are just some of the tasks that you might perform in VBA's debugging environment.
What is a Breakpoint?
A breakpoint is a selected line of code that once reached, your program will momentarily become suspended. Once suspended, you are able to use VBA's debugging environment to view the status of your program, step through each successive line of code, continue execution of the code, or halt execution of the code. You can create as many breakpoints in your code as you want. Breakpoints are particularly useful when you want to suspend your program where you suspect a problem/bug exists.
Setting a Breakpoint
Next, let's take a look at how to set a breakpoint. First, you need to access the VBA environment. The quickest way to do this is by pressing AltF11 while your Access database file is open. To set a breakpoint, find the line of code where you'd like to suspend your program. Left-click in the grey bar to the left of the code. A red dot should appear and the line of code should be highlighted in red.
In this example, we've created a breakpoint at the following line of code: If IsNull(cboKeyword) = True Then
Clearing a Breakpoint
To clear a breakpoint, left-click on the red dot next to the line of code that has the breakpoint.
In this example, we want to clear the breakpoint at the following line of code: If IsNull(cboKeyword) = True Then
Now, the breakpoint is cleared and the line of code should look normal again.
This will remove all breakpoints from your VBA code, so that you don't have to individually remove each breakpoint, Now that we know how to set and clear breakpoints, let's take a closer look at the debug mode in VBA. In our example, we've set our breakpoint and executed the VBA code by clicking on the command button called cmdRunReport.
When the breakpoint is reached, Access will display the Microsoft Visual Basic window and highlight the line (in yellow) where the code has been suspended. one by one.
Checking values
If you want to view the value of a variable or object (ie: text box, combo box, etc) in your VBA code at the time that the program was suspended, you can move your mouse pointer over that VBA code. Bubble text will appear displaying the variable/object name and its value in its current state.
In this example, we've moved the mouse pointer over the combo box called cboKeyword. The bubble text displays the cboKeyword combo box name with a value of "monitor". This feature is useful if you need to quickly check a variable/object in your code.
In this example, we typed print cboKeyword And pressed ENTER. The Immediate window displayed the result in the next line. In this case, the print cboKeyword command returned monitor.
You can also type more complicated expressions in the Immediate window. (Remember to press ENTER.) For example:
The Immediate window can be used to run other kinds of VBA code, but bear in mind that the Immediate window can only be accessed when debugging so any code that you run is for debugging purposes only.
In this example, the Immediate window can be used to open a report called rptEntries.
Step Into
You can choose to "Step Into" your code. What this means is that you will step through each line of code in your current procedure as well as step into the code of any procedures that are called by the current procedure. You can do this by either pressing the F8 key or selecting "Step Into" under the Debug menu.
Each time you select "Step Into", the debugger will move you to the next line of code.
Step Over
You can choose to "Step Over ". What this means is that you will step through each line of code in your current procedure, but step over (ie: run as a unit) any procedures that are called by the current procedure. You can do this by either pressing the Shift-F8 key or selecting "Step Over" under the Debug menu.
Each time you select "Step Over", the debugger will move you to the next line of code in your current procedure. If your current procedure calls another procedure, the debugger will step over the called procedure. It won't drill down into the code of the called procedure.
Continue
You can choose to "Continue" execution of your code. What this means is that your suspended program will continue executing from where it left off. With this option, your program should either finish running or suspend at the next breakpoint encountered. You can do this by either pressing the F5 key or selecting "Continue" under the Run menu.
Halt
You can choose to "Halt" execution of your code. What this means is that your suspended program will halt execution. You will no longer be in debug mode. You can do this by selecting "Reset" under the Run menu.
You would normally select this option after you've identified the bug in your code. Once your code is halted, you can modify your VBA code to remedy the problem. Congratulations! You've just completed the tutorial on Debugging VBA Code.