how to address form fields using string array of field names?

W

WDSnews

I'm struggling with the Access Object Model. How can I address the fields
of my form when the field names are held in an array? My hope is to address
the fields similar to:

Me.Fields(strArray(iCount)).BackColor = RGB(&HF8, &HFF, &HA0)

This works:
Me.[Title].BackColor = RGB(&HFF, &HFF, &HFF)

This doesn't work:
Me.Fields("Title").BackColor = RGB(&HF8, &HFF, &HA0)
 
G

Graham R Seach MVP

A field isn't the same as a control.

The 'Me' keyword refers to the form, and forms don't have fields. Only the
form's Recordset has fields. When you use Me.[Title].BackColor, you're
referring to a form control called "Field". When you use Me.Fields("Title"),
you're attempting to refer to a field (called "Title") in the form's Fields
collection ... which doesn't exist.

Clearly what you're trying to do is change the BackColor of a form's
'controls', not its 'fields'. If I assume that your array actually contains
the names of controls (textboxes, combos and so on, rather than the names of
table fields), then the following construct should work.

Me(strArray(iCount)).BackColor = RGB(&HF8, &HFF, &HA0)

Regards,
Graham R seach
Microsoft Access MVP
Sydney, Australia
 
R

Rob Parker

A form does not have fields; it has controls. A field does not have a
BackColor property; a control does. If you've set up your form by dragging
fields onto it (or perhaps by other ways), the controls will have the same
name as the field to which they are bound (and you can, if you so desire,
change this; many developer do so routinely, and use a naming convention to
identify the type of control).

You can refer to the controls by their name, so this will work (assuming
that the control bound to the [Title] field is also named "Title"):
Me.Controls("Title").BackColor = RGB(&HF8, &HFF, &HA0)

HTH,

Rob
 
W

WDSnews

Me.Controls("Title").BackColor = RGB(&HF8, &HFF, &HA0)

that worked. thank you!



Rob Parker said:
A form does not have fields; it has controls. A field does not have a
BackColor property; a control does. If you've set up your form by dragging
fields onto it (or perhaps by other ways), the controls will have the same
name as the field to which they are bound (and you can, if you so desire,
change this; many developer do so routinely, and use a naming convention to
identify the type of control).

You can refer to the controls by their name, so this will work (assuming
that the control bound to the [Title] field is also named "Title"):
Me.Controls("Title").BackColor = RGB(&HF8, &HFF, &HA0)

HTH,

Rob

I'm struggling with the Access Object Model. How can I address the
fields of my form when the field names are held in an array? My hope
is to address the fields similar to:

Me.Fields(strArray(iCount)).BackColor = RGB(&HF8, &HFF, &HA0)

This works:
Me.[Title].BackColor = RGB(&HFF, &HFF, &HFF)

This doesn't work:
Me.Fields("Title").BackColor = RGB(&HF8, &HFF, &HA0)
 
G

Graham R Seach MVP

I thought you said you had the names in an array? In any case, if you're
going to reference the control name directly, then although using the
Controls collection is marginally faster, you don't need the Controls
keyword at all.
Me("Title").BackColor = RGB(&HF8, &HFF, &HA0)
...or just...
Me!Title.BackColor = RGB(&HF8, &HFF, &HA0)
...or if the contol name contains spaces...
Me![Title].BackColor = RGB(&HF8, &HFF, &HA0)

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

WDSnews said:
Me.Controls("Title").BackColor = RGB(&HF8, &HFF, &HA0)

that worked. thank you!



Rob Parker said:
A form does not have fields; it has controls. A field does not have a
BackColor property; a control does. If you've set up your form by
dragging fields onto it (or perhaps by other ways), the controls will have
the same name as the field to which they are bound (and you can, if you so
desire, change this; many developer do so routinely, and use a naming
convention to identify the type of control).

You can refer to the controls by their name, so this will work (assuming
that the control bound to the [Title] field is also named "Title"):
Me.Controls("Title").BackColor = RGB(&HF8, &HFF, &HA0)

HTH,

Rob

I'm struggling with the Access Object Model. How can I address the
fields of my form when the field names are held in an array? My hope
is to address the fields similar to:

Me.Fields(strArray(iCount)).BackColor = RGB(&HF8, &HFF, &HA0)

This works:
Me.[Title].BackColor = RGB(&HFF, &HFF, &HFF)

This doesn't work:
Me.Fields("Title").BackColor = RGB(&HF8, &HFF, &HA0)
 
W

WDSnews

yes. I'm using an array. I should have said...

Me.Controls(strArray(intCount)).BackColor = RGB(&HF8, &HFF, &HA0)

....worked.

I appreciate your examples and explanations. Thank you.


Graham R Seach MVP said:
I thought you said you had the names in an array? In any case, if you're
going to reference the control name directly, then although using the
Controls collection is marginally faster, you don't need the Controls
keyword at all.
Me("Title").BackColor = RGB(&HF8, &HFF, &HA0)
...or just...
Me!Title.BackColor = RGB(&HF8, &HFF, &HA0)
...or if the contol name contains spaces...
Me![Title].BackColor = RGB(&HF8, &HFF, &HA0)

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

WDSnews said:
Me.Controls("Title").BackColor = RGB(&HF8, &HFF, &HA0)

that worked. thank you!



Rob Parker said:
A form does not have fields; it has controls. A field does not have a
BackColor property; a control does. If you've set up your form by
dragging fields onto it (or perhaps by other ways), the controls will
have the same name as the field to which they are bound (and you can, if
you so desire, change this; many developer do so routinely, and use a
naming convention to identify the type of control).

You can refer to the controls by their name, so this will work (assuming
that the control bound to the [Title] field is also named "Title"):
Me.Controls("Title").BackColor = RGB(&HF8, &HFF, &HA0)

HTH,

Rob


WDSnews wrote:
I'm struggling with the Access Object Model. How can I address the
fields of my form when the field names are held in an array? My hope
is to address the fields similar to:

Me.Fields(strArray(iCount)).BackColor = RGB(&HF8, &HFF, &HA0)

This works:
Me.[Title].BackColor = RGB(&HFF, &HFF, &HFF)

This doesn't work:
Me.Fields("Title").BackColor = RGB(&HF8, &HFF, &HA0)
 

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