Refer to subform

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a Main form with two subforms Sub1 and Sub2.

I want to set the RecordSource of Sub2 from the OnGotfocus event on Control1
on Sub1.

My code is:

Dim intCtrl as integer
Dim strSQL as string

intCtrl = Me.Control1
strSQL = "Select * from MyTable where Id = " & strSQL
Forms!Main!Form!Sub2.RecordSource = strSQL
Forms!Main!Form!Sub2.RecordSource.ReQuery

When Control1 gets the focus I get an error Runtime error 2465 Access can't
find the field 'Form' referred in your expression.

Have tried many permutations but no luck.

Grateful for help.
 
Try this
Forms!Main.Form!Sub2.RecordSource = strSQL
Forms!Main.Form!Sub2.RecordSource.ReQuery

instead of

Forms!Main!Form!Sub2.RecordSource = strSQL
Forms!Main!Form!Sub2.RecordSource.ReQuery
 
if i understand you correctly, both subforms (1 and 2) are on the open main
form, and Control1 is a control on the Sub1 subform. first, i think your
strSQL line should be

strSQL = "Select * from MyTable where Id = " & intCtrl

and try the next two lines as

Me.Parent!Sub2.Form.RecordSource = strSQL
Me.Parent!Sub2.Form.Requery

since you're running the code from the GotFocus event of Control1, keep in
mind that if there's no value in the control when it gets the focus, your
code will at best give you no results, and at worst will err out.

other than that, just make sure that Sub2 is the name of the subform
*control* within the main form (the subform control name *may* be different
than the name of the subform).

hth
 
Back
Top