17.3. Deeper into ObjectsThere comes a point in every Access programmer's life when you realize you've learned enough about the VB language to get by. From that point on, you spend most of your time learning about different objects , which is a much larger task. Access has several dozen built-in objects which, taken together, make up what programmers call an object model . Along with the control and form objects you know so well, it has objects representing queries, projects, reports , smart tags, printers, and much more. You can't cover all these objects in a single chapter. Even if you could, you'd find that many of them just don't interest you. However, you need to know enough so that you can hunt down the features you need when you're tackling a particularly sticky VB challenge. You can explore the Access object model in several ways:
Even if you work your way through the sprawling Access object model, there are still many more objects out there. If you're a black belt VB programmer, then you may choose to create your own objects. And even if you aren't, you may decide to use another component that gives you even more objects to play with. Note: In programmer-speak, a component's just a file that has some objects you can use in your code. The file acedao.dll has the objects you can use to interact directly with your database (see Section 17.4.5). Later in this chapter, you'll learn how to use DAO (the data access objects ) to interact with your database. DAO's such a common part of Access programming that most people treat it as a built-in part of the Access object model. But technically, DAO consists of a set of objects provided by a separate component, which Access provides. Many more components are waiting for you to discover them. To use a new component, you need to add a reference to that component in your database. To do so, in the Visual Basic editor's menu, choose Tools References. Youll see the References dialog box shown in Figure 17-7.
The References dialog box's problem is that you need to know exactly what component you want to use. The Available References list's full of neat-sounding components that aren't designed for use in Access, and won't work right with your code. Among the components you can use are ones from Microsoft that let you interact with files, show Web pages, and interact with other Office applications. However, you won't get far experimenting on your own. Instead, you'll need to find sample code online or in the Access Help.
17.3.1. The DoCmd ObjectThe DoCmd object is the single most useful object in the Access programming world. It provides one-stop shopping for a variety of tasks , like opening forms and reports, launching other programs, finding records, and running macros. Unlike the objects you've seen so far, the DoCmd object doesn't have any properties. Instead, it's made up of methods that perform different actions. If you want to open a form named ProductCatalog, you can use the OpenForm method like this: DoCmd.OpenForm "ProductCatalog" Like most of the DoCmd methods, OpenForm can use several optional parameters. Visual Basic prompts you by showing the list of possible parameters as you type. Here's an example that skips over the second and third parameters (note the commas with no values in between) but supplies a filter in the fourth parameter, and a data mode in the fifth parameter. DoCmd.OpenForm "ProductCatalog", , ," ID=5", acFormReadOnly This command opens the ProductCatalog form, applies a filter to show only the record with the ID of 5, and uses read-only mode to prevent any changes. Note: This example uses a acFormReadOnly, which is a constant . Constants are numeric values that are given more helpful names . So instead of remembering that the number represents read-only mode, you can use the more readable acFormReadOnly constant. Any time you see a variable that starts with ac or vb and you haven't created it yourself, the odds are that it's a constant. Of course, you still need to know the constant names to use them, but IntelliSense can help you out, as shown in Figure 17-8.
If the OpenForm method looks familiar, that's because you've already seen the same functions with the OpenForm macro action (Section 15.3.1). In fact, all the methods of the DoCmd object line up with the macro actions you learned about in Chapter 15. Table 17-1 lists the most useful ones. Table 17-1. Useful Methods of the DoCmd Object
17.3.2. Converting a Macro to VB CodeIf you want to learn a little more about Visual Basic and the DoCmd object, then you can take an existing macro and convert it into a pure code subroutine. Here's how:
The following example shows the result of converting a macro from Chapter 15 (shown in Section 15.3.1) which searches for specific text in the AnimalTypes table: Function FindHayEater( ) On Error GoTo FindHayEater_Err DoCmd.OpenForm "AnimalTypes", acNormal, "", "", , acNormal DoCmd.GoToControl "Diet" DoCmd.FindRecord "=""hay""", acAnywhere, False, , _ False, acCurrent, False FindHayEater_Exit: Exit Function FindHayEater_Err: MsgBox Error$ Resume FindHayEater_Exit End Function You'll notice that the converted code makes heavy use of the DoCmd objectin fact, almost every line uses the DoCmd object. First, it uses the OpenForm method to open a form, then it uses the GoToControl method to switch to the Diet field, and finally it looks for the first record that has the text "hay." This line looks a little weird because it doubles up its quotations marks (""). Quotations marks have a special meaning to Visual Basic (they show where text begins and ends). If you actually want to insert a quotation mark in your text, you need to put two quotation mark characters in a row. Strange, but true. The code ends with an error-handling routine named FindHayEater_Err, which simply shows the problem in a Message box, and then ends the routine. Note: When you convert a macro into code, Access always generates a function (Section 17.1.4), not a sub-routine. However, the function doesn't return a result, so it's not really necessary. (Presumably, Access works this way to give you the flexibility to decide later on that you want to return a value.) |