variables

  • Thread starter kary via AccessMonster.com
  • Start date
K

kary via AccessMonster.com

Hi
I have assigned a variable called StPracLock as follows:
StPracLock = "Forms!Results1.Mod" & stMod & "Ass" & StAssNo & "Prac.Locked =
False"
Now I want to run the command by typing in the variable name but, ofcourse,
it doesn't work! (Comes up with "COmpile Error, Sub,Function,Procedure
expected) Can anyone help me. I have a number of fields to lock/unlock
depending on the Module (stMod) and Assignment (StAssNo) selected.
Thank you
Kary
 
B

BruceM via AccessMonster.com

Why "of course", unless you mean that if it worked you would not be posting
the question? In what way does it not work (error message, incorrect string,
computer explodes)?

Where are you typing the variable names? How are those entries assigned to
the variables? Are the variables declared anywhere? What type of variables
are they (string, variant, long, or what exactly)? I assume they are strings,
but I can't be sure. Please post a sample of how the string would look with
the appropriate values inserted.
 
K

kary via AccessMonster.com

Thanks BruceM for the quick reply!
I am declaring the variables as a string at the beginning of the procedure.

I want to unlock/enable/make visible various controls on a form using the
commands:
Forms!Results1.Mod1Ass2Prac.Locked = False
Forms!Results1.Mod1Ass2Prac.Enabled = True
Forms!Results1.Mod1Ass2Prac.Visible = True

The number after "Mod" and "Ass" will change depending on the
module/assignment selected on the form. When I press OK on the form, the
variables StMod and StAss will be set depending on the module/assignment
selected.

Then I call a module which will contain this code (this is to cut down on the
amount code). However, I don't know how to then "execute" the variable so
that it runs the code.
Hope this makes sense! Kary

Why "of course", unless you mean that if it worked you would not be posting
the question? In what way does it not work (error message, incorrect string,
computer explodes)?

Where are you typing the variable names? How are those entries assigned to
the variables? Are the variables declared anywhere? What type of variables
are they (string, variant, long, or what exactly)? I assume they are strings,
but I can't be sure. Please post a sample of how the string would look with
the appropriate values inserted.
Hi
I have assigned a variable called StPracLock as follows:
[quoted text clipped - 6 lines]
Thank you
Kary
 
B

BruceM via AccessMonster.com

First, I don't think you can use a string in that way to lock, enable, etc.
controls. You can assemble the control name:

Dim stPracLock as String, stMod as String, stAssNo as String

stMod = ?
stAssNo = ??
stPracLock = "Mod" & stMod & "Ass" & StAssNo & "Prac"

Then:

Me.Controls(stPracLock).Enabled = False

For several properties you could do:

With Me.Controls(stPracLock)
.Enabled = False
.Locked = True
.Visible = True
End With

If this is in a module other than the form's code module:

Dim frm As Form
Dim stPracLock as String, stMod as String, stAssNo as String

stMod = ?
stAssNo = ??
stPracLock = "Mod" & stMod & "Ass" & StAssNo & "Prac"

With frm.Controls(stPracLock)
.Enabled = False
.Locked = True
.Visible = True
End With

However, since you are already hard-coding the from name it seems you are
performing all of these actions for a single form, so there is no need for
code anwhere other than the form's code module. The method that uses frm is
if you are applying the code to several forms, and need the code to be in a
standalone code module.

You are not calling a module, which is the entire body of code associated
with a form or in a standalone module, but rather calling a sub or function.

This brings us back to the question of where the user inputs the values to be
used. Only you can answer that question. Where do stMod and stAssNo get
their values? This is not asking where they are declared, but rather how
they receive the values 1 and 2.

I do not know what you mean by "execute" the variable. I could take a few
guesses, but it would be better if you describe exactly what is to happen.
Thanks BruceM for the quick reply!
I am declaring the variables as a string at the beginning of the procedure.

