0% found this document useful (0 votes)
334 views

MS Access Tutorial

- The document discusses forms in Microsoft Access, including what forms are used for, how to create a basic form, and how to configure common form properties like the record source, caption, and default view. - It provides steps for adding fields from the record source table to the form, including resizing and positioning the fields. - The document concludes by demonstrating how to view the form, save it, and then moves on to discuss combo boxes in Access forms.

Uploaded by

Suzana Petrović
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
334 views

MS Access Tutorial

- The document discusses forms in Microsoft Access, including what forms are used for, how to create a basic form, and how to configure common form properties like the record source, caption, and default view. - It provides steps for adding fields from the record source table to the form, including resizing and positioning the fields. - The document concludes by demonstrating how to view the form, save it, and then moves on to discuss combo boxes in Access forms.

Uploaded by

Suzana Petrović
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 90

MS Access Tutorial: Form Basics in Access 2003/XP/2000/97 (page 1 of 12)

Return to Tutorial IndexNext >>

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.

Record Source property


The Record Source property sets the source of the data for the form. For example, if you want to create a data entry form for the Suppliers table, you would specify Suppliers as the Record Source. This means that if you add a new record in the form, it will insert a record into the Suppliers table. If you modified a record within this form, the data in the Suppliers table would also be modified. The Record Source property can be set to a table name, query name, or you can build your own SQL statement by invoking the Query builder (ie: clicking on the button with the three dots to the right of the Record Source property)

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.

Default View property


The next property that we will take a look at is called the Default View property. If you look at the options available, there are three values that you can choose from - single form, continuous forms, and datasheet.

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.

Auto Center property


Another commonly used property is Auto Center. This property can be set to either Yes or No. If this property is set to Yes, the form will center itself automatically when it is opened. If this property is set to No, the form will appear in exactly the same position as you saved it (when you were in Design view).

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

Auto Center = Yes

Now, let's build our form.

Adding objects to our form


Our next step is to add objects from the Suppliers table to our form. Since we've already specified our Record Source, Access gives us some nice features to quickly drop objects onto our form. In the toolbar, there is a button called Field List. It looks like a piece of paper with writing on it. Click on this button so that it is selected.

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.

Viewing your form as a user would


Now that you have finished placing your objects on the form, you will want to view the form as a user would. This whole time you've been editing your form in Design view. To view your form, select Form View from the drop-down list on the toolbar. See picture below.

Our form looks like this:

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.

Congratulations! You've now created your first form.

MS Access Tutorial: Combo Box Basics in Access 2003/XP/2000/97 (page 1 of 15)


Return to Tutorial IndexNext >>

What is a combo box?


A combo box is a drop-down list of values that the user can select from. Below is a combo box that has been set up to display a list of categories.

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.

We've set the form's Record Source property to Products.

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.

Let's begin configuring our combo box.

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.

Control Source property


One of the most important items to configure is the Control Source property. If you remember, we set the Record Source for our form to the Products table. Now we want to specify what field in the Products table this combo box is bound to. So we'll set the Control Source property to CategoryName.

Row Source Type property


When configuring the Row Source Type property, there are 3 values to choose from. 1. The first option is Table/Query. This means that the values for your combo box are populated by a table or query. 2. The second option is Value List. Your values are not stored in tables, so you "hard code" the values, so to speak.

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.

Row Source property


If you'll remember, we selected the Row Source Type property of Table/Query. Now need to create a query to populate the combo box values. Click on the button (with the three dots) to the right of the Row Source property.

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.

Column Count property


The next property is Column Count. This property indicates how many columns so wish to display in our combo box. Since our query that we built only displays the CategoryName, we will set the Column Count property to 1.

Column Widths property


The Column Widths property indicates how wide each column in the combo box will display. Since we've only selected one column to display (CategoryName), we can leave this property blank.

Bound Column 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.

List Width property


The List Width property tells the combo box how wide to make the drop-down portion of the combo box. If you set this value to Auto, then the combo box will automatically size the dropdown portion. In this tutorial, we'll set the List Width property to Auto.

Limit To List property


The Limit To List property tells Access whether to allow a user to select a value that is not in the list. This property can either be set to Yes or No. If the property is set to Yes, then Access will not allow the user to enter a value that isn't in the list. If a user tries to enter a value that is not contained in the combo box, the following message will appear:

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.

