Hiding enabling disabling controls on a from from VB

I

Isis

If I have a Control on a form displaying a Table Field called [INVOICE] -
how do I hide|Unhide|Enable|Disable this control using VB ?

I have trying various things like;

Me.Control("INVOICE").Visible = False to hide


When is it right to use the "Me.Control" statement - for instance how do I
give a control Focus in Code ?

Any help appreciated as always.

Thanks
 
M

Mike Revis

Isis,
The Me. indicates your are referring to something within the scope of the
form your are currently working on.

If you type Me.
You should get a list of everything you can use it with.

You have to use the name of the control.
If the name of your control is Invoice.
Me.Invoice.visible=False will hide the control named Invoice on that form.
Me.Invoice.SetFocus will set the focus to the control named invoice on that
form.

Mike
 
I

Isis

Isis,
The Me. indicates your are referring to something within the scope of
the form your are currently working on.

If you type Me.
You should get a list of everything you can use it with.

You have to use the name of the control.
If the name of your control is Invoice.
Me.Invoice.visible=False will hide the control named Invoice on that
form. Me.Invoice.SetFocus will set the focus to the control named
invoice on that form.

Mike


Isis said:
If I have a Control on a form displaying a Table Field called
[INVOICE] - how do I hide|Unhide|Enable|Disable this control using VB
?

I have trying various things like;

Me.Control("INVOICE").Visible = False to hide


When is it right to use the "Me.Control" statement - for instance how
do I give a control Focus in Code ?

Any help appreciated as always.

Thanks

Thanks for that Mike.

Regards
 
K

Klatuu

Isis,
As to your question on when to use Me.Control. As written, your code would
produce error 2465. It should be:
Me.Controls("INVOICE").Visible = False
But, that is only if the control on the form is named INVOICE. You don't
reference the table field name in a form. You reference the name of the
control. Using bound forms, the control is bound to a table field, so you
don't need to concern yourself with the field name. Using proper naming
convention, you should name control according to it's control source and
control type. For example, if you you are creating a Text Box control with
the control source being a field in your recordset named INVOICE, the proper
name would be txtINVOICE. Now, you know two things about this object when
you see it's name in code. It is a text box and it works with the data
element INVOICE. This helps you and those read your code understand it more
easily.

Now that we understand that and know that Me. is a shorthand identifier for
the current form, we know how to refer to the control and set it's properties.

Me.txtINVOICE.Visible = False ' Make it totally unvailable and unseen
Me.txtINVOICE.Enabled = False 'Appears greyed out, can't be modified or take
focus
Me.txtINVOICE.Locked = True 'Looks normal, can take focus and be manipulated
by
code, but user can't change value

As we learned previously, we can refer to a control with
Me.Controls("ControlName"). It is the equivlant of using Me.ControlName. In
the first case, what we are working with is the Controls collection of the
Form object. This allows some real flexibility in working with our controls.
For example, one reason to use the Controls collection would be if we don't
know which control we will be using until some code makes that decision. We
can assign a variable the name of the control, then use it later. In this
example, the user is asked which data they want to modify. Once they have
made a selection, the control that contains the data they want to modify
receives the focus.

If Msgbox("Yes to Change Invoice Number or No to Change Invoice Date",
_ vbYesNo) = vbYes Then
strSelectControl = "txtINVOICE"
Else
strSelectControl = "txtInvDate"
End If

Me.Controls(strSelectControl).SetFocus

And that's today's lesson!
 
K

Ken Sheridan

To add one thing to what the others have said; if you set a control's Enabled
property to False and its Locked property to True it will appear as normal,
but the user will not be able to edit it, nor even move focus to it. This is
useful as it removes any ambiguity in the mind of the user as to what they
can do with its contents without greying out the control.

Ken Sheridan
Stafford, England
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top