IT tutorials
 
Office
 

Microsoft Access 2010 : TESTING AND DEBUGGING VBA CODE (part 1) - Immediate Window, The Debug.Print Statement

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Product Key Free : Microsoft Office 2019 – Serial Number
5/4/2013 8:56:44 PM

The Code window is where you actually write your code, including the subroutines, functions, and declarations. In addition to the Code window, you'll use other components of the VBA Editor to test and debug your code. The following sections look at each of those components.

Like other things in database design, there is not one definitive right answer for when to debug your VBA code; it's also normal for developers to use different approaches depending on the situation. You could debug as you write, testing every few lines. That can be quite time-consuming, as you would have to run your code every few lines (possibly with incomplete procedures) and make heavy use of tools such as the Immediate window and Watch statements discussed later in this section. The advantage of this approach is that you always know the value of your variables, and the likelihood of making or perpetuating a mistake is reduced.

Another approach is to write all the code for your application and then debug it. Although this approach might seem tempting because it doesn't require you to stop your coding to debug your application, you can easily end up with numerous errors — some of which could require you to make major changes to your code. Using that technique can be like peeling an onion, particularly as the code becomes more complex and interdependent, with one function calling another function.

The best debugging approach falls somewhere between those two options. Debugging as you go — testing and debugging your code at the end of each procedure — enables you to be confident that each procedure produces the appropriate and expected values. If you are writing particularly long procedures, you may want to debug the procedure more frequently. Otherwise, this approach should be sufficient to ensure you're not in too deep with too many errors.

Figure 1.

1. Immediate Window

The Immediate window in the Visual Basic Editor enables you to enter commands and view the contents of variables while your code is in break mode. Press Ctrl+G or select View => Immediate Window to open the window, as shown in Figure 2.

You can display the value of a variable in the Immediate window by either using the ? <Variable Name> or the Debug.Print <Variable Name> in the window. Just type ? along with the variable name and press Enter. VBA Editor displays the contents of the variable in the Immediate window. For example, typing the following and pressing Enter displays the value of intNumEmployee in the Immediate window:

? intNumEmployees

Figure 2.

Seeing the current value of a variable can help you troubleshoot code if you're encountering unexpected results. Simply set a breakpoint in your code and test the value of a variable at any time. This enables you to determine where in the code the value is being incorrectly calculated. Using the question mark in the Immediate window is shorthand for typing Debug.Print. So typing ? intNumEmployees or Debug.Print intNumEmployees produces the same results when you press Enter.

In addition to displaying the value of variables, you can also execute VBA commands in the Immediate window. Just eliminate the ? character and type the entire command, and then press Enter. Typing msgbox("Coffee or Tea?") and pressing Enter displays the message shown in Figure 3.

Figure 3.

You can even perform calculations in the Immediate window such as:

? intTotalEmployees = intTempEmployees + intFullTimeEmployees.
2. The Debug.Print Statement

We previously mentioned using statements in the Immediate window, they are also useful in other places. The following module demonstrates how to use Debug.Print in your code. In looking at this example, you might imagine how Debug.Print can be helpful for testing and debugging.

Sub FunWithStringsAndNumbers()
    Dim strBikes As String
    Dim strCost As String
    Dim strCustomerName As String
    Dim intBikes As Integer
    Dim curCost As Currency

    strBikes = "19"
    strCost = "74"
    strCustomerName = "The Space Needle Seattle, WA"
    intBikes = 19
    curCost = 74

    Debug.Print strBikes + strCost
    Debug.Print intBikes + curCost
    Debug.Print strCustomerName
End Sub
                                                           
This code will show the following output in the Immediate window when you run it:
1974
93
The Space Needle Seattle, WA

Displaying the results of calculations or values of variables in the Immediate Window is one of the ways you can test your procedure. If the results aren't what you expect, you know that you have a problem somewhere; you may need to determine if it is in the initial data provided, the code, or the underlying logic.

 
Others
 
- Microsoft Access 2010 : Using the VBA Editor - ANATOMY OF THE VBA EDITOR, USING THE OBJECT BROWSER
- Microsoft Visio 2010 : Connecting Shapes - Understanding Visio Connectors (part 2) - Connecting to Shapes versus Points on Shapes
- Microsoft Visio 2010 : Connecting Shapes - Understanding Visio Connectors (part 1) - Connecting Basics
- Microsoft PowerPoint 2010 : Working with Animation and Transitions - Setting Slide Transitions
- Microsoft PowerPoint 2010 : Finalizing Your Slide Show - Rehearsing Your Presentation
- Microsoft Project 2010 : Creating Master Schedules with Inserted Projects - Reporting and Analyzing Across Projects
- Microsoft Project 2010 : Creating Master Schedules with Inserted Projects - Critical Path Across Projects
- Microsoft Excel 2010 : Linking a Cell to Smart Art
- Microsoft Excel 2010 : Inserting SmartArt Images
- Microsoft Excel 2010 : SmartArt
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us