Pass subform reference to a module (1 Viewer)

liddlem

Registered User.
Local time
Today, 07:19
Joined
May 16, 2003
Messages
339
I have a few Navigation forms - which obviously have subforms in them
Each subform processes data and I want to update each subform in a similar way.(but contained within a Navigation form)

To do this, I have put common code into a function and then have the function update the relevant subform object accordingly.

For example;
Lets say that my NavFormA has 3 tabs on it called 'Sub1', 'Sub2' and 'Sub3' (Not their real names)

Each subform has a button to launch a process.
It also has 3 labels that I want to update at various stages of the process - like this.

SubFrm1
- BtnImport
-Label1
-Label2
-Label3

SubFrm2
- BtnImport
-Label1
-Label2
-Label3

SubFrm3
- BtnImport
-Label1
-Label2
-Label3

My function looks something like this
Code:
Function MyFunc_SubFrm1 (FrmName as form)
    processData_A
    FrmName!Label1.Caption = "process A name"
    ProcessData_B
    FrmName!Label2.Caption = "process B name"
    ProcessData_C   
    FrmName!Label3.Caption = "All processes completed at : " & CStr(Now())   
End Function

SubFrm2 (on each Navigation form) will point to a similar function called MyFunc_SubFrm2 and so on.

This works fine if I open the form on its own, but when it is a subform on a NavForm, it breaks.
Code:
Private Sub Btn_Import_Click()
Dim FrmName As Form
Set FrmName = Screen.ActiveForm

MyFunc_SubFrm2 FrmName

End Sub

So how do I get the line 'Set FrmName = ' to pass Forms!NameOfMyNavFrm!NameOfMySubFrm
In other words, I want it to
Set FrmName = Forms!NavA!SubFrm1 or
Set FrmName = Forms!NavA!SubFrm2 or
Set FrmName = Forms!NavB!SubFrm1 or
Set FrmName = Forms!NavB!SubFrm2 or
Set FrmName = Forms!NavB!SubFrm3 or
Set FrmName = Forms!NavC!SubFrm1 etc. etc - depending on which NavForm or Subform the call is being launched from.

Hope this makes sense.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:19
Joined
May 7, 2009
Messages
19,233
you only need to pass the "navigationSubform" to your function.
you check the SourceObject of the navigation form for the correct "object name".
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 02:19
Joined
Feb 19, 2002
Messages
43,263
You refer to the subform not by the subform name but by the name of the subform control.
Open the main form in design view. Click to select the subform control.
Look at the control's Name property. THAT is how you reference the subform.

Keep in mind that since there is only one subform control on an Access Navigation Form, only ONE subform is ever loaded at one time so there is no conflict. So, regardless of the name of the subform, You refer to it this way:

Me.SubformControlName.Form!Label1.Caption = "process A name"

Just use the directions above to get the value you need for SubformControlName.
 

liddlem

Registered User.
Local time
Today, 07:19
Joined
May 16, 2003
Messages
339
Thanks for your replies. I am only working on my DB over week-end, hence my late response.

Pat: My function is in a module apart from the form, so 'Me.' should not apply?

I have tried to update a caption in my form as follows'
Code:
 FrmName![Label2].Caption = fPath

But I'm still getting a 2465 Error
Err Msg : Microsoft Access cannot find the field 'Label2' referred to in your expression.

I have also tried the following 'variations of a theme'
Code:
    FrmName.Form!SLinesLbl.Caption = fPath
    Forms("ADM_Student_Import").SLinesLbl.Caption = fPath
    Screen.ActiveForm![SLinesLbl].Caption = fPath
    Forms!ADM_Student_Import!SLinesLbl.Caption = fPath
    ADM_Student_Import!SLinesLbl.Caption = fPath
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:19
Joined
May 7, 2009
Messages
19,233
see this demo if you can get it to work with your navigation form.
 

Attachments

  • func_navigationForm.accdb
    440 KB · Views: 278

liddlem

Registered User.
Local time
Today, 07:19
Joined
May 16, 2003
Messages
339
Thanks ArnelGP- Thas EXACTLY what I was looking for.
My mistake is that I was not using 'With FORMS'

Wiki:
With Forms![navigation form]![navigationsubform]
    ![Label0].Caption = lbl1
    ![Label1].Caption = lbl2
    ![Label2].Caption = lbl3
End With

Thanks for your assistance.
 

Users who are viewing this thread

Top Bottom