Reading values from a form with a Global Function

I

Isis

I am trying to move a local function to a Global Module so that I can use
it from different Forms. I have been trying to find the Syntax for
retrieving values using both the "Me.Controls(strFieldName).SetFocus"
method and the DLookup Function. Using the SetFocus method I had been
using the following code;

(assign value to strFieldName).....
Me.Controls(strFieldName).SetFocus
strValue = Me.Controls(strFieldName).Text

Used fro the Global Function this gives and error about using "Me."

Using the DLookup Method I would use something like this;

(assign value to strFieldName).....
strValue = DLookup([strFieldName], "Colours", "[Type] = CURRENT VALUE OF
TYPE CONTROL")

The Me.Controls method looks better to me as it can be generic for any
form I have open but it does not seem to like it - does that mean I
cannot use it ?

I can't get the syntax of the DLookup call right to use the values on the
current record open in the Form.

Any help appreciated.

Thanks
 
J

Jeff C

Isis said:
I am trying to move a local function to a Global Module so that I can use
it from different Forms. I have been trying to find the Syntax for
retrieving values using both the "Me.Controls(strFieldName).SetFocus"
method and the DLookup Function. Using the SetFocus method I had been
using the following code;

(assign value to strFieldName).....
Me.Controls(strFieldName).SetFocus
strValue = Me.Controls(strFieldName).Text

Used fro the Global Function this gives and error about using "Me."

Using the DLookup Method I would use something like this;

(assign value to strFieldName).....
strValue = DLookup([strFieldName], "Colours", "[Type] = CURRENT VALUE OF
TYPE CONTROL")

The Me.Controls method looks better to me as it can be generic for any
form I have open but it does not seem to like it - does that mean I
cannot use it ?

I can't get the syntax of the DLookup call right to use the values on the
current record open in the Form.

Any help appreciated.

Thanks

A global module requires the complete syntax for a control, as it can be
used from any form, Me. is not the same for each form. For variables,
use Public strValue As String at the top of the module below the 'Option
Explicit' line.

Me.Controls(strFieldName).SetFoc
Forms![formname]![strFieldName].SetFocus

Hope this helps,

Jeff C
 
D

Douglas J Steele

Not quite.

To use a variable form name, you need

Forms(strFormName).Controls(strFieldName)

As well, there's no actual reason to set focus to the control first.
Controls have two separate ways of getting the value in them: the Text
property and the Value property. While the control has the focus, the Text
property contains the text data currently in the control; the Value property
contains the last saved data for the control. When you move the focus to
another control, the control's data is updated, and the Value property is
set to this new value. The Text property setting is then unavailable until
the control gets the focus again. That means that you can get the value as:

Forms(strFormName).Controls(strFieldName).Value

or, since Value is the default property, you can get away with

Forms(strFormName).Controls(strFieldName)

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Jeff C said:
A global module requires the complete syntax for a control, as it can be
used from any form, Me. is not the same for each form. For variables,
use Public strValue As String at the top of the module below the 'Option
Explicit' line.

Me.Controls(strFieldName).SetFoc
Forms![formname]![strFieldName].SetFocus
I am trying to move a local function to a Global Module so that I can use
it from different Forms. I have been trying to find the Syntax for
retrieving values using both the "Me.Controls(strFieldName).SetFocus"
method and the DLookup Function. Using the SetFocus method I had been
using the following code;

(assign value to strFieldName).....
Me.Controls(strFieldName).SetFocus
strValue = Me.Controls(strFieldName).Text

Used fro the Global Function this gives and error about using "Me."

Using the DLookup Method I would use something like this;

(assign value to strFieldName).....
strValue = DLookup([strFieldName], "Colours", "[Type] = CURRENT VALUE OF
TYPE CONTROL")

The Me.Controls method looks better to me as it can be generic for any
form I have open but it does not seem to like it - does that mean I
cannot use it ?

I can't get the syntax of the DLookup call right to use the values on the
current record open in the Form.
 

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