Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 72 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
72
Dung lượng
2,65 MB
Nội dung
FIGURE 9.2 Selecting a custom folder for storing documents in a database. FIGURE 9.3 Setting a reference to the Office 12.0 object library. 269 Working with Files and Folders 9 14_047026 ch09.qxp 4/2/07 9:50 PM Page 269 The FileDialog object has several useful methods and properties: n Filters.Add — Lets you specify the filter(s) for displaying files, such as fd.Filters.Add “Word documents”, “*.doc” n InitialFileName — The default file name, for a File Picker dialog; the default path for a Folder Picker dialog n ButtonName — The button caption (in case you want something other than “Select”). n DialogType — A selection of File Picker, Folder Picker, Open, or Save As dialog type (from the MsoFileDialogType enum, which can be viewed in the Object Browser). n InitialView — Lets you set the view for the dialog (Details, Large Icons, Preview, and so on). Look at the MsoFileDialogView enum in the Object Browser for the full selection. n AllowMultiSelect — If set to True, lets users select multiple files in the dialog (for File Picker dialogs only). Figure 9.4 shows the MsoFileDialogView enum, with all the options for setting the view for the dialog. FIGURE 9.4 Viewing the MsoFileDialogView enum in the Object Browser. 270 Writing VBA Code to Exchange Data between Office Components Part II 14_047026 ch09.qxp 4/2/07 9:50 PM Page 270 The FileSystemObject The FileSystemObject library provides another way to work with Windows folders (and files). To use the components of the object library in a database, you need to set a reference, in this case to the Scripting Runtime library, as shown in Figure 9.5. If you don’t see the Microsoft Scripting Runtime selection in the References dialog, you can get this library by downloading Microsoft Windows Script 5.6 (or whatever is the current version), plus the Microsoft Windows Script 5.6 Documentation files from the Microsoft Windows Scripting Downloads page at http://www.microsoft.com/downloads/details.aspx? familyid=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en . FIGURE 9.5 Setting a reference to the Scripting Runtime library. The Help file shown in Figure 9.6 (script56.chm) is a compiled HTML Help file. You’ll find it very helpful, because it includes a Help book with full information on the components of the FileSystemObject, plus useful code samples. 271 Working with Files and Folders 9 14_047026 ch09.qxp 4/2/07 9:50 PM Page 271 272 Writing VBA Code to Exchange Data between Office Components Part II A Digression on Access Help I n my opinion, Access Help reached its highest point in Access 3.1, when it was provided in the form of a Windows Help file, every bit of which was written specifically for the current version of Access, and was available without an Internet connection. In successive versions of Access, Help moved to the HTML format, which introduced the possibility of retrieving Help topics that were not relevant to Access (say from the MSDN Library that came with VB 6.0), because properties, methods, controls, and other objects may occur in many different Microsoft applications, although they may not (usually don’t) work exactly the same. If you are trying to determine which properties of a Tab control will work on an Access form, it isn’t much help if the Help topic you find is for a Tab control on an Office UserForm or a VB form. Access 2007 Help is even less useful; it searches all of Office online (at least, if you are connected to the Internet; otherwise, you won’t get any help at all), using a shamefully ineffective search engine. I entered “Tab control” into the search box in the Access Help window and got the list of topics shown in the following figure. Not a single one of them is relevant. One might think that Access doesn’t support Tab controls, but of course that is not the case. FIGURE 9.6 The FileSystemObject Help file. 14_047026 ch09.qxp 4/2/07 9:50 PM Page 272 273 Working with Files and Folders 9 A list of inappropriate Help topics on searching for “Tab control.” There is a Table of Contents option in Access Help, but it is very sparse compared to the table of con- tents for Help in previous Access versions, and (curiously) it is quite different depending on whether or not you are connected to the Internet. Rather than giving full coverage of all the controls you can put on an Access form, the Controls heading (in the online version of the Table of Contents, and as shown in the following figure) has only four topics, covering only the most commonly used controls. continued 14_047026 ch09.qxp 4/2/07 9:50 PM Page 273 274 Writing VBA Code to Exchange Data between Office Components Part II continued The Controls book in the Access Help Table of Contents. The offline version of the Table of Contents has a VBA Reference section, with a section on controls, and it does point to a Help topic for the Tab control — but the topic is not available offline! And in any case, Search won’t find it. Does that mean there is no help available for the Tab control? Not at all. I entered “Tab control on Access forms” into Google and got a page of relevant topics, starting with a very useful tutorial on using the Access Tab control. It isn’t specific to Access 2007, but that doesn’t matter because Tab controls weren’t changed in this version. Note that some of the hits are from the Microsoft web site, so Access Help can’t even find appropriate help topics in Microsoft’s own Support files! My conclu- sion: if you need help for Access 2007, try Google. 14_047026 ch09.qxp 4/2/07 9:50 PM Page 274 275 Working with Files and Folders 9 Using Google to get help for Access. There is another option for help, at least when you are writing VBA code: As shown in the following figure, you can use the Object Browser (opened by the F2 key in the Visual Basic window) to view components of the Access object model and their attributes. In previous versions of Access, clicking the yellow question mark button would usually open an appropriate Help topic. However, this is no longer the case in Access 2007. Selecting the TabControl object in the Access library and clicking the Help button just opens the main Help window, where you can search ineffectually for help on the Tab control, just as I described previously. continued 14_047026 ch09.qxp 4/2/07 9:50 PM Page 275 276 Writing VBA Code to Exchange Data between Office Components Part II continued Selecting an Access object in the Object Browser. In Access 2003, clicking the Help button for the Tab control in the Object Browser opened an appro- priate Help topic. 14_047026 ch09.qxp 4/2/07 9:50 PM Page 276 Backing up Your Database Everybody knows that data should be backed up frequently, and I like to make it as convenient as possible to back up a database. My standard database main menu features a Backup button, which calls the BackupDB procedure listed next. I created the Backup code and menu button in an ear- lier version of Access, when there was no way of backing up a database without closing it down. Since that time, Microsoft has added a backup command that doesn’t require closing down the database, though it’s still not as convenient as my one-click backup. The Access 2007 backup com- mand is available through the Manage button on the File menu (shown in Figure 9.7). Selecting the “Back Up Database” option opens the Save As dialog shown in Figure 9.8. 277 Working with Files and Folders 9 An Access 2003 Help topic for the Tab control. 14_047026 ch09.qxp 4/2/07 9:50 PM Page 277 FIGURE 9.7 The Access 2007 Back Up Database selection. FIGURE 9.8 The built-in database save dialog. If you are running the backup code in Windows Vista, you may be unable to back up databases in certain folders, because of Vista security restrictions. This is a Vista issue, not a problem with the database, as you can backup the database after moving it to another folder with lower security. NOTE NOTE 278 Writing VBA Code to Exchange Data between Office Components Part II 14_047026 ch09.qxp 4/2/07 9:50 PM Page 278 [...]... variable is written to the txtImportedText textbox, as shown in Figure 9. 15 296 Working with Files and Folders FIGURE 9. 15 A form with data loaded from a text file, using the FileSystemObject method Working with Attachment Fields NEW FEATURE The Attachment data type discussed in this section is new to Access 2007 Previous versions of Access had an OLE Object field data type, which only supported certain... add-in with user-selectable backup options, including selection of the backup folder from a Folder Picker dialog The Access 2007 Backup.accdb database contains the table, module, and macros that are used to do the database backups These database objects can be imported into any Access 2007 database, and the BackupDB function can be run from the mcrBackup macro, or from a button on the main menu The... shows a record with a Word 2007 document attachment The attachment icons differ according to the Office version of the stored attachment file A Word document’s icon has a Word document with a W image over the upper-left corner; the style of the W differs for Word 97-2003 (.doc) or Word 2007 (.docx) documents For a Word 2007 document, the W is similar to the one displayed in a Word 2007 document’s taskbar... attachments is represented in VBA code as a separate recordset of attachments belonging to a record in a table Using a Recordset2 object (new to Access 2007) to work with the attachments lets you use the new LoadFromFile and SaveToFile methods to work with the attachments Figure 9.19 shows a folder with Contact ID documents of various types for loading as attachments 299 9 Part II Writing VBA Code to Exchange... Exit Sub ErrorHandler: Outlook is not running; open Outlook with CreateObject If Err.Number = 429 Then Set appOutlook = CreateObject(“Outlook.Application”) Resume Next ElseIf Err.Number = 55 Then 289 9 Part II Writing VBA Code to Exchange Data between Office Components File is already open; close it Close #1 Resume Else MsgBox “Error No: “ & Err.Number _ & “; Description: “ & Err.Description Resume ErrorHandlerExit...Working with Files and Folders Though it is much easier to back up an Access database in Access 2007 than in previous versions, the new “Back Up Database” selection on the Manage menu defaults to saving the database copy in the same folder as the database itself If you want to save your... strTitle = “Done!” MsgBox strPrompt, vbOKOnly + vbInformation, strTitle 303 9 Part II Writing VBA Code to Exchange Data between Office Components ErrorHandlerExit: Exit Function ErrorHandler: MsgBox “Error No: “ & Err.Number _ & “; Description: “ & Err.Description Resume ErrorHandlerExit End Function In Access 2007, it is much easier to save a database to an older format, as well as saving a copy of the... Data:=strText, _ Options:=adWriteLine GoTo NextContactADO End If Check for company name strCompanyName = Nz(lst.Column(3, varItem)) strTest = strCompanyName Debug.Print “Company name: “ & strTest 2 85 9 Part II Writing VBA Code to Exchange Data between Office Components If strTest = “” Then blnSomeSkipped = True strText = “Contact No “ & lngContactID _ & “ (“ & strFullName _ & “) skipped; no company name” tstr.WriteText... versions of Access had an OLE Object field data type, which only supported certain types of objects and was quite cumbersome to use (not to mention causing terrible database bloat) By contrast, in Access 2007, it is quite easy to store files of any type in a field of the new Attachment data type, and the attachments are automatically compressed to save database space The Contacts form, frmContactsWithAttachments... copy using the BackupDB function Working with Text Files For purposes of working with text files in VBA code, there are three types of text files: commadelimited, fixed-width (columnar), or free-form Data from the first two types of text files can be imported or exported using the TransferText method in Access, and comma-delimited files can be processed with the TransferSpreadsheet method If you just . 4/2/07 9 :50 PM Page 271 272 Writing VBA Code to Exchange Data between Office Components Part II A Digression on Access Help I n my opinion, Access Help reached its highest point in Access 3.1,. previously. continued 14_047026 ch09.qxp 4/2/07 9 :50 PM Page 2 75 276 Writing VBA Code to Exchange Data between Office Components Part II continued Selecting an Access object in the Object Browser. In Access 2003, clicking. security. NOTE NOTE 278 Writing VBA Code to Exchange Data between Office Components Part II 14_047026 ch09.qxp 4/2/07 9 :50 PM Page 278 Though it is much easier to back up an Access database in Access 2007 than in