Microsoft office access 2010 vba programming for dummies
->>>> Click Here to Download <<<<<<<-
Contents Exit focus mode. Is this page helpful? Please rate your experience Yes No. Any additional feedback? Note Interested in developing solutions that extend the Office experience across multiple platforms? In this article. For example, you could write some VBA code that displays a pop up message that reminds users to save a document to a particular network drive the first time they try to save it.
This article explores some of the primary reasons to leverage the power of VBA programming. It explores the VBA language and the out-of-the-box tools that you can use to work with your solutions.
Finally, it includes some tips and ways to avoid some common programming frustrations and missteps. Interested in developing solutions that extend the Office experience across multiple platforms? Check out the new Office Add-ins model. VBA is effective and efficient when it comes to repetitive solutions to formatting or correction problems. For example, have you ever changed the style of the paragraph at the top of each page in Word?
Have you ever had to reformat multiple tables that were pasted from Excel into a Word document or an Outlook email? Have you ever had to make the same change in multiple Outlook contacts? If you have a change that you have to make more than ten or twenty times, it may be worth automating it with VBA.
If it is a change that you have to do hundreds of times, it certainly is worth considering. Almost any formatting or editing change that you can do by hand, can be done in VBA.
There are times when you want to encourage or compel users to interact with the Office application or document in a particular way that is not part of the standard application. For example, you might want to prompt users to take some particular action when they open, save, or print a document.
Do you need to copy all of your contacts from Outlook to Word and then format them in some particular way? Or, do you need to move data from Excel to a set of PowerPoint slides? Sometimes simple copy and paste does not do what you want it to do, or it is too slow. You can use VBA programming to interact with the details of two or more Office applications at the same time and then modify the content in one application based on the content in another.
VBA programming is a powerful solution, but it is not always the optimal approach. Sometimes it makes sense to use other ways to achieve your aims. The critical question to ask is whether there is an easier way. Before you begin a VBA project, consider the built-in tools and standard functionalities.
For example, if you have a time-consuming editing or layout task, consider using styles or accelerator keys to solve the problem. Can you create a new document with the correct format or template, and then copy the content into that new document? Office applications are powerful; the solution that you need may already be there. Take some time to learn more about Office before you jump into programming. Programming requires focus and can be unpredictable.
Especially as a beginner, never turn to programming unless you have time to work carefully. Trying to write a "quick script" to solve a problem when a deadline looms can result in a very stressful situation. If you are in a rush, you might want to use conventional methods, even if they are monotonous and repetitive. You might think that writing code is mysterious or difficult, but the basic principles use every-day reasoning and are quite accessible.
Microsoft Office applications are created in such a way that they expose things called objects that can receive instructions, in much the same way that a phone is designed with buttons that you use to interact with the phone. When you press a button, the phone recognizes the instruction and includes the corresponding number in the sequence that you are dialing.
In programming, you interact with the application by sending instructions to various objects in the application. These objects are expansive, but they have their limits. They can only do what they are designed to do, and they will only do what you instruct them to do. For example, consider the user who opens a document in Word, makes a few changes, saves the document, and then closes it.
Developers organize programming objects in a hierarchy, and that hierarchy is called the object model of the application.
Word, for example, has a top-level Application object that contains a Document object. The Document object contains Paragraph objects and so on. Object models roughly mirror what you see in the user interface. They are a conceptual map of the application and its capabilities. The definition of an object is called a class, so you might see these two terms used interchangeably.
Technically, a class is the description or template that is used to create, or instantiate, an object. Once an object exists, you can manipulate it by setting its properties and calling its methods. If you think of the object as a noun, the properties are the adjectives that describe the noun and the methods are the verbs that animate the noun.
Changing a property changes some quality of appearance or behavior of the object. Calling one of the object methods causes the object to perform some action. The VBA code in this article runs against an open Office application where many of the objects that the code manipulates are already up and running; for example, the Application itself, the Worksheet in Excel, the Document in Word, the Presentation in PowerPoint, the Explorer and Folder objects in Outlook. Once you know the basic layout of the object model and some key properties of the Application that give access to its current state, you can start to extend and manipulate that Office application with VBA in Office.
In Word, for example, you can change the properties and invoke the methods of the current Word document by using the ActiveDocument property of the Application object. This ActiveDocument property returns a reference to the Document object that is currently active in the Word application.
The following code does exactly what it says; that is, it saves the active document in the application. Read the code from left to right, "In this Application, with the Document referenced by ActiveDocument, invoke the Save method. You instruct a Document object to Save and it does not require any more input from you. If a method requires more information, those details are called parameters. The following code runs the SaveAs method, which requires a new name for the file.
Values listed in parentheses after a method name are the parameters. Here, the new name for the file is a parameter for the SaveAs method.
You use the same syntax to set a property that you use to read a property. The following code executes a method to select cell A1 in Excel and then to set a property to put something in that cell. The first challenge in VBA programming is to get a feeling for the object model of each Office application and to read the object, method, and property syntax. The object models are similar in all Office applications, but each is specific to the kind of documents and objects that it manipulates.
In the first line of the code snippet, there is the Application object, Excel this time, and then the ActiveSheet , which provides access to the active worksheet. After that is a term not as familiar, Range, which means "define a range of cells in this way. In other words, the first line of code defines an object, the Range, and runs a method against it to select it.
The result is automatically stored in another property of the Application called Selection. The second line of code sets the Value property of Selection to the text "Hello World", and that value appears in cell A1.
The simplest VBA code that you write might simply gain access to objects in the Office application that you are working with and set properties. For example, you could get access to the rows in a table in Word and change their formatting in your VBA script.
That sounds simple, but it can be incredibly useful; once you can write that code, you can harness all of the power of programming to make those same changes in several tables or documents, or make them according to some logic or condition. For a computer, making changes is no different from making 10, so there is an economy of scale here with larger documents and problems, and that is where VBA can really shine and save you time.
Now that you know something about how Office applications expose their object models, you are probably eager to try calling object methods, setting object properties, and responding to object events. To do so, you must write your code in a place and in a way that Office can understand; typically, by using the Visual Basic Editor. Although it is installed by default, many users do not know that it is even available until it is enabled on the ribbon.
All Office applications use the ribbon. One tab on the ribbon is the Developer tab, where you access the Visual Basic Editor and other developer tools. For example, if the database is stored on your computer and you are the sole user, and if you are comfortable using VBA code, you might decide to use VBA to perform most of your programming tasks. However, if you intend to share your database with other people by locating it on a file server, you might want to avoid using VBA for security considerations.
You should base your decision to use either macros or VBA code on two concerns: security and the functionality that you want.
Security is an issue because VBA can be used to create code that either compromises the security of your data or can harm files on your computer.
When you use a database that was created by someone other than yourself, you should enable VBA code only if you know the database comes from a trustworthy source.
When you create a database that will be used by other people, you should try to avoid including programming tools that require the user to specifically grant trusted status to the database. General techniques for avoiding the need for users to trust your database come later in this section. To help ensure the security of your database, you should try to use macros when you can and use VBA programming only for operations that cannot be performed by using macro actions.
Furthermore, you should try to use only macro actions that don't require granting trusted status to the database in order to run. Limiting the use of macro actions in this manner lets users to be confident that the database has no programming that could harm the data or other files on their computers. Beginning in the Access release, Access contains many new macro actions that enable you to build more powerful macros than you can build by using earlier versions of Access.
For example, you can now create and use global temporary variables by using macro actions, and you can handle errors more gracefully by using new error-handling macro actions. In earlier versions of Access, these kinds of features are available only by using VBA. In addition, you can embed a macro directly into the event property of an object or control. An embedded macro becomes a part of the object or control and stays with the object or control if it is moved or copied.
Macros provide an easy way to handle many programming tasks, such as opening and closing forms and running reports. You can quickly and easily tie together the database objects forms, reports, and so on that you have created because there is little syntax that you must remember. The arguments for each action are displayed in the Macro Builder. In addition to the increased security and ease of use that macros provide, you must use macros to perform the following tasks:.
Assign an action or set of actions to a key. This requires creating a macro group named AutoKeys. Carry out an action or a series of actions when a database first opens. This requires creating a macro named AutoExec.
For more information about how to build macros, see the section Understand macros. Use built-in functions, or create your own functions Access includes many built-in functions, such as the IPmt function, which calculates an interest payment. You can use these built-in functions to perform calculations without having to create complicated expressions. By using VBA code, you can also create your own functions either to perform calculations that exceed the capability of an expression or to replace complex expressions.
In addition, you can use the functions that you create in expressions to apply a common operation to more than one object. Create or manipulate objects In most cases, you will find that it is easiest to create and modify an object in that object's Design view. In some situations, however, you might want to manipulate the definition of an object in code.
By using VBA, you can manipulate all the objects in a database, in addition to the database itself. Perform system-level actions You can carry out the RunApp action in a macro to run another program such as Microsoft Excel from within Access, but you can't use a macro to do much else outside of Access.
Manipulate records one at a time You can use VBA to step through a set of records, one record at a time, and perform an operation on each record. In contrast, macros work with entire sets of records at one time. If you are adding a command button to a form, the Command Button Wizard can help you get started with programming. The wizard helps you create a command button that performs a specific task. In an Access.
In an. In either case, you can then modify or enhance the macro or VBA code to better suit your needs. In the Navigation Pane, right-click the form to which you want to add the command button, and then click Design View. On the Design tab, click the down arrow to display the Controls gallery, and then ensure that Use Control Wizards is selected.
On the Design tab, in the Controls gallery, click Button. On the first page of the wizard, click each category in the Categories list to see which actions the wizard can program the command button to perform. In the Actions list, select the action that you want, and then click Next. Click either the Text option or the Picture option, depending on whether you want text or a picture to be displayed on the command button.
If you want text to be displayed, you can edit the text in the box next to the Text option. If you want a picture to be displayed, the wizard suggests a picture in the list. If you want to select a different picture, select the Show All Pictures check box to display a list of all the command button pictures that Access provides, or click Browse to select a picture that is stored elsewhere.
Enter a meaningful name for the command button.