Save the names of tables, forms, and reports in a Microsoft Access database to a table named "ObjectNames. (2 Viewers)

Ramzi

New member
Local time
Today, 18:01
Joined
May 8, 2022
Messages
13
Public Sub SaveObjectNames()
On Error GoTo ErrorHandler

Dim db As DAO.Database
Dim obj As AccessObject
Dim strSQL As String
Dim rs As DAO.Recordset
Dim objectName As String

' Open the current database
Set db = CurrentDb

' Loop through each object in the current database and insert names into the table
For Each obj In db.AllTables
objectName = obj.Name
If Not IsObjectNameExist("Table", objectName) Then
InsertObjectName "Table", objectName
End If
Next obj

For Each obj In db.AllForms
objectName = obj.Name
If Not IsObjectNameExist("Form", objectName) Then
InsertObjectName "Form", objectName
End If
Next obj

For Each obj In db.AllReports
objectName = obj.Name
If Not IsObjectNameExist("Report", objectName) Then
InsertObjectName "Report", objectName
End If
Next obj

' Cleanup
Set obj = Nothing
Set db = Nothing

MsgBox "Object names have been saved to the table.", vbInformation
Exit Sub

ErrorHandler:
MsgBox "An error occurred: " & Err.Description, vbExclamation
End Sub

Private Function IsObjectNameExist(ByVal objectType As String, ByVal objectName As String) As Boolean
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

Set db = CurrentDb
strSQL = "SELECT COUNT(*) AS CountOfRecords " & _
"FROM ObjectNames " & _
"WHERE ObjectType='" & objectType & "' AND ObjectName='" & objectName & "'"

Set rs = db.OpenRecordset(strSQL)
If Not rs.EOF Then
IsObjectNameExist = (rs!CountOfRecords > 0)
End If

rs.Close
Set rs = Nothing
Set db = Nothing
End Function

Private Sub InsertObjectName(ByVal objectType As String, ByVal objectName As String)
Dim db As DAO.Database
Dim strSQL As String

Set db = CurrentDb
strSQL = "INSERT INTO ObjectNames (ObjectType, ObjectName) " & _
"VALUES ('" & objectType & "', '" & objectName & "')"
db.Execute strSQL
Set db = Nothing
End Sub
 

Ramzi

New member
Local time
Today, 18:01
Joined
May 8, 2022
Messages
13
Code:
Public Sub SaveObjectNames()
    On Error GoTo ErrorHandler
    
    Dim db As DAO.Database
    Dim obj As AccessObject
    Dim strSQL As String
    Dim rs As DAO.Recordset
    Dim objectName As String
    
    ' Open the current database
    Set db = CurrentDb
    
    ' Loop through each object in the current database and insert names into the table
    For Each obj In db.AllTables
        objectName = obj.Name
        If Not IsObjectNameExist("Table", objectName) Then
            InsertObjectName "Table", objectName
        End If
    Next obj
    
    For Each obj In db.AllForms
        objectName = obj.Name
        If Not IsObjectNameExist("Form", objectName) Then
            InsertObjectName "Form", objectName
        End If
    Next obj
    
    For Each obj In db.AllReports
        objectName = obj.Name
        If Not IsObjectNameExist("Report", objectName) Then
            InsertObjectName "Report", objectName
        End If
    Next obj
    
    ' Cleanup
    Set obj = Nothing
    Set db = Nothing
    
    MsgBox "Object names have been saved to the table.", vbInformation
    Exit Sub
    
ErrorHandler:
    MsgBox "An error occurred: " & Err.Description, vbExclamation
End Sub

Private Function IsObjectNameExist(ByVal objectType As String, ByVal objectName As String) As Boolean
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
    
    Set db = CurrentDb
    strSQL = "SELECT COUNT(*) AS CountOfRecords " & _
             "FROM ObjectNames " & _
             "WHERE ObjectType='" & objectType & "' AND ObjectName='" & objectName & "'"
    
    Set rs = db.OpenRecordset(strSQL)
    If Not rs.EOF Then
        IsObjectNameExist = (rs!CountOfRecords > 0)
    End If
    
    rs.Close
    Set rs = Nothing
    Set db = Nothing
