简体   繁体   中英

MS Access 2007 Converting forms into sub form - “form not found” issue

So I recently have been trying to incorporate more sub forms to make the UI more friendly. So I have a pre developed form that runs some VB, so for examples sake lets just say that it runs SQL statement, returns the recordset, and fills text boxes with the data (because this is sort of an interactive dashboard concept:

dim db as database
dim rs as recordset
dim sql as string
set db = current db

sql = "SELECT * FROM tblMain;"

   set rs = db.OpenRecordSet(sql)

         rs.movefirst
             [forms]![DashboardSubName].txtValue1 = rs!Value1
         rs.close

 set rs = nothing
 set db = nothing

with error handling that returns a error "DashboardSubName" not found. So orginally this form was its own form, and opening it by itself it works fine, but once I use it within the parent form i get this error.

im sure it is simply something i am not aware of, but what gives?

thanks guys justin

When a Form is loaded as a sub-form, it is referenced as if it were just another control on the Main Form - it is no longer part of the global Forms collection.

So the correct reference should be: Me!DashboardSubName.Form.txtValue1

where DashboardSubName is the name of your sub-form control.

Assuming the code you showed us is part of the subform, rather than the parent form, replace

[forms]![DashboardSubName].txtValue1 = rs!Value1

with

Me.txtValue1 = rs!Value1

The reason for the syntax here is that the subform control is distinct from the subform embedded inside it. The subform control has properties of its own (limited in comparison to the subform itself), and to get to the properties/methods of the subform embedded in it you have to specify that you want the form that the subform embeds.

  Me!MySubformControl

...is the subform control.

  Me!MySubformControl.Form

...is the form embedded in the subform control.

Last of all, I really wonder why you're walking a recordset to update data in a subform. This is not normal practice, though it's tangential to your actual question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM