access97 How do I set the value of a datasheet field with VB code

M

Marvin

I have a form with a subform displayed as a datasheet.
With code:
When a new record is created I need to add a "session value" to one of the
fields it the new record before it is saved.

I cannot find the proper object reffrece & syntax to do this like I can in a
form
"txtSessionValue.text = strThisSessionValue"

Note: The field I need to write to is number 10 of 10 fields.

Thanks for you help...
 
V

Van T. Dinh

If the code is executed in the context of the main Form, you need to use:

Me.SubformCONTROL.Form.txtSessionValue = strThisSessionValue

You need to check the name of the SubformCONTROL in the design view of the
main Form. The SubformCONTROL name may be different from the name of the
Form being used as the Subform (more technically accurate, being used as the
SourceObject of the SubformControl).
 
M

Marvin

I am attempting to use the subform's 'Form_BeforeInsert' event to trigger code
The BeforeInsert event appears to be a safe place to set values, in a new
record, without triggering other events…

Fact Pattern:
Who makes each call needs to be included in each record added to the table.
The application requires the user to enter their ID at strart up. This ID is
stored in a variable Dim ensioned in a module named 'GlobalMod' as

'Public strCallerName As String' .

The Parent form's name is 'CallList'
The SubFormControl name is 'CallsSubform'
The Source Object is ' CallsSubform'
The Subform's name is CallsSubForm


QUESTIONS:
1) Will code in a Form work the same when it is running as a SubForm?
2) What is the syntax when I want to set the value of the Table.Field
named 'CalledBy', which is attached to a textBox named 'txtCalledBy' in the
subform which is in the DataSheet mode?
i.e. : If the subForm was NOT a subform I could say:

TxtCalledBy.Text = strCallerName

to insert the users ID into the tenth field of the datasheet. (with 1 being
the first field in the table with a total of 10 fields)

Thanks again for you help…
Marvin
 
V

Van T. Dinh

1. Ans. to Q1: It depends on how you refer to Controls on the Form. For
example, if you have a TextBox1 on the *Form* "CallsSubform" and in the code
of this Form, you refer to this TextBox1 as

Forms!CallsSubform!TextBox1

then this won't work when this Form is used as the SourceObject of a
SubformControl on another Form.

2. Ans. to Q2: I already gave you the syntax in my first reply. In Access,
we rarely use the "Text" Property and we use the "Value" Property instead.
In general, you cannot refer to the Text Property unless the Control (whose
Text you want) has the Focus. In addition "Value" is the Default Property
of a lot of Controls so you don't even need to use the ".Value". For
example, if you use:

Form!MainForm!SubformCONTROL.Form.txtSessionValue = ...

, the referece actually refers to the Value (default Property) of the
TextBox txtSessionValue on the Form being used as the SourceObject of the
SubformControl on the MainForm.
 
M

Marvin

THANKS for your help Vin!

Van T. Dinh said:
1. Ans. to Q1: It depends on how you refer to Controls on the Form. For
example, if you have a TextBox1 on the *Form* "CallsSubform" and in the code
of this Form, you refer to this TextBox1 as

Forms!CallsSubform!TextBox1

then this won't work when this Form is used as the SourceObject of a
SubformControl on another Form.

2. Ans. to Q2: I already gave you the syntax in my first reply. In Access,
we rarely use the "Text" Property and we use the "Value" Property instead.
In general, you cannot refer to the Text Property unless the Control (whose
Text you want) has the Focus. In addition "Value" is the Default Property
of a lot of Controls so you don't even need to use the ".Value". For
example, if you use:

Form!MainForm!SubformCONTROL.Form.txtSessionValue = ...

, the referece actually refers to the Value (default Property) of the
TextBox txtSessionValue on the Form being used as the SourceObject of the
SubformControl on the MainForm.
 
Top