Using VB.NET to obtain macros in MS Access

独自空忆成欢 提交于 2021-02-16 20:46:06

问题


I'm trying to create a VB.NET button which will connect to an MS Access database and obtain a list of all the macros stored in the access database.

After I have the list I want to display the values in a combobox. I've found something online similar to what i'm trying to do but I cannot find anything which will just give me a list of macros.

Dim userTables As DataTable = Nothing
Dim connection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection()
Dim source As String
source = TextDBPath.Text
connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + source
    Dim restrictions() As String = New String(3) {}
restrictions(3) = "Table"
connection.Open()
' Get list of user tables
userTables = connection.GetSchema("Tables", restrictions)
connection.Close()
' Add list of table names to listBox
Dim i As Integer
For i = 0 To userTables.Rows.Count - 1 Step i + 1
    cbox.items.add(userTables.Rows(i)(2).ToString())
Next

In the program i'm working on, this was obtained in VB6 by using .Containers("Scripts") with DAO.Database. Any help would be appreciated!


回答1:


At first I thought this was going to be a simple matter of executing the query

SELECT [Name] FROM MSysObjects WHERE Type = -32766

but that fails with "no read permission on MSysObjects". OLEDB seems to have access to the system tables locked down pretty tight. I even tried doing a CREATE VIEW listMacros AS SELECT [Name] FROM MSysObjects... followed by a SELECT * FROM listMacros. The VIEW (Query) was created successfully but OLEDB still told me that I wasn't allowed to read [MSysObjects].

Bummer.

However, I did some fooling around with VBA code in Excel and I got this to work...

Sub listAccessMacros()
Dim objAccess As Object  '' Access.Application
Dim i As Long
Set objAccess = CreateObject("Access.Application")
objAccess.OpenCurrentDatabase "C:\Documents and Settings\Administrator\Desktop\Database1.accdb"
For i = 0 To objAccess.CurrentProject.AllMacros.Count - 1
    Debug.Print objAccess.CurrentProject.AllMacros(i).Name
Next
objAccess.CloseCurrentDatabase
objAccess.Quit
Set objAccess = Nothing
End Sub

...so perhaps you can come up with a way to tweak it so it will work in VB.NET. (Sorry, but I don't have ready access to VB.NET myself....)



来源:https://stackoverflow.com/questions/15887387/using-vb-net-to-obtain-macros-in-ms-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!