Referencing a Field in the RecordSource but not on the activeform

  • Thread starter Thread starter L.A. Lawyer
  • Start date Start date
L

L.A. Lawyer

I want to reference a field within a form's recordsource but which is not on
the form itself. In the past I have been placing invisible fields on the
screen but that has become too clumsy, too time-consuming and too annoying.
I just want to be able to programmatically update the value of those fields
for the record referenced on the form itself.

I assumed that the reference would be something like:

Screen.ActiveForm.RecordSource, etc.

but I can't get it to work.
 
Assuming the code you're executing is within the form...

Me.SomeFieldName = "whatever"
 
No, the field is NOT on the form, but rather a field in the recordsource of
the activeform.
 
L.A. Lawyer said:
I want to reference a field within a form's recordsource but which is not on
the form itself. In the past I have been placing invisible fields on the
screen but that has become too clumsy, too time-consuming and too annoying.
I just want to be able to programmatically update the value of those fields
for the record referenced on the form itself.

I assumed that the reference would be something like:

Screen.ActiveForm.RecordSource, etc.

but I can't get it to work.


You can refer to a field in a form's record source using the
same syntax as you would refer to a control on the form (as
long as there is no control with the same name as the
field). Because of some subtle issues, I prefer to use the
! instead of dot syntax.

From some unknown procedure that is unaware of the specific
form:
Screen.ActiveForm!thefield

or from within the form's own module:
Me!thefield

(Note that this will not work in a report)
 
Whether or not there is a CONTROL on the form or not...if the field is in
the recordsource, you can do exactly what I said.
 
The only time a subtle issue gets involved is when people don't give their
controls proper names! First thing I do after laying out a form or report
is rename all the controls properly....takes 2 seconds with the tool on site
below. :o)
 
Paul said:
The only time a subtle issue gets involved is when people don't give their
controls proper names! First thing I do after laying out a form or report
is rename all the controls properly....takes 2 seconds with the tool on site
below. :o)


While a good naming sequence is definitely a best practice,
that's not the issue I was referring to.

The subtle issue I avoid is that the dot syntax burps at
compile time when the record source does not include the
field (a good thing if it's supposed to be there). However,
if you are not going to set the record source at design time
(set it at runtime), then the ! syntax will let your code
compile.

Actually, I think there's even more to it than that, but I
don't remember the details.
 
Ah. Personally, I like that it doesn't compile if you're referring to a
field that doesn't exist....makes sense...saves you from run-time errors.
I've never used Bang...except in queries. Never had a problem....but then I
always name controls properly.
 
Back
Top