End Function

Private Sub InsertObjectName(ByVal objectType As String, ByVal objectName As String)
    Dim db As DAO.Database
    Dim strSQL As String
    
    Set db = CurrentDb
    strSQL = "INSERT INTO ObjectNames (ObjectType, ObjectName) " & _
             "VALUES ('" & objectType & "', '" & objectName & "')"
    db.Execute strSQL
    Set db = Nothing
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:01
Joined
Sep 21, 2011
Messages
14,306
What is not doing that you want it to?
Have you walked your code with F8 and breakpoints?
 

ebs17

Well-known member
Local time
Today, 15:01
Joined
Feb 7, 2020
Messages
1,946
First of all, I don't understand your measure. All information (and some more) is already contained in the system table MSysObjects.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:01
Joined
May 7, 2009
Messages
19,245
table names, form names, report names, etc. are already in table MsysObjects, why re-inventing it?
 

Ramzi

New member
Local time
Today, 18:01
Joined
May 8, 2022
Messages
13
First of all, I don't understand your measure. All information (and some more) is already contained in the system table MSysObjects.
Actually i want to save the names in my own table to give the permission for users where authorized users can access the forms reports.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:01
Joined
Sep 21, 2011
Messages
14,306
So use the correct syntax.? :( do not make names of properties up. See the link in post #9
 

ebs17

Well-known member
Local time
Today, 15:01
Joined
Feb 7, 2020
Messages
1,946
Code:
SELECT * FROM MSysObjects
Take a look at this.

Then you take only the fields you need, and you filter out records other than the forms and reports and tables, and you can insert the rest into your table.
Whether this query is carried out by hand or by code is up to your skill.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:01
Joined
Feb 28, 2001
Messages
27,186
You COULD drop us a hint... what error is reported when it fails? Or do you have error reporting somehow disabled despite having an ON ERROR trapper in place? The specific error should tell us more about what is happening. To ask "What is wrong" you HAVE to know our response will be "Specifically, how do you know something is wrong?"
 

Ramzi

New member
Local time
Today, 18:01
Joined
May 8, 2022
Messages
13
You COULD drop us a hint... what error is reported when it fails? Or do you have error reporting somehow disabled despite having an ON ERROR trapper in place? The specific error should tell us more about what is happening. To ask "What is wrong" you HAVE to know our response will be "Specifically, how do you know something is wrong?"
 

Attachments

  • Error.JPG
    Error.JPG
    11.2 KB · Views: 25

jdraw

Super Moderator
Staff member
Local time
Today, 09:01
Joined
Jan 23, 2006
Messages
15,379
Ramzi,

You might want to review some recent(Oct/23) work "List Access Objects" by Crystal (strive4peace). The video and accompanying application do what you are asking and much more. You can review/inspect her code in detail to learn various coding techniques.
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:01
Joined
Sep 21, 2011
Messages
14,306
You COULD drop us a hint... what error is reported when it fails? Or do you have error reporting somehow disabled despite having an ON ERROR trapper in place? The specific error should tell us more about what is happening. To ask "What is wrong" you HAVE to know our response will be "Specifically, how do you know something is wrong?"
OP appears to be mixing up properties ffrom different objects? :(

The AllTables collection contains an AccessObject for each table in the CurrentData or CodeData object.
Code:
Sub AllTables()
 Dim obj As AccessObject, dbs As Object
 Set dbs = Application.CurrentData
 ' Search for open AccessObject objects in AllTables collection.
 For Each obj In dbs.AllTables
 If obj.IsLoaded = True Then
 ' Print name of obj.
 Debug.Print obj.Name
 End If
 Next obj
End Sub
 

Users who are viewing this thread

Top Bottom