1

I like to index the content (tables) and the code (VBA, view queries) of several MS Access files, preferably on a machine that doesn't have MS Access installed. I used OleDb.OleDbConnection to get the names and content of all tables.

Is it possible to use OleDb.OleDbConnection to get the content of all VBA modules and view queries inside the MS Access database as well? I know something like that is possible in SQL server since all stored procedures or views are stored in some system tables, but I'm not sure about MS Access.

3
  • Which version of Access? (ie., is it an .accdb or .mdb?) also is this vba (like you question's title) or .net (like it's tagged)? can you show your code? Commented Jul 14, 2018 at 10:24
  • Preferrably both versions of Access (accdb and mdb), I used the ODBC provider for the new Access version, which is working for both versions. Commented Jul 14, 2018 at 12:18
  • And my program (the indexer) is written in .NET but it's supposed to extract the VBA modules from the access files. Commented Jul 14, 2018 at 12:19

3 Answers 3

1

You need the access "application" object to extract things like code modules etc.

Just connecting with the database engine (which is separate from Access) will not get you use of the access object model.

So while VB6, FoxPro, c#, vb.net can use the database engine separate from the application object model, to pull things like forms, reports or code modules, you will need the access.application object model.

Access is a development tool. You can use Access to build applications that work with Oracle, or SQL server. So often, the data engine you use with Access will not be the built in one, but an external data system. So to grab objects of the Access application (and not the data engine), then you need an installed copy of Access for this purpose.

This is really not different then say wanting to grab and work with the power-point object model. However, to be fair, all recent versions of office store “everything” as a zip file. I you rename a word file, excel file, power-point file as .zip extension, then you can simply open up that zip file and browse + see all of the xml, and even code.

However, Access is the only product that continues to store everything in a binary object model, and thus grabbing of those objects for all practical purposes requires the access application object to gain use of such objects. So while most office files can be re-named as a zip file of which then you can browse and extract the xml files inside of that zip container, unfortunately Access is not a “zip” container like most other office files.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I was assuming something like that.
0

For the Queries part

SELECT MSysObjects.Name
FROM MSysObjects
WHERE MSysObjects.Type=5;

Exclude the WHERE to see the different objects.

You could grab the sql by creating an Access Object in .net

Microsoft.Office.Interop.Access.Dao

See: https://stackoverflow.com/a/1458873/2895831

Microsoft.Office.Interop.Access.Application app = new Application(); 
app.OpenCurrentDatabase(@"[FILENAME]", false,"");
QueryDefs qdefs = app.CurrentDb().QueryDefs;
 foreach (QueryDef qdef in qdefs)
 {
     string qname = qdef.Name;
     string qSql = qdef.SQL;
 }
 app.Quit(AcQuitOption.acQuitSaveNone);

—-

To better understand the tables, you can go into the Nav Pane and unhide the Sys tables.

4 Comments

I'm outside of Access, and preferrably there's not even any Access installed. In the end I'd like to have a .NET program which can extract the VBA modules from accdb or mdb files
how would one get the contents of the vba modules using this? (or maybe I misunderstand the question)
I was answering one part of the question
Will I get the code part of a view (SELECT * FROM...) with such a query? Or just the names?
0

Also, see this question: How do you use version control with Access development?

Option Compare Database

Public Sub DocDatabase()
 '====================================================================
 ' Name:    DocDatabase
 ' Purpose: Documents the database to a series of text files
 '
 ' Author:  Arvin Meyer
 ' http://www.accessmvp.com/Arvin/DocDatabase.txt
 ' Date:    June 02, 1999
 ' Comment: Uses the undocumented [Application.SaveAsText] syntax
 '          To reload use the syntax [Application.LoadFromText]
 
 '====================================================================

 ' Minor modification & notes by imjosh, March 01, 2024
 ' This code is simple and it works, but this Version Control Add-in for MS Access does a whole lot more:
 ' https://github.com/joyfullservice/msaccess-vcs-addin

On Error GoTo Err_DocDatabase
Dim dbs As Database
Dim cnt As Container
Dim doc As Document
Dim i As Integer
Dim savePath As String

savePath = "D:\"
Set dbs = CurrentDb() ' use CurrentDb() to refresh Collections

Set cnt = dbs.Containers("Forms")
For Each doc In cnt.Documents
    Application.SaveAsText acForm, doc.Name, savePath & doc.Name & ".txt"
Next doc

Set cnt = dbs.Containers("Reports")
For Each doc In cnt.Documents
    Application.SaveAsText acReport, doc.Name, savePath & doc.Name & ".txt"
Next doc

Set cnt = dbs.Containers("Scripts")
For Each doc In cnt.Documents
    Application.SaveAsText acMacro, doc.Name, savePath & doc.Name & ".txt"
Next doc

' F: [\S\s\n]*?(Option Compare Database) R: $1 in vscode will delete everything before the actual code in the output for a Form
Set cnt = dbs.Containers("Modules")
For Each doc In cnt.Documents
   Application.SaveAsText acModule, doc.Name, savePath & doc.Name & ".txt"
Next doc

For i = 0 To dbs.QueryDefs.count - 1
   ' "~sq* files" are queries defined by the forms. They're already included in the Form itself and can be excluded
 ' https://stackoverflow.com/questions/187506/how-do-you-use-version-control-with-access-development
    ' Debug.Print dbs.QueryDefs(i).SQL ' to get just the SQL
    Application.SaveAsText acQuery, dbs.QueryDefs(i).Name, savePath & dbs.QueryDefs(i).Name & ".txt"
Next i

Set doc = Nothing
Set cnt = Nothing
Set dbs = Nothing

Exit_DocDatabase:
    Exit Sub


Err_DocDatabase:
    Select Case Err

    Case Else
        MsgBox Err.Description
        Resume Exit_DocDatabase
    End Select

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.