We've set the Limit To List property to Yes.

Changing caption on label


Now that we've configured our combo box, we'll want to change the caption on the combo box label to something more meaningful. Currently, the label reads "Combo0".

Select the label object on the form to view its properties. Set the Caption property to "Category Name:"

MS Access Tutorial: Tab Control in Access 2003/XP/2000/97 (page 1 of 8)


Return to Tutorial IndexNext >>

What is a Tab Control?


A Tab Control is an Access object that allows you to create tabbed pages on an Access form. See picture below.

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:

General Address Shipping Info

Creating a Tab Control


Let's take a look at how to create a Tab Control. First, open your Form in Design View. Then select the Tab Control object in the Toolbox.

Then left-click on the Form where you'd like the Tab Control to appear.

Your Tab Control object should now appear on your form.

Naming your Tab Pages


To name your tab pages, highlight the Page in your Tab Control that you wish to name.

Then under the View menu, select Properties.

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".

Now, when we view the Tab Control, it looks as follows:

Adding objects on the Tab Pages


The next thing you'll want to do is add objects (ie: text boxes, combo boxes, etc) to your tab pages. Select the tab page that you'd like to add the objects to. In our example, we're going to add objects to our General page. Now, just drag and drop your objects onto your tab page.

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.

dding Tab Pages


By default, your Tab Control will be created with 2 tab pages. But if you want more tab pages, it's quite easy to customize your Tab Control to display as many pages as you'd like. To create a tab page, right-click on the Tab Control and select "Insert Page" from the pop-up menu.

A new page should appear on your Tab Control.

Reordering Tab Pages


Once you've started adding tab pages, you may find that you want to reorder how your pages appear. To do this, select the page that you'd like to move and change the Page Index property for that page. Here's how the Page Index property works: The first page on your Tab Control has a Page Index value of 0. The second page on your Tab Control has a Page Index value of 1. The third page on your Tab Control has a Page Index value of 2. and so on... In this example, we want Page3 to appear as the first tab, so we've change the Page Index property to 0.

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.

Deleting Tab Pages


Once you've started customizing your Tab Control, you may find that you wish to delete a tab page. To do this, select the page that you wish to delete. Right-click and select "Delete Page" from the popup menu. In this example, we've chosen to delete Page2.

Now when you view your Tab Control, you can see that Page2 has been deleted.

Displaying Labels on Multiple Rows

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.

Congratulations! You've now created your first Tab Control.

MS Access Tutorial: Pass-through Queries in Access 2003/XP/2000/97 (page 1 of 12)


Return to Tutorial IndexNext >>

What is a Pass-through Query?


A Pass-through query allows you to execute an SQL statement directly against the tables in an external database (such as an Oracle, Sybase, or SQL Server database). This method uses your ODBC connection to directly access the data in the external database without having to link to the tables or import the data from the external database.

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.

Create an ODBC connection


The first step is to create an ODBC connection to your external database. This ODBC connection will be used by the pass-through query to access the tables directly in your external database. If you don't already have an ODBC connection defined, you can create one by selecting Data Sources (ODBC) under the Control Panel.

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.

Create a Pass-through Query


Now that you've set up your ODBC connection, you can begin to setup your pass-through query. To do this, create a new Query. When the New Query window appears, select Design View.

When the Show Table window appears, click on the Close button.

You should now have a blank query that looks like the one below.

Write SQL for your Pass-through Query


Since you are choosing to create a pass-through query instead of linking to the table, Access does not know the table or field names in your external database. So you will have to write your SQL without the help of the Query Builder in Access. As a result, you'll need to switch your query to the SQL View. To do this, select SQL View from the Toolbar. See picture below.

Your query should now look like this.

Now, type in your SQL statement.

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.

ag the query as a Pass-through Query


Now that you've created your SQL, you need to let Access know that you intend to use this query as a pass-through query. To do this, select SQL Specific > Pass-Through under the Query menu.

Your query should now display "Pass-Through Query" in the title bar on the Query window. See below.

Link the Query to the ODBC connection


Now, it's time to link the query to the ODBC connection that you configured at the beginning of this tutorial. To do this, under the View menu, select Properties.

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.)

Run the Pass-through Query


Now that we've configured our pass-through query, let's try running it.

You can execute the query by selecting the Run button on the Toolbar.

