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

Ramzi

New member
Local time
Today, 16:41
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, 16:41
Joined
May 8, 2022
Messages
13
Guys!
Basically i have a table
ID
ObjectType
ObjectName
HasAccess

I want to get the names of all Forms and Reports in this table instead typing one by one.

Anyone who can help me to get ride on it.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:41
Joined
Feb 19, 2002
Messages
43,275
Here's a table that is useful when you write queries against MSysObjects. To find out what object types you have run a query on MSysObjects and look at all the Type values. It is possible that this table is not complete because I don't have any databases with a particular object.
1710695235472.png
 

Ramzi

New member
Local time
Today, 16:41
Joined
May 8, 2022
Messages
13
Here's a table that is useful when you write queries against MSysObjects. To find out what object types you have run a query on MSysObjects and look at all the Type values. It is possible that this table is not complete because I don't have any databases with a particular object.
View attachment 113112
Thanks alot I got my problem solved.....
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:41
Joined
Feb 19, 2002
Messages
43,275
Anyone who can help me to get ride on it.
Use the MSysObjects table. Create your own copy of the table I pasted. That will make it easy for you to select the objects you are interested in and give them a "type" name.
This query will give you just the form's and reports.

1710695698818.png
 

LarryE

Active member
Local time
Today, 04:41
Joined
Aug 18, 2021
Messages
591
This query will list all your tables:
SELECT DISTINCT MSysObjects.Name, MSysObjects.Type, MSysObjects.Flags
FROM MSysObjects
WHERE (((MSysObjects.Type)=1) AND ((MSysObjects.Flags)=0))
ORDER BY MSysObjects.Name, MSysObjects.Type, MSysObjects.Flags;
 

spaLOGICng

Member
Local time
Today, 04:41
Joined
Jul 27, 2012
Messages
127
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
Why is it necessary to do this? There is already a hidden table in Access that already maintains this information. What you are doing is redundant and a waste of precious space in the database. Unless you are doing this for educational purposes and training exercises working with Recordsets, I recommend otherwise, echoing what others have stated above.

You can visualize the Access Hidden Tables by enabling the "Show Hidden Objects" as seen here. Click your right mouse on the All Access Objects and select Navigation Options.

Edit: You will need to enable "Show System Objects" too.
1711204292611.png




Here is a simple Query reading the MSysObjects Table. I rename the [Name] Column to [Object_Name] because [Name] is a reserved word and you will run into issues referring to it.

I included the Connecd and Database Columns because they are useful while working with linked Tables to Excel, Access, SQL Server, etc.
1711204610564.png


You can build off of the existing Access information that is already built-in. Why build it again!

You can create an INSERT Query based on the MSysObject and be done with it. You can join your Obejct Table to the MSysObject Table to remove Objects that you have already inserted into you table.

One caveat you need to consider is when you renamed an object. I would be better to store the ObjectID rather than the Name.
 
Last edited:

spaLOGICng

Member
Local time
Today, 04:41
Joined
Jul 27, 2012
Messages
127
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.
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.
You can build off of the existing Access information that is already built-in. Why build it again!

You can create an INSERT Query based on the MSysObject and be done with it. You can join your Obejct Table to the MSysObject Table to remove Objects that you have already inserted into you table.

One caveat you need to consider is when you renamed an object. It would be better to store the ObjectID rather than the Name.
 

jdraw

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

Based on your original request, saving names of tables/forms and reports to your own table, you have been given various options -all relating to MSysObjects. However, it seems to me that your underlying issue is assigning roles and permissions to various users, and that your desired table is but a step in that process. You may want to research role based access control (RBAC) for more information.

You might find this post/thread offers some insight. And this summary post.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:41
Joined
Feb 19, 2002
Messages
43,275
@spaLOGICng The OP needs a table to store security information for each object for each user but he technically doesn't need to "duplicate" the MSysObjects table. His security table can use the ID of the MySysObjects table as the FK.
 

spaLOGICng

Member
Local time
Today, 04:41
Joined
Jul 27, 2012
Messages
127
@spaLOGICng The OP needs a table to store security information for each object for each user but he technically doesn't need to "duplicate" the MSysObjects table. His security table can use the ID of the MySysObjects table as the FK.
Yes, I corrected myself in my most recent comment just two responses above yours, once I read about the security requirements. I, like you, would use as much of the built-in objects as possible. In my corrected statement, I mentioned using the ObjectID of the Table rather than the name, in the event he wanted to rename an object. The Foreign Key I did not mention, but it would be very useful if he add referential integrity for the cascade delete.
 

Users who are viewing this thread

Top Bottom