access 2007 vba bible phần 7 docx

72 236 0
access 2007 vba bible phần 7 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

& “order shipped on “ _ & Format(Date, “dd-mmm-yyyy”) MsgBox strPrompt, vbInformation, strTitle ErrorHandlerExit: Figure 12.6 shows the “Finished!” message after all the sets of labels have been created. FIGURE 12.6 The success message after all labels have been printed. Clear the progress meter: Application.SysCmd acSysCmdClearStatus Exit Sub ErrorHandler: If Err = 429 Then Word is not running; open Word with CreateObject: Set appWord = CreateObject(“Word.Application”) Resume Next Else MsgBox “Error No: “ & Err.Number & “; Description: “ _ & Err.Description Resume ErrorHandlerExit End If End Sub The GetDocumentsPath function that retrieves the Documents path from tblInfo is listed next; the GetTemplatesPath function is similar: Public Function GetDocumentsPath() As String On Error GoTo ErrorHandler Set dbs = CurrentDb Set rst = dbs.OpenRecordset(“tblInfo”) 413 Going Beyond the Basics 12 17_047026 ch12.qxp 4/2/07 9:53 PM Page 413 rst.MoveFirst GetDocumentsPath = rst![DocumentsPath] & “\” rst.Close ErrorHandlerExit: Exit Function ErrorHandler: MsgBox “Error No: “ & Err.Number & “; Description: “ & _ Err.Description Resume ErrorHandlerExit End Function Figure 12.7 shows a page of shipping labels. FIGURE 12.7 A set of Word shipping labels filled with data from Access. 414 Writing VBA Code to Exchange Data between Office Components Part II 17_047026 ch12.qxp 4/2/07 9:53 PM Page 414 Every development project has unique requirements; when using Access to create mailing labels (or any other type of Word documents), you have the freedom to create tables and forms that exactly meet your needs, storing all (and only) the information you need to select and print the data you need on Word documents. The code used to create the shipping labels described in this section includes several techniques that are useful for producing Word documents of various types, such as creating filtered recordsets for working with detail records, calculating “x of y” numbers when working with sets of records, displaying a progress meter in the status bar, and updating tables from code. Creating Excel PivotCharts from Access Queries You can create great interactive charts and tables using Access’s own tools (PivotCharts and PivotTables), as noted in Chapter 1. However, there is a drawback to using Access PivotCharts and PivotTables — they are only interactive while working in Access. If you save a PivotChart or PivotTable as a PDF (if you have installed the Save as PDF utility) or Snapshot file, and send it to someone else, it is just an image, not an interactive chart or table. If you need to put Access data into an interactive chart or table for others to work with (even if they don’t have Access installed), you can use a different approach: Export the Access data to an Excel worksheet, and then create an Excel PivotChart or PivotTable that users can manipulate as they wish. When you create an Excel PivotChart, it is automatically created with a linked PivotTable. The first step in creating an Excel PivotChart is to create an Access query with the data to be charted. Excel PivotCharts are not exactly the same as Access PivotCharts; in particular, they lack the date grouping feature that automatically creates a variety of date sorts from a Date field (Year, Month, Quarter, Week). If you want to analyze data in an Excel PivotChart by month, quarter, or year, you need to do the breakdown in an Access query, before exporting the data to Excel, or cre- ate the date groups manually in Excel by using the Group command. In Office 2007, you can create an Excel PivotChart manually, following these steps: 1. Create an Access query with the data to be charted; qryQuarterlySalesByCategory has only three fields: OrderQuarter, Category, and Price. The OrderQuarter field extracts the year and quarter from the OrderDate field, using this expression: OrderQuarter: Year([OrderDate]) & “ Q” & DatePart(“q”,[OrderDate]) 2. Export this query to Excel using the Excel command in the Export group of the External Data tab of the Ribbon, as shown in Figure 12.8. NOTE NOTE 415 Going Beyond the Basics 12 17_047026 ch12.qxp 4/2/07 9:53 PM Page 415 FIGURE 12.8 Exporting a query to Excel using a Ribbon command. 3. On the Export dialog, edit the worksheet name as desired, and browse for an alternate location if you don’t want to store the worksheet in the default Documents folder (in Figure 12.9, I edited the worksheet name and left the folder at the default setting). FIGURE 12.9 Editing the worksheet save name in the Export dialog. 416 Writing VBA Code to Exchange Data between Office Components Part II 17_047026 ch12.qxp 4/2/07 9:53 PM Page 416 4. Open the newly created worksheet in Excel. 5. Click anywhere in the data range, select the Insert tab of the Ribbon, and select PivotChart from the drop-down in the Tables group, as shown in Figure 12.10. FIGURE 12.10 Creating a PivotChart from data in an Excel worksheet. 6. The Create PivotTable with PivotChart dialog opens, as shown in Figure 12.11, with the range preselected; just click OK to create the PivotChart in another worksheet in the same workbook. 417 Going Beyond the Basics 12 17_047026 ch12.qxp 4/2/07 9:53 PM Page 417 FIGURE 12.11 The Create PivotTable with PivotChart dialog. 7. The new, blank PivotChart appears, as shown in Figure 12.12. FIGURE 12.12 A newly created Excel PivotChart. 418 Writing VBA Code to Exchange Data between Office Components Part II 17_047026 ch12.qxp 4/2/07 9:53 PM Page 418 8. The layout of a PivotChart is different in Excel than in Access (a discrepancy Microsoft should clear up, but that is another matter). The fields from the query are listed in the PivotTable Field List in the panel on the right of the worksheet; I dragged OrderQuarter to the Axis Fields drop zone, CategoryName to the Legend Fields drop zone, and Price to the Values drop zone (Excel automatically makes it a Sum of Price). Figure 12.13 shows the plainly formatted PivotChart at this point. FIGURE 12.13 An Excel PivotChart with fields assigned to drop zones. 9. To format the left axis number, right-click any category value in the PivotChart and select “Format Axis” from the context menu, as shown in Figure 12.14. 419 Going Beyond the Basics 12 17_047026 ch12.qxp 4/2/07 9:53 PM Page 419 FIGURE 12.14 Formatting axis number. 10. The Format Axis dialog opens; I formatted the left axis number for U.S. Currency by selecting Number for the Axis Option, Currency for the Category, 0 decimal places, and $ English (U.S.) for the Symbol, as shown in Figure 12.15. FIGURE 12.15 Formatting the left axis for U.S. Currency. 420 Writing VBA Code to Exchange Data between Office Components Part II 17_047026 ch12.qxp 4/2/07 9:53 PM Page 420 When the PivotChart (or one of its components) is selected, you should see special PivotChart commands in the Design and Layout groups of the Ribbon; if you don’t see them, click the PivotChart to give it the focus. 11. To give the chart a title (generally a good idea), I selected Layout 1 in the Chart Layouts group on the Design tab of the Ribbon (in PivotChart Tools mode), as shown in Figure 12.16. FIGURE 12.16 The PivotChart Tools. 12. This selection adds a Chart Title control to the chart; right-click it and select Edit Text to edit the chart name as desired; I made it “Quarterly Sales by Category.” 13. The final step is to select a chart style. The default style (contrasting color bars) is gener- ally fine, but there are lots more choices available. To select a different chart style, drop down the More button at the lower-right corner of the Chart Styles group, as shown in Figure 12.17. FIGURE 12.17 Opening the palette of chart styles. 14. A palette of 54 styles opens as shown in Figure 12.18. 15. The formatted PivotChart is shown in Figure 12.19. NOTE NOTE 421 Going Beyond the Basics 12 17_047026 ch12.qxp 4/2/07 9:53 PM Page 421 FIGURE 12.18 Selecting a chart style from the palette. FIGURE 12.19 A PivotChart with contrasting color bars and a light background. 422 Writing VBA Code to Exchange Data between Office Components Part II 17_047026 ch12.qxp 4/2/07 9:53 PM Page 422 [...]... CommandBars collection In Access 20 07, COM add-ins place buttons in the Toolbar Commands group of the Add-Ins tab of the Ribbon, for backwards compatibility with the old CommandBars collection In addition to COM add-ins and Access add-ins, Access 20 07 also offers a brand-new option: using the XML programming language to add controls to the Ribbon, powered by code written in VBA This technique is covered... errX.Description MsgBox strMsg, , “Error!” End Sub The AccessDesigner Module The COM add-in needs several entries in the Declarations section of the AccessDesigner module To open the designer module, open the Designers folder in the project tree, right-click the AccessDesigner item, and select View Code from the context menu, as shown in Figure 13 .7 FIGURE 13 .7 Opening the AccessDesigner code module The standard... Report Design) Access toolbars (in older versions of Access) , or in the Toolbar Commands group on the backward compatibility Add-Ins tab of the Ribbon (in Access 20 07 running on Windows XP) n The OnDisconnection event procedure runs a function that removes the two command bar buttons when the add-in is disconnected by unloading the add-in from the COM Add-Ins dialog (they are not removed when Access is... 2003, and there is none for Office 20 07, so that option is no longer viable, unless you still have the Developer Edition of Office 2000 or Office XP installed Visual Studio Tools for Office lets you create Visual Studio add-ins for some Office components, but unfortunately, even the latest edition, the one that supports Office 20 07, still lacks support for creating Access add-ins See Chapter 16 for... supports Office 20 07, still lacks support for creating Access add-ins See Chapter 16 for a discussion of creating Visual Studio add-ins for working with the Access 20 07 Ribbon CROSS-REF However, that doesn’t mean you can’t create COM add-ins for Office 20 07 Visual Basic was last updated in 1998 (v 6.0), but it is still quite useful, and is fully supported by Microsoft, unlike most other Microsoft applications... both Access and Excel, there are some differences: The Polar chart type is only available in Access, and the Surface chart type is only available in Excel Figure 12.24 shows the Access chart types on the left, and the Excel chart types on the right, to make it easier to identify the chart type you want to use 425 12 Part II Writing VBA Code to Exchange Data between Office Components FIGURE 12.24 Access. .. the shipping report Although saving to the PDF format didn’t make it to the release version of Access 20 07, Microsoft has provided a downloadable utility that adds PDF support to Access This utility can be downloaded from http://www.microsoft.com/downloads/details.aspx?familyid= F1FC413C-6D89-4F15-991B-63B07BA5F2E5&displaylang=en (or search the Microsoft Downloads page for “Save to PDF”) Once you have... expand the functionality of Access databases, using VBA code to examine data and make decisions about what data should be exported, and to format the Office documents filled with Access data 432 Adding More Functionality to Office IN THIS PART Chapter 13 Creating COM Add-ins with Visual Basic 6 Chapter 14 Creating Access Add-ins Chapter 15 Customizing the Ribbon with XML in Access Databases and Add-ins... to host application (Access) Public pappAccess As Access. Application Regular variables for creating toolbar buttons Private cbrMenu As Office.CommandBar Private cbbAddIn As Office.CommandBarButton Public variables for handling renaming Public Public Public Public Public Public Public Public Public Public Public Public Public pctl As Access. Control pdbs As DAO.Database pfrm As Access. Form pintRenameFail... Databases and Add-ins Chapter 16 Customizing the Access Ribbon with a Visual Studio 2005 Shared Add-in Chapter 17 Creating Standalone Scripts with Windows Script Host Chapter 18 Working with SQL Server Data Creating COM Add-ins with Visual Basic 6 O ffice 2000 introduced COM add-ins as a new development tool, an alternative to creating VBA add-ins for Access, Excel, Outlook, and Word A COM add-in is . open, as shown in Figure 12. 27, where you can edit product amounts as needed. 4 27 Going Beyond the Basics 12 17_ 0 470 26 ch12.qxp 4/2/ 07 9:53 PM Page 4 27 FIGURE 12. 27 A form for editing product. worksheet in the same workbook. 4 17 Going Beyond the Basics 12 17_ 0 470 26 ch12.qxp 4/2/ 07 9:53 PM Page 4 17 FIGURE 12.11 The Create PivotTable with PivotChart dialog. 7. The new, blank PivotChart appears,. 12 .7 shows a page of shipping labels. FIGURE 12 .7 A set of Word shipping labels filled with data from Access. 414 Writing VBA Code to Exchange Data between Office Components Part II 17_ 0 470 26

Ngày đăng: 14/08/2014, 06:22

Từ khóa liên quan

Mục lục

  • Access 2007 VBA Bible

    • Part II: Writing VBA Code to Exchange Data between Office Components

      • Chapter 12: Going Beyond the Basics

        • Creating Excel PivotCharts from Access Queries

        • Emailing Shipping and Reordering Reports

        • Summary

        • Part III: Adding More Functionality to Office

          • Chapter 13: Creating COM Add-ins with Visual Basic 6

            • Creating a COM Add-in Using Visual Basic 6.0

            • Using a COM Add-in

            • Comparing COM Add-ins with Access Add-ins

            • Summary

            • Chapter 14: Creating Access Add-ins

              • The Purpose of Access Add-ins

              • Add-in Types

              • Creating a Library Database

              • Things You Need to Know When Writing Add-ins

              • The Extras Add-in Code

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan