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!