I want to return true if a form exist or not.

  • Thread starter Thread starter blankman
  • Start date Start date
B

blankman

Hello, I'm new to the world of vba and I'm trying to write a boolean function
that that returns true if the form exist or false if it doesn't. Here's what
I got so far, I got it from another programer off of this site.

Public Function FormExists(strFormName As String) As Boolean
Dim fExists As Boolean
Dim db As DAO.Database
Dim intI As Integer
Set db = CurrentDb()
Do Until intI = db.Containers("AllForms").Documents.Count - 1 Or fExists
If db.Containers("AllForms").Documents(intI).Name = strFormName Then
fExists = True
Else
intI = intI + 1
End If
Loop
FormExists = fExists
Set db = Nothing
End Function
 
Hello, I'm new to the world of vba and I'm trying to write a boolean function
that that returns true if the form exist or false if it doesn't. Here's what
I got so far, I got it from another programer off of this site.

I'd suggest something simpler: attempt to reference the form from the
Containers collection, and trap the error:

Public Function FormExists(strFormName As String) As Boolean
Dim db As DAO.Database
Dim strName As String
On Error GoTo Proc_Error
FormExists = True
Set db = CurrentDb
strName = db.Containers("Forms").Documents(strFormName).Name
Proc_Exit: Exit Function
Proc_Error:
If Err.Number = 3265 Then ' Item Not Found In This Collection error
FormExists = False
Else
MsgBox "Error " & Err.Number & " in FormExists:" & vbCrLf _
& Err.Description
End If
Resume Proc_Exit
End Function

John W. Vinson[MVP]
 
Back
Top