This fully commented function below describes how to check if a report exists in the database or not. If you are writing reports dynamically using VBA, you must have a function like this one in your database to ensure you are not overwriting fully functional existing reports. We use this function in MAARS code base while creating new reports dynamically. Feel free to copy this function in your program and become efficient with your development.
Function reportExistsInTheDatabase(rptName As String) As Boolean
'Purpose: This function checks if a report already exists in the database where this code is executed
' It returns true if the report exists, otherwise it returns false
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentProject 'Let's get the content of the project explorer
reportExistsInTheDatabase = False ' Assume the Report does not exists in the database
For Each obj In dbs.AllReports 'Check each Report in the Reports collection using a for loop
If obj.Name = rptName Then ' The current report name matches the given report name
reportExistsInTheDatabase = True 'Report was found
Exit Function ' Exit the function, no need to continue the for loop and exit out of the function
End If
Next obj
'Usage: How to call
' Dim rst as boolean
' rst=reportExistsInTheDatabase("My Report")
End Function
This is a complimentary article written by the MAARS team for the MAARS user community. Code in this article drives the operation of MAARS (MS Access Application wRiting Software). MAARS is an intelligent automation program that speeds up MS Access Application Development by 10x, 20x or 100x times. To learn more about MAARS, click here.
Disclaimer:
Some information included in this article may have been sourced from other publicly available websites and blogs. In such cases, credit goes to those authors for the original ideas and thoughts, but we do take credit for putting valuable information together and improve the efficiency of other office developers.