我发现这个用户定义的函数列出了访问数据库中的所有表:http://www.consultdmw.com/access-VBA-list-objects.htm
用户定义的功能如下:
Function dmwListAllTables() As String
Dim tbl As AccessObject, db As Object
Dim strMsg As String
On Error GoTo Error_Handler
Set db = Application.CurrentData
For Each tbl In db.AllTables
Debug.Print tbl.Name
Next tbl
strMsg = " -- Tables listing complete -- "
Procedure_Done:
dmwListAllTables = strMsg
Exit Function
Error_Handler:
strMsg = Err.Number & " " & Err.Description
Resume Procedure_Done
End Function
此函数最初设计用于将当前数据库中的所有表名打印到即时窗口,但是我正在寻找一种方法将dmwListAllTables()
输出合并为带有组合框的表单中使用的值。这个壮举可能吗?
提前致谢,
dubbbdan
答案 0 :(得分:2)
我确信可以修改代码以执行您想要的操作。只是一个问题,你希望在什么时候发生这种情况。当表单加载或单击按钮时,ComboBox是否需要填充表的名称?所有你需要做的就是,(更简单的例子,我将在Form Loads时使用),
Private Sub Form_Load()
Me.comboBoxName.RowSourceType = "Value List"
Me.comboBoxName.RowSource = dmwListAllTables()
End Sub
然后将您的功能修改为,
Function dmwListAllTables() As String
Dim tbl As AccessObject, db As Object
Dim strMsg As String
On Error GoTo Error_Handler
Set db = Application.CurrentData
For Each tbl In db.AllTables
strMsg = strMsg & tbl.Name & ";"
Next tbl
Procedure_Done:
dmwListAllTables = strMsg
Exit Function
Error_Handler:
strMsg = Err.Number & " " & Err.Description
Resume Procedure_Done
End Function