Below are the results from our pass-through query.

Now, let's take a look at how we can integrate our pass-through query with our other Access queries.

Integrating a Pass-through Query with a Make-Table Query


One of the limitations of using a pass-through query is that it can only access data from your external database. So if you need to move the data from the external database into your Access database, you can create a Make-Table query that uses the pass-through query as input. Let's take a look at an example. First, create a new Query. When the Show Table window appears, click on the Queries tab. Then select the pass-through query that you just created. In this example, our pass-through query is called Query1. Then click on the Add button.

Next, select the fields from Query1.

Under the Query menu, select Make-Table Query.

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.

Integrating a Pass-through Query with an Append Query


If your table already exists in Access, but you need to move the data from the external database into this table, you can create an Append query that uses the pass-through query as input. Let's take a look at an example. First, create a new Query. When the Show Table window appears, click on the Queries tab. Then select the pass-through query that you just created. In this example, our pass-through query is called Query1. Then click on the Add button.

Next, select the fields from Query1.

Under the Query menu, select Append Query.

When the Append window appears, enter the name of the table that you wish to append records to. Click on the OK button.

Your Append query should now look like this.

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.

MS Access Tutorial: Report Basics in Access 2003/XP/2000/97 (page 1 of 12)


Return to Tutorial IndexNext >>

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.

Record Source property


The Record Source property sets the source of the data for the report. The Record Source property can be set to a table name, query name, or you can build your own SQL statement by invoking the Query builder (ie: clicking on the button with the three dots to the right of the Record Source property).

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));

Caption = Suppliers Report

Now, let's build our report.

Adding objects to our report


Our next step is to add objects from the Suppliers table to our report. Since we've already specified our Record Source, Access gives us some nice features to quickly drop objects onto our report. In the toolbar, there is a button called Field List. It looks like a piece of paper with writing on it. Click on this button so that it is selected.

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.

We've chosen to set the text to Arial 14.

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.

Viewing your report as a user would


Now that you have finished placing your objects on the report, you will want to view the report as a user would. This whole time you've been editing your report in Design view. To view your report, select Print Preview from the drop-down list on the toolbar. See picture below.

Our report looks like this:

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.

Congratulations! You've now created your first report.

MS Access Tutorial: VBA Basics in Access 2003/XP/2000/97 (page 1 of 7)


Return to Tutorial IndexNext >>

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.

How to get to the VBA environment


You can access the VBA environment by opening up the Microsoft Visual Basic window. The quickest way to do this is by pressing Alt-F11 while your Access database file is open.

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.

Objects displayed in the Project Explorer


The next important aspect to discuss is what objects appear in the Project Explorer. At first glance, you'd assume that all of the Forms, Reports, and Modules that you create will automatically appear in the Project Explorer. But this is not true. The VBA environment only knows about the objects that you tag as "having a module". So by default, all Modules will appear in the Project Explorer, but not all Forms and Reports necessarily will. A Form will only appear in the Project Explorer if the Form's "Has Module" property is set to "Yes".

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.

MS Access Tutorial: Debugging VBA Code in Access 2003/XP/2000/97 (page 1 of 12)


Return to Tutorial IndexNext >>

VBA's Debugging Environment


VBA's debugging environment allows the programmer to momentarily suspend the execution of VBA code so that the following debug tasks can be done: 1. 2. 3. 4. 5. Check the value of a variable in its current state. Enter VBA code in the Immediate window to view the results. Execute each line of code one at a time. Continue execution of the code. Halt execution of the code.

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.

Clearing all Breakpoints


Because you can set as many breakpoints as you want, you can save time by clearing all breakpoints in your VBA code at once. To clear all breakpoints in your program, select "Clear All Breakpoints" under the Debug menu.

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.

Using the Immediate Window


The Immediate window can be used to debug your program by allowing you to enter and run VBA code in the context of the suspended program. We've found the Immediate window to be the most help when we need to find out the value of a variable, expression, or object at a certain point in the program. This can be done using the print command. For example, if you wanted to check the current value of the combo box called cboKeyword, you could use the print command as follows:

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.

Choose how to move through your code


Now that you've suspended execution of your program and are in debug mode, you need to choose how to proceed through your code. We'll cover the 4 most common choices. 1. Step Into 2. Step Over

3. Continue 4. Halt Let's take a look at each of these.

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.

You might also like