Variables as identifiers in forms

K

Kirk Wilson

I want to programatically assign values to controls. I can do it
explicitly but am unable to do it by reference.

frmPreWash1 is the main form
frmDiagnostics is a tabed subform on frmPreWash1
DateOfVisit is a textbox on frmPreWash1
DateOfVisit is a control for a column in the underlaying table
tblDiagnostices

The user enters a date in the main form text box.
The Tabed subform has multiple tabs each relating to a different table.
Each subtable has a column for [DateOfVisit]
As the user enters data on each subform I want to capture the main form
textbox date for each subform control.

I can do this explicitly for each tabed subform with appropriate
identifiers using the following sample code;

Private Sub Form_Dirty(Cancel As Integer)

Forms![frmPreWash1]![frmDiagnostice].Form![DateOfVisit] =
Forms![frmPreWash1].Form![DateOfVisit]

End Sub

I created a public string variable strFormName and initialized it with
the onCurrent event of frmPreWash1 using strFormName = Form.Name

I then wrote the following code which doesn't work

Private Sub Form_Dirty(Cancel As Integer)

Dim strSubFormName As String

strSubFormName = Form.Name

Forms![strFormName]![strSubFormName].Form![DateOfVisit] =
Forms![strFormName].Form![DateOfVisit]

End Sub

I get a run time error cant find...
I set a break point and verified that each variable has the correct name.

I tried the following as an alternate. No Joy.

Forms(strFormName)(strSubFormName).Form![DateOfVisit] =
Forms(strFormName).Form![DateOfVisit]

1. How can I indirectly refer to forms, subforms & controls to acomplish
this?
2. Can I get the main form's name from the subform so as to not have to
use a public variable?

I'm pretty sure this is just a syntax issue but can't find a hint anywhere.

Thanks in advance.
 
A

Alex Dybenko

Hi!
try:
Forms![frmPreWash1]![frmDiagnostice].Form![DateOfVisit] =
Forms![frmPreWash1]![DateOfVisit]

Forms![frmPreWash1]![frmDiagnostice] returns a subform control, so we use
..Form to get reference to form inside this control. Forms![frmPreWash1]
returns reference to main form, you do not need to add .form there

--
Best regards,
___________
Alex Dybenko (MVP)
http://accessblog.net
http://www.PointLtd.com
 
K

Kirk Wilson

Hi Alex

I want to use a variable instead of the explicit form names so that I
can write one function to handle this instead of 27 explicit ones.

I appreciate your reply. I already have explicit code and it works fine.
 
J

John Spencer

If you want to use a string variable to refer to an object

Forms![strFormName].Controls(strSubFormName).Form![DateOfVisit] =
Forms![strFormName].Form![DateOfVisit]

To get the parent name of a subform use the subform's parent property.

In an event of the subform you can use
strMainForm = Me.Parent.Name

You could try something along the lines of
Me.DateOfVisit = Me.Parent!DateOfVisit

If you want to call a sub, you should be able to use something like the
following untested code.

Public Sub sSetDate(frmSubForm as Form)
frmSubForm!DateOfVisit = frmSubForm.Parent!DateOfVisit
End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
P

Paolo

Hi Kirk Wilson,
to indirectly refer to forms etc. you can try using () instead of ![]

e.g. Forms(strFormName).Form![DateOfVisit]

HTH Paolo
 
B

BruceM via AccessMonster.com

To expand a little on what John wrote, instead of this:

Forms![frmPreWash1]![frmDiagnostice].Form![DateOfVisit] =
Forms![frmPreWash1].Form![DateOfVisit]

You should be able to do this:

Me.SubformControlName.Form.DateOfVisit = Me.DateOfVisit

That said, if DateOfVisit on the subform is a field, and you are storing the
date from the main form, I have to wonder why unless the subform value may be
changed to something other than the parent form's DateOfVisit value. If on
the other hand DateOfVisit on the subform is an unbound text box, used for
convenience, it makes perfect sense.

Kirk said:
Hi Alex

I want to use a variable instead of the explicit form names so that I
can write one function to handle this instead of 27 explicit ones.

I appreciate your reply. I already have explicit code and it works fine.
[quoted text clipped - 5 lines]
Forms![frmPreWash1] returns reference to main form, you do not need to
add .form there
 
K

Kirk Wilson

Thanks for the Reply John,

Me.DateOfVisit = Me.Parent!DateOfVisit works like a champ in a form but
"Me" does not work outside of a form.

I want to replace "Me" with a variable that I can pass values to in a
procedure.

Kirk

John said:
If you want to use a string variable to refer to an object

Forms![strFormName].Controls(strSubFormName).Form![DateOfVisit] =
Forms![strFormName].Form![DateOfVisit]

To get the parent name of a subform use the subform's parent property.

In an event of the subform you can use
strMainForm = Me.Parent.Name

You could try something along the lines of
Me.DateOfVisit = Me.Parent!DateOfVisit

If you want to call a sub, you should be able to use something like the
following untested code.

