To reuse code, you need a place to store it, a way to find it, and a mechanism for retrieving it. The Code Librarian that ships with ODE lets you do all of this and more. To start the Code Librarian, you must perform two steps. (These steps are similar for the other ODE built-in add-ins.) The first time in a session that you want to invoke the Code Librarian, choose Add-Ins from the VBE Project window, and then double-click Code Librarian in the Add-In Manager dialog box. (See Figure 14-1) This places the word Loaded in the Load Behavior column and selects the Loaded/Unloaded check box in the Load Behavior group. Double-click any other add-ins that you want to use during the session. You can select the Load On Startup check box to cause the add-in to start automatically when the Project window opens. After loading the add-in, you can start it from the Add-Ins menu.
Figure 14-1. The Add-In Manager dialog box.
Figure 14-2 shows the Code Librarian window, which organizes code samples by category. The categories are expandable. You can associate a description with a code segment to help you identify it. On the Keywords tab, you can retrieve a list of code samples associated with a keyword. You can use the Search tab to retrieve samples that contain a keyword associated with the sample or that contain a keyword in the full text of the code.
The Code Librarian toolbar offers typical management features. For example, you can see the code behind a selected item by clicking the View Code button. (See Figure 14-2.) The Insert Code To Module button to the right adds the code to your code window. It does this at the cursor's current location in the window, so be sure that the cursor is not in the middle of another procedure (unless that is where you want to insert a snippet). You can also just drag and drop a code sample from the librarian to your code window. The Copy Code To Clipboard button lets you copy code to the Windows Clipboard. This is convenient when you want to copy code to another session that does not appear on the screen. Simply toggle to the session and then paste the contents of the Clipboard into the code window.
Figure 14-2. The Code Librarian organizes its code samples by category, but you can also search for them by keyword or on a full-text basis.
You can also modify existing code or add new code to the Code Librarian. To modify code, open it for viewing, make your changes, and then click Apply. Close the Code Librarian window to commit your changes. To add new code, click the Add New Code toolbar button to open a blank Code Librarian dialog box. Paste a code sample from the Clipboard into the dialog box, and assign a name to the sample in the Name text box. The Name text box is just above where you pasted the code. Use the Description tab to assign a type, such as code snippet, function, module, or class module. Enter a description for the sample in the text box below the Sample Type drop-down list box. You can also assign a category and one or more keywords to the sample. Click OK to save the sample.
Figure 14-3 shows a series of Jet provider ADO samples. As you can see, there are two other collections of ADO samples. These and many other samples ship with the Code Librarian as a starter kit. Some of these might be appropriate for your custom applications with little or no refinement. One of the samples in the figure computes the file size of the current file. The second sample computes a person's age, given the person's birthdate and the current date.
Figure 14-3. These samples ship with the Code Librarian.