I want to unlock/enable/make visible various controls on a form using the
commands:
Forms!Results1.Mod1Ass2Prac.Locked = False
Forms!Results1.Mod1Ass2Prac.Enabled = True
Forms!Results1.Mod1Ass2Prac.Visible = True

The number after "Mod" and "Ass" will change depending on the
module/assignment selected on the form. When I press OK on the form, the
variables StMod and StAss will be set depending on the module/assignment
selected.

Then I call a module which will contain this code (this is to cut down on the
amount code). However, I don't know how to then "execute" the variable so
that it runs the code.
Hope this makes sense! Kary
Why "of course", unless you mean that if it worked you would not be posting
the question? In what way does it not work (error message, incorrect string,
[quoted text clipped - 11 lines]
 
K

kary via AccessMonster.com

Hi Bruce,
I need a bit of time to try this but I think you've solved my query. I see
now that I need to split the control name which I'm storing in the variable
(stPracLock ="Mod" & stMod & "Ass" & StAssNo & "Prac") from the control value
(.Enabled = False). Many thanks for your time - I really appreciate it. Kary
First, I don't think you can use a string in that way to lock, enable, etc.
controls. You can assemble the control name:

Dim stPracLock as String, stMod as String, stAssNo as String

stMod = ?
stAssNo = ??
stPracLock = "Mod" & stMod & "Ass" & StAssNo & "Prac"

Then:

Me.Controls(stPracLock).Enabled = False

For several properties you could do:

With Me.Controls(stPracLock)
.Enabled = False
.Locked = True
.Visible = True
End With

If this is in a module other than the form's code module:

Dim frm As Form
Dim stPracLock as String, stMod as String, stAssNo as String

stMod = ?
stAssNo = ??
stPracLock = "Mod" & stMod & "Ass" & StAssNo & "Prac"

With frm.Controls(stPracLock)
.Enabled = False
.Locked = True
.Visible = True
End With

However, since you are already hard-coding the from name it seems you are
performing all of these actions for a single form, so there is no need for
code anwhere other than the form's code module. The method that uses frm is
if you are applying the code to several forms, and need the code to be in a
standalone code module.

You are not calling a module, which is the entire body of code associated
with a form or in a standalone module, but rather calling a sub or function.

This brings us back to the question of where the user inputs the values to be
used. Only you can answer that question. Where do stMod and stAssNo get
their values? This is not asking where they are declared, but rather how
they receive the values 1 and 2.

I do not know what you mean by "execute" the variable. I could take a few
guesses, but it would be better if you describe exactly what is to happen.
Thanks BruceM for the quick reply!
I am declaring the variables as a string at the beginning of the procedure.
[quoted text clipped - 20 lines]
 
B

BruceM via AccessMonster.com

As a point of terminology, Enabled, Visible, Locked, and the like are
properties, not values.

Note that if all of this is in a single form you can use the Me. prefix as I
showed rather than the fully qualified name (Forms!...). Note too that you
need to use syntax along the lines of:

Me.Controls("ControlName").Enabled = False
or
Me.Controls(stPracLock).Enabled = False

to use a string value for the control name. You can't treat the whole
expression as a string.

Hi Bruce,
I need a bit of time to try this but I think you've solved my query. I see
now that I need to split the control name which I'm storing in the variable
(stPracLock ="Mod" & stMod & "Ass" & StAssNo & "Prac") from the control value
(.Enabled = False). Many thanks for your time - I really appreciate it. Kary
First, I don't think you can use a string in that way to lock, enable, etc.
controls. You can assemble the control name:
[quoted text clipped - 54 lines]
 
K

kary via AccessMonster.com

Hi Bruce
Thanks, the right terminology certainly helps! I have now got the following:

1) A Select Case statement to set the value of stMod and stAssNo
2) A Call statement to call a sub (Call ShowControls(stMod, stAssNo))
3) In ShowControls:
Public Sub ShowControls(stMod, stAssNo)

Dim stPrac As String
Dim stAcad As String

stPrac = "Forms!Results1.Mod" & stMod & "Ass" & stAssNo & "Prac"
stAcad = "Forms!Results1.Mod" & stMod & "Ass" & stAssNo & "Acad"

'Forms!Results1!Mod1Ass1Acad.Visible = True '<-- (a)

With Me.Controls(stPrac)
.Locked = False
.Visible = True
End With

With Me.Controls(stAcad)
.Locked = False
.Visible = True
End With

End Sub

Now when I run it, it goes through the select statement, passes the variables
to the ShowControls sub and then gives the error message: "Office can't find
the field 'Forms!Results1!Mod1Ass1Prac' referred to in your expression."
However, the code which I've marked with (a) above which is currently
commented out does work.
Thank you for your help, Kary
As a point of terminology, Enabled, Visible, Locked, and the like are
properties, not values.

Note that if all of this is in a single form you can use the Me. prefix as I
showed rather than the fully qualified name (Forms!...). Note too that you
need to use syntax along the lines of:

Me.Controls("ControlName").Enabled = False
or
Me.Controls(stPracLock).Enabled = False

to use a string value for the control name. You can't treat the whole
expression as a string.
Hi Bruce,
I need a bit of time to try this but I think you've solved my query. I see
[quoted text clipped - 7 lines]
 
B

BruceM via AccessMonster.com

Me.Controls("ControlName") uses the name of the control only. "Forms!
Results1" is extraneous.

stPrac = "Mod" & stMod & "Ass" & stAssNo & "Prac"
stAcad = "Mod" & stMod & "Ass" & stAssNo & "Acad"

The rest of the code looks OK.

If you need to see what a string (or any variable) looks like after it is
assmbled, add this line of code after you have set the variable (in this case,
directly after the stAcad line of code):
Debug.Print stPrac
Debug.Print stAcac

After you run the code, press Ctrl + G to open the immediate code window.
The strings will be shown there.

It's best to remove or comment out the Debug lines of code after you are done
testing (before you start using the database for real).
Hi Bruce
Thanks, the right terminology certainly helps! I have now got the following:

1) A Select Case statement to set the value of stMod and stAssNo
2) A Call statement to call a sub (Call ShowControls(stMod, stAssNo))
3) In ShowControls:
Public Sub ShowControls(stMod, stAssNo)

Dim stPrac As String
Dim stAcad As String

stPrac = "Forms!Results1.Mod" & stMod & "Ass" & stAssNo & "Prac"
stAcad = "Forms!Results1.Mod" & stMod & "Ass" & stAssNo & "Acad"

'Forms!Results1!Mod1Ass1Acad.Visible = True '<-- (a)

With Me.Controls(stPrac)
.Locked = False
.Visible = True
End With

With Me.Controls(stAcad)
.Locked = False
.Visible = True
End With

End Sub

Now when I run it, it goes through the select statement, passes the variables
to the ShowControls sub and then gives the error message: "Office can't find
the field 'Forms!Results1!Mod1Ass1Prac' referred to in your expression."
However, the code which I've marked with (a) above which is currently
commented out does work.
Thank you for your help, Kary
As a point of terminology, Enabled, Visible, Locked, and the like are
properties, not values.
[quoted text clipped - 15 lines]
 
K

kary via AccessMonster.com

Yes! You did it. Thank you SO much - you're a star!
Kary
Me.Controls("ControlName") uses the name of the control only. "Forms!
Results1" is extraneous.

stPrac = "Mod" & stMod & "Ass" & stAssNo & "Prac"
stAcad = "Mod" & stMod & "Ass" & stAssNo & "Acad"

The rest of the code looks OK.

If you need to see what a string (or any variable) looks like after it is
assmbled, add this line of code after you have set the variable (in this case,
directly after the stAcad line of code):
Debug.Print stPrac
Debug.Print stAcac

After you run the code, press Ctrl + G to open the immediate code window.
The strings will be shown there.

It's best to remove or comment out the Debug lines of code after you are done
testing (before you start using the database for real).
Hi Bruce
Thanks, the right terminology certainly helps! I have now got the following:
[quoted text clipped - 36 lines]
 

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