Link to home
Start Free TrialLog in
Avatar of droy0521
droy0521

asked on

Passing an object to a form

I created a generic "Open Form" prodedure that will do some standard formatting to a form before it opens.  Worked great when forms were bound.  However, I'm switching over to an OOD and need to pass objects into a form. The best way I've come up with is have a public subroutine within the form's module.  

When I've explicitly called the subroutine (i.e. Set frmForm = FORM_frmWhatever), I can call the subroutine.  However, when I try to call the subroutine from a generic Form object, I get a "Application-Defined Error."  Any help would be appreciated.
Sub OpenForm(strFormName as string, Optional objObject as Object)        
dim frmFrom as form
set frmform = forms(strformname)
If Not objObject Is Nothing Then   
   frmForm.CustomSub 'Application Error Here
End If
 frmform.visible = true
end sub

Open in new window

Avatar of puppydogbuddy
puppydogbuddy

In lines 2 and 3, you use different spellings for your objects: frmFrom vs frmform
dim frmFrom as form
set frmform = forms(strformname)

Avatar of droy0521

ASKER

That was just a typo when I rewrote the code to get rid of some of the other procedures that are called prior to the sending of the object to the form.  Below is the direct copy and past from my modules.
'****************************************************************************************
Public Function OpenForm(strFormName As String, _
    Optional strWHERE As String, _
    Optional objObject As Object)
On Error Resume Next
    Dim frmForm As Form
 
    gstrCallingForm = Screen.ActiveForm.Name
 
On Error GoTo HandleError
    Set frmForm = GetForm(strFormName)
    
    With frmForm
        
        FormatFormDefaults frmForm
        
        If Not objObject Is Nothing Then
            
            frmForm.GetObject objObject
            
        End If
        
        If strWHERE <> "" Or Not IsNull(strWHERE) Then
            
            .FilterOn = True
            .FilterOnLoad = True
            .Filter = strWHERE
        End If
        
        .visible = True
        ChangeFormView .Name, acNormal
 
    End With
    
Exit_Function:
Exit Function
HandleError:
    Select Case Err.Number
        Case 2450 'Form Already closed, happens when user opens a form from within itself
            Resume Next
        Case Else
            GeneralErrorHandler Err.Number, Err.Description, MODULE_NAME, "OpenForm"
    End Select
    GoTo Exit_Function
End Function
 
FROM THE FORM:
Public Sub GetObject(objSentMember As Object)
    
    Set objMember = objSentMember
    
End Sub

Open in new window

Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Try this:

frmForm_CustomSub()

No that didn't work either.  It thinks you're trying call a procedure.
"It thinks you're trying call a procedure."

Isn't that what you are trying to do?  If not, what ?

mx
Sorry, I should have been more specific.  The compiler thinks I'm trying to call a procedure name "frmForm_GetObject" when really I'm trying to call the procedure "GetObject" which is within the frmForm module.
Let me be a little bit more specific.  I have the intention of creating a procedure called "GetObject" in every form that I make unbound.  What I want the procedure OpenForm to do is: if an object is passed into it, then to pass the object onto the form which is being opened.  The form will then set its controls' values according to the properties of the object.

thanks,
Dennis
So, you are trying to pass the name of a procedure in Optional objObject as Object ... the call that procedure in the code?

mx
There's two different examples below.  The first one works, but the second one does not.  I need to make the second one work.


Example #1 - Works
Sub OpenForm(Optional objObject as object)
    Set frmForm = New Form_pfmMemberClubEnrollment
    If Not objObject Is Nothing Then
       frmForm.GetObject objObject
    End If
Exit Sub
 
Example #2 - Doesn't work
Sub OpenForm(strFormName as string, Optional objObject as Object)
 Dim frmForm as Form
  Set frmForm = Forms(strformname)
        If Not objObject Is Nothing Then
            frmForm.GetObject objObject
        End If
exit sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of puppydogbuddy
puppydogbuddy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Unfortunately, that didn't work either.  Apparently, I need to look for another technique for passing the objects into the form.  
I'm not really clear on what this 'object' is ... and what you are going to do with it ?

mx
I'm designing a database for tracking all of the 4-H members that I coordinate for the county I work at.  Each member record has about three or four other tables it has one-to-many relationships with (clubs, projects, exhibits, etc.).  With all of the relationships & volume of records/indexes, performance was starting to suffer & I wanted to have an eye on going multi-user in the future.  So I decided to convert the database over to an Object Oriented Design.  So I've made some custom classes (members, leaders, parents, etc).  Most of these of these classes has custom collections as properties of themselves.

So for instance, I have a member who is enrolling in a new club.  My standard form has a button that opens a popup form (which then has two list boxes, one with possible to clubs the member could join, the other with clubs the member is already enrolled in).  Since the "base form" is object oriented, the goal was to pass the member object/class from the base form to the popup form(s).  My goal was to minimize the number of connections to the backend dataset, since all of the information had already been loading into the object.

Does that make sense?  
I just got back on-line.  It didn''t work because I did not give you the entire picture.  This should work.

Code in the calling form:
Call OpenForm(Me)
___________________________
Code in the called sub :
Sub OpenForm(frmForm as Form)
   Set frmForm = Forms(frmForm.Name)
 exit sub
I finally figured it out.  The comment about not having the methods to the form led me to doing some troubleshooting.  in my "OpenForm" procedure, I had changed the form to design view while it was hidden.  Because ti was in design view, I couldn't reference the methods of the form.  It works now that I moved some of the sequencing around.

thanks,
Dennis