How to use Form/Subform Names as variables

G

Guest

I have a module that updates "Last Updated" information when records are
changed. Because this functionality will apply to several forms and/or
subforms, I wanted to use a variable for the form/subform names and call this
procedure from a module instead of recreating the code inside each
form/subform.

What's the correct syntax to get Access to recognize the variable as the
form name? What I've got below doesn't work...


Public Static Sub CreateLastUpdated(strFormName As String)
...
Forms!strFormName!frmLast_Updated![Date].Value = Date
 
M

Marshall Barton

Anita said:
I have a module that updates "Last Updated" information when records are
changed. Because this functionality will apply to several forms and/or
subforms, I wanted to use a variable for the form/subform names and call this
procedure from a module instead of recreating the code inside each
form/subform.

What's the correct syntax to get Access to recognize the variable as the
form name? What I've got below doesn't work...


Public Static Sub CreateLastUpdated(strFormName As String)
...
Forms!strFormName!frmLast_Updated![Date].Value = Date


It's the same syntax you can use to refer to an element in
any collection.

Your code looks like its also referring to a subform control
named frmLast_Updated. If that's what you're doing, then
you should use its Form property to get to the date
field/control. Note that the use of a reserved name as the
name of anything you create is a very poor practice that is
likely to cause you some trouble. Most folks add a three
character prefix that indicates the type of the control
(e.g. txtDateSold) with no prefix for the DateSold field in
a table.

I think your code should probably be:

Forms(strFormName)!frmLast_Updated.Form![Date] = Date
 
G

Guest

Marshall, thanks a million for your help - worked like a charm.

And thanks for the note about not using "Date" as my field name. As you can
probably tell, I'm very new to programming!

Marshall Barton said:
Anita said:
I have a module that updates "Last Updated" information when records are
changed. Because this functionality will apply to several forms and/or
subforms, I wanted to use a variable for the form/subform names and call this
procedure from a module instead of recreating the code inside each
form/subform.

What's the correct syntax to get Access to recognize the variable as the
form name? What I've got below doesn't work...


Public Static Sub CreateLastUpdated(strFormName As String)
...
Forms!strFormName!frmLast_Updated![Date].Value = Date


It's the same syntax you can use to refer to an element in
any collection.

Your code looks like its also referring to a subform control
named frmLast_Updated. If that's what you're doing, then
you should use its Form property to get to the date
field/control. Note that the use of a reserved name as the
name of anything you create is a very poor practice that is
likely to cause you some trouble. Most folks add a three
character prefix that indicates the type of the control
(e.g. txtDateSold) with no prefix for the DateSold field in
a table.

I think your code should probably be:

Forms(strFormName)!frmLast_Updated.Form![Date] = Date
 
M

Marshall Barton

It's not just the word Date. There are an innumerable
number of words that are reserved (and no definitive list of
them), so a fairly safe practice is to never use a real word
for anything. Use names that are more descriptive of the
field's purpose (e.g. CustomerName instead of of just Name
or Customer).
--
Marsh
MVP [MS Access]

Marshall, thanks a million for your help - worked like a charm.

And thanks for the note about not using "Date" as my field name. As you can
probably tell, I'm very new to programming!

Marshall Barton said:
Anita said:
I have a module that updates "Last Updated" information when records are
changed. Because this functionality will apply to several forms and/or
subforms, I wanted to use a variable for the form/subform names and call this
procedure from a module instead of recreating the code inside each
form/subform.

What's the correct syntax to get Access to recognize the variable as the
form name? What I've got below doesn't work...


Public Static Sub CreateLastUpdated(strFormName As String)
...
Forms!strFormName!frmLast_Updated![Date].Value = Date


It's the same syntax you can use to refer to an element in
any collection.

Your code looks like its also referring to a subform control
named frmLast_Updated. If that's what you're doing, then
you should use its Form property to get to the date
field/control. Note that the use of a reserved name as the
name of anything you create is a very poor practice that is
likely to cause you some trouble. Most folks add a three
character prefix that indicates the type of the control
(e.g. txtDateSold) with no prefix for the DateSold field in
a table.

I think your code should probably be:

Forms(strFormName)!frmLast_Updated.Form![Date] = Date
 

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