Public Sub sSetDate(frmSubForm as Form)
frmSubForm!DateOfVisit = frmSubForm.Parent!DateOfVisit
End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Kirk said:
I want to programatically assign values to controls. I can do it
explicitly but am unable to do it by reference.

frmPreWash1 is the main form
frmDiagnostics is a tabed subform on frmPreWash1
DateOfVisit is a textbox on frmPreWash1
DateOfVisit is a control for a column in the underlaying table
tblDiagnostices

The user enters a date in the main form text box.
The Tabed subform has multiple tabs each relating to a different table.
Each subtable has a column for [DateOfVisit]
As the user enters data on each subform I want to capture the main
form textbox date for each subform control.

I can do this explicitly for each tabed subform with appropriate
identifiers using the following sample code;

Private Sub Form_Dirty(Cancel As Integer)

Forms![frmPreWash1]![frmDiagnostice].Form![DateOfVisit] =
Forms![frmPreWash1].Form![DateOfVisit]

End Sub

I created a public string variable strFormName and initialized it with
the onCurrent event of frmPreWash1 using strFormName = Form.Name

I then wrote the following code which doesn't work

Private Sub Form_Dirty(Cancel As Integer)

Dim strSubFormName As String

strSubFormName = Form.Name

Forms![strFormName]![strSubFormName].Form![DateOfVisit] =
Forms![strFormName].Form![DateOfVisit]

End Sub

I get a run time error cant find...
I set a break point and verified that each variable has the correct name.

I tried the following as an alternate. No Joy.

Forms(strFormName)(strSubFormName).Form![DateOfVisit] =
Forms(strFormName).Form![DateOfVisit]

1. How can I indirectly refer to forms, subforms & controls to
acomplish this?
2. Can I get the main form's name from the subform so as to not have
to use a public variable?

I'm pretty sure this is just a syntax issue but can't find a hint
anywhere.

Thanks in advance.
 
J

John Spencer

So, pass the CONTROL as a control to your procedure. You still have the
problem of identifying the control that has the source value. But you could
add additional arguments to the procedure

Public Sub sSetDate(ctl As Control)
Dim frmAny as Form
setFrmAny = ctl.Parent.Parent
ctl = SetFormAny!DateOfVisit
End Sub

In your subform you would need to call the procedure with something like:
sSetDate Me.DateOfVisit


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Kirk said:
Thanks for the Reply John,

Me.DateOfVisit = Me.Parent!DateOfVisit works like a champ in a form but
"Me" does not work outside of a form.

I want to replace "Me" with a variable that I can pass values to in a
procedure.

Kirk

John said:
If you want to use a string variable to refer to an object

Forms![strFormName].Controls(strSubFormName).Form![DateOfVisit] =
Forms![strFormName].Form![DateOfVisit]

To get the parent name of a subform use the subform's parent property.

In an event of the subform you can use
strMainForm = Me.Parent.Name

You could try something along the lines of
Me.DateOfVisit = Me.Parent!DateOfVisit

If you want to call a sub, you should be able to use something like
the following untested code.

Public Sub sSetDate(frmSubForm as Form)
frmSubForm!DateOfVisit = frmSubForm.Parent!DateOfVisit
End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County

Kirk said:
I want to programatically assign values to controls. I can do it
explicitly but am unable to do it by reference.

frmPreWash1 is the main form
frmDiagnostics is a tabed subform on frmPreWash1
DateOfVisit is a textbox on frmPreWash1
DateOfVisit is a control for a column in the underlaying table
tblDiagnostices

The user enters a date in the main form text box.
The Tabed subform has multiple tabs each relating to a different table.
Each subtable has a column for [DateOfVisit]
As the user enters data on each subform I want to capture the main
form textbox date for each subform control.

I can do this explicitly for each tabed subform with appropriate
identifiers using the following sample code;

Private Sub Form_Dirty(Cancel As Integer)

Forms![frmPreWash1]![frmDiagnostice].Form![DateOfVisit] =
Forms![frmPreWash1].Form![DateOfVisit]

End Sub

I created a public string variable strFormName and initialized it
with the onCurrent event of frmPreWash1 using strFormName = Form.Name

I then wrote the following code which doesn't work

Private Sub Form_Dirty(Cancel As Integer)

Dim strSubFormName As String

strSubFormName = Form.Name

Forms![strFormName]![strSubFormName].Form![DateOfVisit] =
Forms![strFormName].Form![DateOfVisit]

End Sub

I get a run time error cant find...
I set a break point and verified that each variable has the correct
name.

I tried the following as an alternate. No Joy.

Forms(strFormName)(strSubFormName).Form![DateOfVisit] =
Forms(strFormName).Form![DateOfVisit]

1. How can I indirectly refer to forms, subforms & controls to
acomplish this?
2. Can I get the main form's name from the subform so as to not have
to use a public variable?

I'm pretty sure this is just a syntax issue but can't find a hint
anywhere.

Thanks in advance.
 

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