Monday, May 30, 2011

Power tip: Use the form property for the subform in the code refer to

Today's power tip is provided by Juan Soto.

I love of form variables. It my code faster and easier to read, and they also offer a great way to remote control another form without is in this form or have it focus. Another advantage is that reading data record values from a form faster than using a Recordset much use a table from the hard disk. If you know that the data you are looking for is already loaded on a form, the read you a form variable can speed up your application.

For example, suppose I am coding in a form with the name of FrmOrders and I need called to controls on an open form (you can only a form variable to an open form map) FrmCustomers read. I would use the following code to my variable instantiate:

Dim frm as Form_frmCustomers
Dim LngCustomerID as long

If CurrentProject.AllForms("frmCustomers").IsLoaded, then
Set frm = forms! FrmCustomers
' Use the frm variable fields from the form read
LngCustomerID = Frm.CustomerID
Else
"To a different technique to CustomerID find using"
End if

The frm as Form_frmCustomers instead of the generic type form declare one of the advantages, that IntelliSense is opens so that you refer to controls on the form. You can do this with any shape, which is already loaded. So make sure that you followed by the name of the form "Form_" at the declare variable of this type use.

Subforms will lose as part of the main form and their own identity, as soon as they are loaded. A subform to reference, you must use the form property to associate the form variable contains the subform. We take on FrmOrders is the parent form and the name of the subform in FrmOrdersis FrmOrderItems_sub . If you need to reference the subform, this could do in the following thing:

Dim frm as Form_frmOrderItems_sub

Set frm = forms! frmCustomers. frmOrderItems_sub.Form

See the note on the .Form property at the end of the second statement, which is required to the variable frmsubform map.

Note 1: You can use only form variables in forms that have modules; Otherwise, the compiler will error out.

Note 2: Make sure that you reference the correct name of the subform to the main form or the code will not work. For example, if the subform control has a SourceObject is set to a form named "FrmOrderItems_sub", but a control name of "Order items" on the main form has code should look then like this:
Dim frm as Form_frmOrderItems_sub

Set frm = forms! frmCustomers.OrderItems.Form

I encourage you start in your code using form variables, if you need to reference a form from one other. It is another great tool in access, which facilitates sharing information about your application and may even accelerate it.

Juan Soto is senior Access developers at AccessExperts.net. His blog is at AccessExperts.net/blog. You can subscribe to it here. Juan is a frequent speaker at access and SQL Server user groups nationwide and is a contribution to the access blog.

Send your power tips to accpower@microsoft.com!

--Chris downs


View the original article here

No comments:

Post a Comment