Ms-Access Advanced VBA Programming Techniques, Tips and Tricks.

Setting Focus on a field inside a Sub-Form

A Main Form (frm_Students) have two Sub-Forms (frm_Sessions and frm_Payment) on it.  A sample image of such a Form is given below:

The sub-forms on the main form are treated as objects of the main form.  These objects (Sub-forms) have other controls (like Textboxes) within them and we cannot set focus on one of the text controls within the sub-form directly for keying in values into it.  That doesn’t mean that we cannot address the controls directly to retrieve or set value into that control through vba.

Setting focus on Amount field of frm_Payments directly, when frm_Payments doesn't have focus on through vba is not possible.

Example:

'this statement will not work when frm_Payments doesn't have focus
Forms![frm_Students]![frm_Payments].Form.Amount.SetFocus

The above statement may highlight the Amount field, but the focus will not change to that field.

But, we can retrieve the value directly from the Amount field of frm_Payments, even when the focus is not ON on that form, with the following statement.

Example-2:

'this statement retrieves the Amount field value directly.
m_Amt = Forms![frm_Students]![frm_Payments].Form!Amount

When the frm_Payments sub-form have the focus ON you can address a control (say the Amount field) within that form to set the real focus on it.

It simply means that it takes two steps action to address a control within a sub-form to set focus on:

  1. Set focus on the sub-form object first.  By setting Tab Index Value of the frm_Payment Sub-Form to 0 also works.
  2. Set focus on any control within the frm_Payments sub-form.

So, it is a two step process and the following two VBA statements does the job:

Me.Payments.form.SetFocus
Me.Payments.form.Amount.SetFocus

The following version of the above statements are also valid:

Forms![frm_Students]![frm_Payments].SetFocus
Forms![frm_Students]![frm_Payments].Form.Amount.SetFocus

Next we will see how to jump from last field of one sub-form record to it's corresponding record field on the second sub-form directly.  If you think it is so easy after learning the above two lines, then think twice.

Share:

No comments:

Post a Comment

Trending

Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Labels

Forms How Tos MS-Access Security Functions Reports Animations msaccess animation msaccess forms Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Queries External Links msaccess reports msaccess tips Menus and Toolbars Accesstips MsaccessLinks Process Controls Art Work Downloads msaccess How Tos Graph Charts msaccessQuery List Boxes Command Buttons Emails and Alerts Query Combo Boxes Custom Wizards msaccess graphs msaccess reporttricks ms-access functions msaccessprocess security advanced Access Security DOS Commands Data Macros Menus Property Report Top Values VBA msaccess email msaccess menus progressmeter Access2007 Array Auto-Number Command Button Join Microsoft Numbering System Records Security Split SubForm Table Utility Workgroup database msaccess functions msaccess wizards Access2003 Accounting Year Action Animation Attachment Binary Numbers Budgeting Calculation ChDir Color Palette Conditional Formatting Controls Copy Custom Functions Data Filtering Data Type Defining Pages Diagram Disk Dynamic Lookup Error Handler Excel Export Expression External Field Type Fields Filter Form Form Instances Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Methods Monitoring Object Reference Objects Octal Numbers Operating System Paste Primary-Key Product Rank Reading Rich Text Sequence SetFocus Summary Tab-Page Time Difference Union Query User Users Variables Water-Mark Word automatically commands function hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar tutorial updating upload vba code

Featured Post

Important Notice

Dear Readers, Very sorry to inform you that I am finding it difficult to renew my contract with the Hostgator Hosting Plans to continue prov...

Labels

Blog Archive

Recent Posts