I was writing a procedure for a client some time ago that interacted with a series of forms that came from a table, the problem being was that some of the specified forms no longer existed. When faced with this we have 2 options:
- Use error trapping to trap this specific error and move on
- Create some function to validate if the form exists before executing the code in the first place
I chose option 2 and set out to create a simple function to do exactly that. This is what I put together:
'---------------------------------------------------------------------------------------
' Procedure : FrmExist
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Determine whether or not a form exists in the current database
' Returns True if it exists, False if it does not
' Copyright : The following may be altered and reused as you wish so long as the
' copyright notice is left unchanged (including Author, Website and
' Copyright). It may not be sold/resold or reposted on other sites (links
' back to this site are allowed).
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sFrmName Name of the form to search for
'
' Usage:
' ~~~~~~
' ? FrmExist("frm_Login")
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2009-03-17 Initial Release
'---------------------------------------------------------------------------------------
Function FrmExist(sFrmName As String) As Boolean
On Error GoTo Error_Handler
Dim frm As Access.AccessObject
For Each frm In Application.CurrentProject.AllForms
If sFrmName = frm.Name Then
FrmExist = True
Exit For 'We know it exist so let leave, no point continuing
End If
Next frm
Error_Handler_Exit:
On Error Resume Next
Set frm = Nothing
Exit Function
Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: FrmExist" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
Then I could easily perform checks an act accordingly with a single call to my function, doing something like:
If FrmExist("frm_Login") = True Then
'Do something because the form was found
Else
'Do something because the form was not found
End if
I liked your solution, and by looking at it came up with this code:
Len(Application.CurrentProject.AllForms(pstrFrmName).FullName) > 0
It seems to do the job, without having to loop over all existing forms.
MS-Access user since version 1.0 came out.
Len(Application.CurrentProject.AllForms(pstrFrmName).FullName) > 0
The one-liner will result in error, if the form with the said name does not exist. But the many line function gracefully skipped the error.
Skipping the error is the preferred way, because working with errors with On Error statements opens up a can of worms.
Oh please, ‘On Error’ is used everywhere, there is absolutely no issue whatsoever with using it! None! I’ve used it throughout my coding, in hundreds of databases, 1000s of code samples, … for almost 20 years now and it does not pose any issue.
Use the approach you’d like, that’s the beauty of coding/VBA, no need for fear mongering.
I’m glad you said that. Because I have it on every sub. I’m no pro at coding. I know just enough to stay out of trouble.
Ralph