Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run-time error "2450" Application cannot find form - But form is open

Status
Not open for further replies.

smsmail

Programmer
Aug 11, 2010
105
US
Hello,

I do not understand why I am getting the run-time error "2450", when the form is open. The code the is bold is getting the error. Your expert advise is truly appreciated:


Code:
If CurrentProject.AllForms("frmclientmailschedule").IsLoaded Then
     If i[b]Forms!frmClientMailSchedule!ScheduleID[/b] <> " " Then
         Call openFormInstance("frmClientMailSchedule",   Forms!frmClientMailSchedule!ScheduleID, "Main")
       Else
           DoCmd.OpenForm "frmclientmailschedule", , , , acNew
          'IntMoveWindow = IntMoveWindow + 1
          'DoCmd.MoveSize (IntMoveWindow + 1) * 80, (IntMoveWindow + 1) * 350
    End If
 Else"
       DoCmd.OpenForm "frmclientmailschedule", , , , acNew
       'IntMoveWindow = IntMoveWindow + 1
       'DoCmd.MoveSize (IntMoveWindow + 1) * 80, (IntMoveWindow + 1) * 350
End If

 
What is iForms ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV, how are you?

A instance of the form is open, when trying to open another instance of the form I get the run-time error "2450". I tried recoding that line of code (i.e. if isnull(Forms!frmClientMailSchedule!ScheduleID) = false then), and get the same error. Is it something I am missing? I am at a lost!!!

sorry about the error when copying the code: The code in my program is below:

Code:
If CurrentProject.AllForms("frmclientmailschedule").IsLoaded Then
     If [b]Forms!frmClientMailSchedule!ScheduleID [/b] <> " " Then
         Call openFormInstance("frmClientMailSchedule",   Forms!frmClientMailSchedule!ScheduleID , "Main")
       Else
           DoCmd.OpenForm "frmclientmailschedule", , , , acNew
          'IntMoveWindow = IntMoveWindow + 1
          'DoCmd.MoveSize (IntMoveWindow + 1) * 80, (IntMoveWindow + 1) * 350
    End If
 Else"
       DoCmd.OpenForm "frmclientmailschedule", , , , acNew
       'IntMoveWindow = IntMoveWindow + 1
       'DoCmd.MoveSize (IntMoveWindow + 1) * 80, (IntMoveWindow + 1) * 350
End If

A instance of the form is open, when trying to open another instance of the form I get the run-time error "2450". I tried recoding that line of code (i.e. if isnull(Forms!frmClientMailSchedule!ScheduleID) = false then), and get the same error. Is it something I am missing? I am at a lost!!!

 
Why would you open multiple instances of the same form ?
I don't think you may use the Application.Forms collection for a form multi-instantiated but the class name, ie Frm_xxx

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is strange, but I believe I understand what is happening. When you open a form by its class, it does get added to the forms collection. It appears however the name is not added as a key. So you cannot retrieve the form by

Forms("formName") or Forms!FormName

Try this
Code:
Public Sub FormInstance()
  Dim frm As Access.Form
  Set iFrm = New Form_Form1
  'Prove loaded
  MsgBox "Is loaded" & CurrentProject.AllForms("form1").IsLoaded
  'Prove 1 form in forms collection
  MsgBox Forms.Count
  'return that form
  For Each frm In Forms
    MsgBox frm.Name
  Next frm
  'return the form using the numeric index
  MsgBox Forms(0).Name
  'The following fails when returning it using the key
  MsgBox Forms("Form1").Name
End Sub

so here is the solution

Code:
Public Function GetIndex(frmName As String) As Integer
  Dim frm As Access.Form
  For Each frm In Forms
     If frm.Name = frmName Then
       Exit Function
     End If
     GetIndex = GetIndex + 1
  Next frm
End Function
to use
Code:
.....
If CurrentProject.AllForms("frmclientmailschedule").IsLoaded Then
     If Forms(getIndex("frmClientmailschedule"))  <> " " Then
 
Should read

If Forms(getIndex("frmClientmailschedule")).scheduleid <> " " Then
 
Thanks so much for your help.

The strange thing is that the code once worked. But after my application crashed due to data corruption and then loosing 3 weeks worth of code because of the crash (by the way, I still get teary eyed when I think about it), I am piecing my application back together by way of an old backup. Now the code in question does not work with the backup version of my application.

I will try MajP's suggestion. Any other thoughts as to why the code is not working is appreciated.

 
The behavior is unexpected, but I get the exact same results. A form opened as a form instance (not using docmd.openform) gets added to the forms collection but it is not given a named index in the collection. This behavior could be a bug or as designed. Maybe this is a current bug and that is why it used to work. Or maybe there are conditions when the named index is added to the forms collection.

If you are rebuilding this, I would strongly recommend not using the forms collection at all. You need to build your own custom collection for managing the form instances. That way you can specifically open, close, and refer to an instance. I would use the Schedule ID as the Key.

You can add methods to the custom collection.

Example

class SolitaireForms
Code:
Private mForms As New Collection
Private mForm As Access.Form

' Add a form object to the collection.
Public Sub Add(ByVal frm As Form_frmSolitaire, ByVal key As String)
    frm.Visible = True
    mForms.Add frm, key
End Sub

' Return the number of items in the collection.
Public Function Count() As Long
    Count = mForms.Count
End Function

' Remove a Form object from the collection.
Public Sub Remove(ByVal Index As Variant)
    mForm.Remove Index
End Sub

' Return a Solitaire Form object.
Public Function Item(ByVal Index As Variant) As Access.Form
    Set Item = mForms(Index)
End Function

used as
Code:
Dim sfSolitaireForms As New SolitaireForms

Public Sub openSolitaire()
  ...
  Dim frmInstance As Form_frmSolitaire
  ...
  Set frmInstance = New Form_frmSolitaire
  'Set specific properties for an instance
  With frmInstance
      .Recordset.Move intCount
      .Move xMove, yMove, lngWidth, lngHeight
      .Caption = frmInstance.strLastName
      .NavigationButtons = False
      .DividingLines = False
      .RecordSelectors = False
      .ScrollBars = 0
    End With
   sfSolitaireForms.Add frmInstance, CStr(frmInstance.Hwnd)
   ... 
End Sub

Public Sub closeAllSolitaireForms()
 Dim intCounter As Integer
 For intCounter = sfSolitaireForms.Count To 1 Step -1
    DoCmd.Close
 Next intCounter
 DoCmd.Close acForm, "frmSolitaire"
End Sub

Public Sub RemoveInstance(frm As Form_frmSolitaire)
  On Error Resume Next
  sfSolitaireForms.Remove (CStr(frm.Hwnd))
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top