VBA编程指南:控件与窗体操作

5星 · 超过95%的资源 需积分: 35 48 下载量 159 浏览量 更新于2024-07-17 1 收藏 2.18MB PDF 举报
"VBA编程大全,涵盖了控件的使用、控件引用方法、以及设置控件可见性的示例。" 在VBA编程中,Visual Basic for Applications (VBA) 是一种强大的工具,用于自动化Microsoft Office应用程序,如Access、Excel、Word等。本资源主要讨论了在Access中使用VBA进行编程的一些核心概念。 首先,控件是用户界面中的元素,允许用户与数据库交互。资源列举了一系列Access中的控件类型,包括: 1. acBoundObjectFrame - 绑定对象框,用于显示数据库中的对象,如表或查询的结果。 2. acCheckBox - 复选框,供用户选择或取消选择一个选项。 3. acComboBox - 组合框,结合了文本框和列表框的功能,用户可以输入文本或从下拉列表中选择。 4. acCommandButton - 命令按钮,执行指定的宏或过程。 5. acCustomControl - ActiveX自定义控件,可扩展功能的控件。 6. acImage - 图像控件,用于显示图片。 7. acLabel - 标签,用于显示文本但不可编辑。 8. acLine - 线条,用于美化或分割界面。 9. acListBox - 列表框,显示一组可选择的项目。 10. acObjectFrame - 未绑定对象框或图表,可插入非数据相关的对象。 11. acOptionButton - 选项按钮,单选按钮,通常用于提供一组互斥的选项。 12. acOptionGroup - 选项组,包含多个选项按钮的容器。 13. acPage - 页,多页控件的一部分,用于组织内容。 14. acPageBreak - 分页符,用于在打印时创建新页面。 15. acRectangle - 矩形,用于设计目的。 16. acSubform - 子窗体/子报表,用于嵌入其他窗体或报表。 17. acTabCtl - 选项卡控件,用于创建多面板视图。 18. acTextBox - 文本框,用于输入或显示文本。 19. acToggleButton - 切换按钮,类似开关,可处于两种状态之一。 引用控件是VBA编程中的关键部分。可以通过窗体或报表的名称,加上"!"运算符和控件名称来访问它们。例如,`Forms![订单]![订单ID]` 引用"订单"窗体上的"订单ID"控件。对于子窗体或子报表,可以直接使用子窗体或子报表的名称,如 `Forms![订单]![订单子窗体]![数量]`。 在VBA中,可以利用循环来判断和操作窗体或报表上的控件。例如,`Forms![Employees].Count` 返回窗体"Employees"上的控件数量,而 `Forms![Orders].Controls.Count` 返回特定窗体上控件的总数。通过索引访问这些控件并设置其可见性,例如: ```vba For i = 3 To 10 Me.Controls.Item(i).Visible = True Next For i = 11 To 22 Me.Controls.Item(i).Visible = False Next ``` 这段代码将从索引3到10的控件设为可见,而从11到22的控件设为不可见。 此外,还可以根据控件的名称进行筛选和设置可见性,例如: ```vba For i = 27 To 47 If Me.Controls.Item(i).Name Like "A*" Then Me.Controls.Item(i).Visible = False End If Next ``` 此代码会隐藏所有名称以"A"开头的控件。 通过这样的方式,VBA编程允许开发者灵活地控制Access应用的界面和行为,实现自定义功能,提升用户体验。
2012-09-14 上传
OfficeTips Home || VBA Section || General Section || Download Section || Privacy Policy Useful PowerPoint VBA code snippets More Sharing Services Share | Share on gmail Share on google Share on facebook Share on twitter Determine the current slide in the Slide View mode: Sub SlideIDX() MsgBox "The slide index of the current slide is:" & _ ActiveWindow.View.Slide.SlideIndex End Sub Determine the current slide in Slide Show mode: Sub SlideIDX() MsgBox "The slide index of the current slide is:" & _ ActivePresentation.SlideShowWindow.View.Slide.SlideIndex End Sub Difference between SlideIndex property and SlideNumber property: The SlideIndex property returns the actual position of the slide within the presentation. The SlideNumber property returns the PageNumber which will appear on that slide. This property value is dependent on "Number Slide from" option in the Page Setup. Go to Page Setup and Change the value of "Number Slide from" to 2 and then while on the 1st slide in Slide View run the following Macro Sub Difference() MsgBox "The Slide Number of the current slide is:" & _ ActiveWindow.View.Slide.SlideNumber & _ " while the Slide Index is :" & _ ActiveWindow.View.Slide.SlideIndex End Sub Macro to exit all running slide shows: Sub ExitAllShows() Do While SlideShowWindows.Count > 0 SlideShowWindows(1).View.Exit Loop End Sub Code to refresh current slide during the slide show: Sub RefreshSlide() Dim lSlideIndex As Long lSlideIndex = SlideShowWindows(1).View.CurrentShowPosition SlideShowWindows(1).View.GotoSlide lSlideIndex End Sub Code to reset animation build for the current slide during the slide show: Sub ResetSlideBuilds() Dim lSlideIndex As Long lSlideIndex = SlideShowWindows(1).View.CurrentShowPosition SlideShowWindows(1).View.GotoSlide lSlideIndex, True End Sub Insert a slide after current slide Sub InsertSlide() Dim oView As View With ActivePresentation.Slides Set oView = ActiveWindow.View oView.GotoSlide .Add(oView.Slide.SlideIndex + 1, _ ppLayoutTitleOnly).SlideIndex Set oView = Nothing End With End Sub Copyright 1999-2011 (c) Shyam Pillai. All rights reserved.