0
votes

I've a form named "FindCustomerCode" that is called by many others forms (orders process, billing, accounting and so on). This form is typically launched like this:

    DoCmd.OpenForm "FindCustomerCode", , , , , acDialog, Me.Name & "." & "NameOfFunctionIntentedToReceiveTheCustomerCodeChosenByUser"

Significant code in "FindCustomerCode" is:

    ArgV = Split(Me.OpenArgs, ".", -1)
    Calling_Form = ArgV(0)
    Return_Method = ArgV(1)
    (...)
    Ret = CallByName(Forms(Calling_Form), Return_Method, VbMethod, CustomerCodeChosenByUser)

This code perfectly works when the CALLING form was opened by Docmd.Openform.

The issue is : when the CALLING form was instantiated, the CallByName fails with error 2450: can't find the form "CallingForm" (approximative translation of french error message).

Code used to instantiate the calling form is (thanks to Gustav) :

    Public collForms As New Collection

    Dim fForm As Form_CallingForm
    Set fForm = New Form_CallingForm
    collForms.Add Item:=fForm, Key:=fForm.Hwnd
    fForm.SetFocus

It's quite weird. Once error 2450 is arisen :

    ?Forms(1).Name
    CallingForm
    if forms(1).Name = Calling_Form then ? "==="
    ===

but

    ?Forms(Calling_Form).Name
    --> error 2450

The workaround is not elegant but obvious. Basically :

Dim ii 
For ii = 0 To Forms.Count - 1
    If Forms(ii).Name = Calling_Form Then 
        Ret = CallByName(Forms(ii), Return_Method, VbMethod, CustomerCodeChosenByUser)
        Exit For
    End If
Next ii

I'll be glad if someone answers with a more elegant workaround, but my question is: why Forms(Calling_Form) does not work ?

Thanks

1

1 Answers

0
votes

As far as I recall (haven't worked with this for a long time), the culprit is, that all instances of the form are named the same in the Forms' collection. So your loop just catches the first instance of the form.

That's one task the collection tries to solve: To be able to identify a specific instance of the form by a unique key for which hWnd is the natural option.