eliwil
asked on
use Dim frm As Form
I'm constantly typing out the same long form references over and over again because I am not confident I know how to use Dim whatever As Form to refer to a main forms, subforms, subsubforms. Using these examples; how could I use As Form to refer to the form in the first, the sub in the second and the subsub in the third?
Forms!frmMainInd!cboWhatev er
Forms!frmMainInd!fsubGrtOr g.Form!cbo Whatever
Forms!frmMainInd!fsubGrtOr g.Form!fsu bGrtApp.Fo rm!cboWhat ever
I'm Ok with strings but as soon as you get to As Form or As Control I need some clarification of when and how to use them.
E
Forms!frmMainInd!cboWhatev
Forms!frmMainInd!fsubGrtOr
Forms!frmMainInd!fsubGrtOr
I'm Ok with strings but as soon as you get to As Form or As Control I need some clarification of when and how to use them.
E
Hi,
A variable of type Form (or Object) can hold a pointer to any existing open form. You use it like this:
Dim frmAny As Form
Set frmAny = Forms!frmMainInd
frmAny!cboWhatever.Value = Null ' on main form
Set frmAny = Forms!frmMainInd!fsubGrtOr g.Form
frmAny!cboWhatever.Value = Null ' on sub form
Set frmAny = frmAny!fsubGrtApp.Form
frmAny!cboWhatever.Value = Null ' on previous' form subform...
The whole idea of objects is that you manipulate "pointers" to them. To create a new object, you use special instructions, and Access also creates new objects -- e.g. when you open a form. Form _variables_ however are merely pointers. Shortcuts if you like.
If you have a doubt about an object's type, you can ask in the immediate pane:
? TypeName( Forms!frmMainInd!fsubGrtOr g.Form!cbo Whatever )
Combobox
This means that you can create a variable of type Combobox to hold a pointer to that particular control. Some "family" objects exist, for example the type Control, that can point to any control, not just comboboxes.
If you just want to avoid typing, you can use the "implicit variable" of the With contruct, as in:
With Forms!frmMainInd!fsubGrtOr g.Form
!cboWhatever.Value = Null
.BackColor = vbRed
!!fsubGrtApp.Form.RecordSe t.MoveLast
End With
Every object starting with . or ! will be taken from the innermost With construct.
Does that help any?
(°v°)
A variable of type Form (or Object) can hold a pointer to any existing open form. You use it like this:
Dim frmAny As Form
Set frmAny = Forms!frmMainInd
frmAny!cboWhatever.Value = Null ' on main form
Set frmAny = Forms!frmMainInd!fsubGrtOr
frmAny!cboWhatever.Value = Null ' on sub form
Set frmAny = frmAny!fsubGrtApp.Form
frmAny!cboWhatever.Value = Null ' on previous' form subform...
The whole idea of objects is that you manipulate "pointers" to them. To create a new object, you use special instructions, and Access also creates new objects -- e.g. when you open a form. Form _variables_ however are merely pointers. Shortcuts if you like.
If you have a doubt about an object's type, you can ask in the immediate pane:
? TypeName( Forms!frmMainInd!fsubGrtOr
Combobox
This means that you can create a variable of type Combobox to hold a pointer to that particular control. Some "family" objects exist, for example the type Control, that can point to any control, not just comboboxes.
If you just want to avoid typing, you can use the "implicit variable" of the With contruct, as in:
With Forms!frmMainInd!fsubGrtOr
!cboWhatever.Value = Null
.BackColor = vbRed
!!fsubGrtApp.Form.RecordSe
End With
Every object starting with . or ! will be taken from the innermost With construct.
Does that help any?
(°v°)
If you want to take this a step further, forms with code modules have their own datatype. This allows you to see the form's controls (using the auto entry feature) and public functions (if any) in the code view.
Dim M As Form_frmMainInd
Dim O As Form_fsubGrtOrg
Dim A As Form_fsubGrtApp
Set M = Forms!frmMainInd
Set O = M.fsubGrtOrg.Form
Set A = M.fsubGrtApp.Form
M.cboWhatever.Value = Null ' on main form ' Note the absence of the bang notation.
O.cboWhatever.Value = Null ' on sub form
A.cboWhatever.Value = Null ' on the other sub form
Dim M As Form_frmMainInd
Dim O As Form_fsubGrtOrg
Dim A As Form_fsubGrtApp
Set M = Forms!frmMainInd
Set O = M.fsubGrtOrg.Form
Set A = M.fsubGrtApp.Form
M.cboWhatever.Value = Null ' on main form ' Note the absence of the bang notation.
O.cboWhatever.Value = Null ' on sub form
A.cboWhatever.Value = Null ' on the other sub form
ASKER
harfang,
thanks this is exactly what I was looking for. one question: how come the double bang in
!!fsubGrtApp.Form.RecordSe t.MoveLast
thanks this is exactly what I was looking for. one question: how come the double bang in
!!fsubGrtApp.Form.RecordSe
You were looking for a With block?
OK.
The double bang just means that even EE Access Geniuses make typos ;-)
OK.
The double bang just means that even EE Access Geniuses make typos ;-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Set frmWhatever = Forms!frmMainInd
Debug.print frmWhatever!cboWhatever
Dim ctlWhatever as Control
Set ctlWhatever = frmWhatever!cboWhatever
'etc