1. Trang chủ
  2. » Công Nghệ Thông Tin

access 2007 vba bible phần 9 docx

72 587 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 72
Dung lượng 3,01 MB

Nội dung

FIGURE 15.19 A table filled with names of tables and their fields. Selecting the “Back up back end database” command extracts the name of the back end database from the Connect string of a linked table, and presents an InputBox with a proposed save name, which can be edited as desired. The InputBox is shown in Figure 15.20 FIGURE 15.20 An InputBox with a proposed save name for a back-end database. 557 Customizing the Ribbon with XML in Access Databases and Add-ins 15 21_047026 ch15.qxp 4/2/07 10:06 PM Page 557 If you run this command in a database that has no linked tables, you will instead get the message shown in Figure 15.21: FIGURE 15.21 A message when attempting to back up the back end for a database that has no linked tables. The procedures for the “List Table Fields”, “List Query Fields”, “Select Options”, “Back up current database”, and “Back up back end database” buttons are similar to those in the Extras 2007.accda add-in, except that they use the ByVal control As IRibbonControl argument that is needed to run them from Ribbon buttons; their code is not listed here. Summary Although at first it may appear that the new Ribbon is not customizable, you can in fact customize it, at least by adding new tabs and groups, with controls to run your code. This chapter covered writing XML code to load a custom Ribbon, and VBA code for procedures to run from the custom Ribbon buttons, both in regular Access 2007 databases and in library databases for Access 2007 add-ins. The next chapter describes how to write Shared add-ins working with the Access Ribbon in Visual Basic 2005. 558 Adding More Functionality to Office Part III 21_047026 ch15.qxp 4/2/07 10:06 PM Page 558 I n Chapter 13, I described creating a VB 6 COM add-in to add extra functionality to Access. VB 6 (though still supported by Microsoft) is not the latest version of Visual Basic; if you want to use the latest version, that is VB 2005, included in Visual Studio 2005, available in several editions. There are significant differences between these versions of VB and some com- patibility problems with Office 2007, but you can create Visual Studio 2005 add-ins that work with Access, though at present the job is much harder than it should be, and their functionality is limited, because the Visual Studio Tools for Office add-in does not yet include an Access add-in template. This chapter describes creating a simple Visual Studio Shared add-in for Access that will run in both Windows XP and Windows Vista. Preparing to Write a Visual Studio Add-in Before you start writing a Visual Studio add-in, there are several preliminary steps you need to take. The first is to check that.NET support is enabled for Office, to support the Access interoperability component needed to work with Access. Adding .NET Support to Office Since your installation of Office 2007 may not have .NET support enabled, you need to check that this feature has been selected; it is required in order to create Shared add-ins. To check whether.NET support is enabled, you 559 IN THIS CHAPTER Customizing the Ribbon with a Visual Studio 2005 Shared add-in A comparison of Access and Visual Studio add-ins Customizing the Access Ribbon with a Visual Studio 2005 Shared Add-in 22_047026 ch16.qxp 4/2/07 10:07 PM Page 559 need to run Office install. In Windows Vista, first select Programs in the Control Panel, then Programs and Features, then select the Microsoft Office item, as shown in Figure 16.1. If you are running Windows XP, start by selecting the Add or Remove Programs applet in the Control Panel, then the Microsoft Office 2007 item. FIGURE 16.1 Changing the Office 2007 installation in Windows Vista. On the next screen, shown in Figure 16.2, select the Change option, then “Add or Remove Features” for Vista, or the “Add or Remove Features” option for Windows XP. In the Installation Options dialog, drop down the Microsoft Office Access list; if the .NET Programmability Support item has a big red X, that means that it is not installed. To install it, drop down its list and select the “Run from My Computer” item (see Figure 16.3). 560 Adding More Functionality to Office Part III 22_047026 ch16.qxp 4/2/07 10:07 PM Page 560 FIGURE 16.2 Selecting the “Add or Remove Features” option for changing Office in Windows XP. FIGURE 16.3 Selecting the “Run from My Computer” option for .NET programmability support. 561 Customizing the Access Ribbon with a Visual Studio 2005 Shared Add-in 16 22_047026 ch16.qxp 4/2/07 10:07 PM Page 561 If you want to create add-ins for any other Office components, select “Run from My Computer” for their .NET Programmability Support components as well. After clicking Continue, Office installs the new features, and when it is done, you will get a success screen, depicted in Figure 16.4; click Close on this screen, and then close the Add/Remove Program applet. FIGURE 16.4 The success screen after changing the Office configuration. Although VB 2005 Express has an Upgrade Wizard, it isn’t helpful in upgrading a VB 6 COM add- in to VB 2005 because the Express edition of VB 2005 doesn’t support creating add-ins. If you attempt to upgrade a VB 6 COM add-in with this wizard (or the similar wizard in Visual Studio 2005), all the project components will be upgraded except the critical Access Designer, and you will get a message “Activex Designer AccessDesigner.Dsr was not upgraded” in the Upgrade Report, as shown in Figure 16.5. 562 Adding More Functionality to Office Part III 22_047026 ch16.qxp 4/2/07 10:07 PM Page 562 FIGURE 16.5 The Upgrade Report for a VB 6 COM add-in, showing that the Access Designer was not upgraded. The VB 6 COM add-in was the topic of Chapter 13. You might think that Visual Studio Tools for Office 2005 (VSTO) would be an appropriate tool for creating Access add-ins, when supplemented with the downloadable upgrade that supports Office 2007 — after all, Access is part of Office. But this is not so. Though you can create add-ins for all the other major Office 2007 components, and some minor ones as well (see Figure 16.6), you can’t create an Access add-in with VSTO, and therefore in this section I use Visual Studio 2005 for creat- ing a Shared add-in (this is the new name for what was previously called a COM add-in). To create a Shared add-in that adds capability to Access, start by running Visual Studio 2005 and selecting File ➪ New Project. In the New Project dialog, select the Extensibility selection under the Other Project Types category, then select the Shared Add-in template. Enter the add-in’s name and solution name; you can either accept the default location for the add-in’s files or browse for a cus- tom location, as I did in Figure 16.7. CROSS-REF CROSS-REF 563 Customizing the Access Ribbon with a Visual Studio 2005 Shared Add-in 16 22_047026 ch16.qxp 4/2/07 10:07 PM Page 563 FIGURE 16.6 The VSTO New Project Office screen, showing that Access add-in creation is not supported. FIGURE 16.7 Creating a Shared add-in in Visual Studio 2005. The add-in’s name can’t contain spaces or punctuation marks— just letters and numbers. However, the solution name can contain spaces or punctuation marks. After clicking OK, you will get a “Welcome to the Add-in Wizard” screen, as seen in Figure 16.8. CAUTION CAUTION 564 Adding More Functionality to Office Part III 22_047026 ch16.qxp 4/2/07 10:07 PM Page 564 565 Customizing the Access Ribbon with a Visual Studio 2005 Shared Add-in 16 Running Visual Studio 2005 in Windows Vista I n order to run Visual Studio 2005 on Windows Vista, you need to install a Service Pack and then a hot- fix. The first download to install is the Visual Studio 2005 Service Pack 1, which can be downloaded from http://msdn2.microsoft.com/en-us/vstudio/bb265237.aspx (in various versions depending on the edition of Visual Studio). The hotfix is called the Visual Studio 2005 Service Pack 1 Update for Windows Vista Beta; it can be downloaded at http://www.microsoft.com/down- loads/details .aspx?familyid=fb6bb56a-10b7-4c05-b81c-5863284503cf&displaylang=en . After installing the service pack and hotfix, when you run Visual Studio 2005, you will probably get this message: continued FIGURE 16.8 The first screen of the Shared Add-in Wizard. 22_047026 ch16.qxp 4/2/07 10:07 PM Page 565 566 Adding More Functionality to Office Part III continued If you click on the link, you will get a Web page with more links to pages with information about running Visual Studio 2005 on Vista: For running Visual Studio 2005 on Vista, the link you need is the third one, “Running with elevated administrator permissions.” There is a lot of information on this page, but basically you only need one thing: when running Visual Studio in Vista, right-click its icon and select “Run as administrator” from its context menu: 22_047026 ch16.qxp 4/2/07 10:07 PM Page 566 [...]... Microsoft.Office.Interop .Access. TextBox Microsoft.Office.Interop .Access. Label Microsoft.Office.Interop .Access. ComboBox Microsoft.Office.Interop .Access. ListBox Microsoft.Office.Interop .Access. CheckBox Create a new report rpt = appAccess.CreateReport() rpt.RecordSource = “tblTest” txt = appAccess.CreateReportControl(ReportName:=rpt.Name, _ ControlType:=Microsoft.Office.Interop .Access. AcControlType.acTextBox,... Microsoft.Office.Interop .Access. Label Microsoft.Office.Interop .Access. ComboBox Microsoft.Office.Interop .Access. ListBox Microsoft.Office.Interop .Access. CheckBox Create a new form frm = appAccess.CreateForm() frm.RecordSource = “tblTest” txt = appAccess.CreateControl(FormName:=frm.Name, _ ControlType:=Microsoft.Office.Interop .Access. AcControlType.acTextBox, _ Section:=Microsoft.Office.Interop .Access. AcSection.acDetail, _ Left:=0,... Height:=400) lst = appAccess.CreateControl(FormName:=frm.Name, _ ControlType:=Microsoft.Office.Interop .Access. AcControlType.acListBox, _ Section:=Microsoft.Office.Interop .Access. AcSection.acDetail, _ Left:=0, Top:=3000, Width:=2500, Height:=400) 5 79 16 Part III Adding More Functionality to Office chk = appAccess.CreateControl(FormName:=frm.Name, _ ControlType:=Microsoft.Office.Interop .Access. AcControlType.acCheckBox,... Height:=400) lbl = appAccess.CreateControl(FormName:=frm.Name, _ ControlType:=Microsoft.Office.Interop .Access. AcControlType.acLabel, _ Section:=Microsoft.Office.Interop .Access. AcSection.acDetail, _ Left:=0, Top:=1000, Width:=2500, Height:=400) cbo = appAccess.CreateControl(FormName:=frm.Name, _ ControlType:=Microsoft.Office.Interop .Access. AcControlType.acComboBox, _ Section:=Microsoft.Office.Interop .Access. AcSection.acDetail,... support creating Access add-ins, so this step requires extensive manual modification of the Connect class module, to support working with Access and the Ribbon 573 16 Part III Adding More Functionality to Office In the OnConnection method, modify the rows that set the application (Access) and add-in variables as follows: appAccess = DirectCast(application, _ Microsoft.Office.Interop .Access. Application)... ControlType:=Microsoft.Office.Interop .Access. AcControlType.acTextBox, _ Section:=Microsoft.Office.Interop .Access. AcSection.acDetail, _ Left:=0, Top:=0, Width:=2500, Height:=400) lbl = appAccess.CreateReportControl(ReportName:=rpt.Name, _ ControlType:=Microsoft.Office.Interop .Access. AcControlType.acLabel, _ Section:=Microsoft.Office.Interop .Access. AcSection.acDetail, _ Left:=0, Top:=1000, Width:=2500, Height:=400) cbo = appAccess.CreateReportControl(ReportName:=rpt.Name,... appAccess.CreateReportControl(ReportName:=rpt.Name, _ ControlType:=Microsoft.Office.Interop .Access. AcControlType.acComboBox, _ Section:=Microsoft.Office.Interop .Access. AcSection.acDetail, _ Left:=0, Top:=2000, Width:=2500, Height:=400) lst = appAccess.CreateReportControl(ReportName:=rpt.Name, _ ControlType:=Microsoft.Office.Interop .Access. AcControlType.acListBox, _ Section:=Microsoft.Office.Interop .Access. AcSection.acDetail, _ Left:=0, Top:=3000,... much more verbose in Visual Studio 2005 than in Access VBA or VB 6: Instead of (for example) acControlType, you need the full enum reference, Microsoft.Office.Interop .Access. acControlType NOTE If you turn on the Error List pane (View ➪ Error List), you will see a number of warnings about implicit conversion of a variable from Access control to a specific Access control type (see Figure 16.23) In general,... breakpoints in your code (using the F9 function key), as with Access VBA, and when you select Start Debugging on the Debug menu, Access will open automatically Select (or create) a new database, and then click the add-in’s buttons to run and debug the code; it will stop at the breakpoints you have set so you can step through the code with the F8 function key 582 Customizing the Access Ribbon with a Visual Studio... Installation Complete screen, depicted in Figure 16. 29 FIGURE 16. 29 The final screen of the Setup Wizard Generally speaking, after clicking Close on the last wizard screen, the add-in is installed automatically; however, if you don’t see the custom Ribbon that the add-in should create in an Access 2007 database, you can install it manually Open the Access Options dialog from the Office menu, and select . to load a custom Ribbon, and VBA code for procedures to run from the custom Ribbon buttons, both in regular Access 2007 databases and in library databases for Access 2007 add-ins. The next chapter. enabled for Office, to support the Access interoperability component needed to work with Access. Adding .NET Support to Office Since your installation of Office 2007 may not have .NET support enabled, you. support is enabled, you 5 59 IN THIS CHAPTER Customizing the Ribbon with a Visual Studio 2005 Shared add-in A comparison of Access and Visual Studio add-ins Customizing the Access Ribbon with a Visual

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

TỪ KHÓA LIÊN QUAN