Graying out control not working

G

Guest

Hello,

I have some code that is based on the user's choice from a combo box field
('sexcode'), whose values are derived from a Value List
("f";"female";"m";"male"). I have code to gray out the next control
('Frame9surg_sterl') if the user chooses "male", but to keep it Enabled if
"female" is chosen. What happens is that 'Frame9surg_sterl' is grayed out
regardless of what the user chooses. I have this working elsewhere in my
database, but this is the first time I'm attempting to do this using a combo
box populated by an alphanumeric value from a Value List instead of a number.
What is my code (below) missing here?

Private Sub sexcode_AfterUpdate()
Dim sexcode As String
If sexcode = f Then
Me!Frame9surg_sterl.Enabled = True
Else
Me!Frame9surg_sterl.Enabled = False
End If
End Sub
 
S

Sandra Daigle

Try comparing to the quoted "f" instead of f . If you do not have "Option
Explicit" in the module header your code is comparing the combo to the
undeclared variable f. Make sure that you have "Option Explicit" in the
header of all of your modules to prevent undeclared variables from being
used.

To simplify this code you can instead use:

Me!Frame9surg_sterl.Enabled =me.sexcode="f"

Be sure you also include this code on the current event of the form if you
want the control to be toggled when you navigate to existing records.
 
J

John Vinson

Hello,

I have some code that is based on the user's choice from a combo box field
('sexcode'), whose values are derived from a Value List
("f";"female";"m";"male"). I have code to gray out the next control
('Frame9surg_sterl') if the user chooses "male", but to keep it Enabled if
"female" is chosen. What happens is that 'Frame9surg_sterl' is grayed out
regardless of what the user chooses. I have this working elsewhere in my
database, but this is the first time I'm attempting to do this using a combo
box populated by an alphanumeric value from a Value List instead of a number.
What is my code (below) missing here?

Private Sub sexcode_AfterUpdate()
Dim sexcode As String
If sexcode = f Then
Me!Frame9surg_sterl.Enabled = True
Else
Me!Frame9surg_sterl.Enabled = False
End If
End Sub

I think if you type

Option Explicit

before the very first Sub line in this form's module (as you should do
routinely), you'll find that VBA sees f as an undefined Variant
variable. Since this variable has an undefined value, it won't match
anything.

If you want to compare sexcode to the text string "f", you must
enclose it in quotemarks:

If Sexcode = "f" Then


John W. Vinson[MVP]
 
G

Guest

Hi Sandra,

Thanks for the tip-- it works great. A quick question:

I literally used just this line of code:
Me!Frame9surg_sterl.Enabled =me.sexcode="f"

Is it Best Practice to declare a variable, and, if so, would it be declared
as a String? Or is it really unnecessary if I've put 'Option Explicit' for
this Event Procedure (in the "After Update' Event)?? This code isn't part of
a Module (as in the 'Module' section of the database), per se. This code
appears w/ the other Event Procedure code tied to this particular form. Hope
I'm not just stuck on semantics, but I'm a bit of a code novice.

Sandra Daigle said:
Try comparing to the quoted "f" instead of f . If you do not have "Option
Explicit" in the module header your code is comparing the combo to the
undeclared variable f. Make sure that you have "Option Explicit" in the
header of all of your modules to prevent undeclared variables from being
used.

To simplify this code you can instead use:

Me!Frame9surg_sterl.Enabled =me.sexcode="f"

Be sure you also include this code on the current event of the form if you
want the control to be toggled when you navigate to existing records.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Pat said:
Hello,

I have some code that is based on the user's choice from a combo box field
('sexcode'), whose values are derived from a Value List
("f";"female";"m";"male"). I have code to gray out the next control
('Frame9surg_sterl') if the user chooses "male", but to keep it Enabled if
"female" is chosen. What happens is that 'Frame9surg_sterl' is grayed out
regardless of what the user chooses. I have this working elsewhere in my
database, but this is the first time I'm attempting to do this using a
combo box populated by an alphanumeric value from a Value List instead of
a number. What is my code (below) missing here?

Private Sub sexcode_AfterUpdate()
Dim sexcode As String
If sexcode = f Then
Me!Frame9surg_sterl.Enabled = True
Else
Me!Frame9surg_sterl.Enabled = False
End If
End Sub
 
S

Sandra Daigle

Hi Pat,

All code is in a module - your code just happens to be in a special type of
module called the form's class module. Regardless, Option Explicit should be
in the header of every module. This tells the compiler to require that all
variables are declared - this makes it very hard for you to accidentally
make a comparision to a non-existant variable (f in your case) rather than a
literal string ("f" which is what you intended).

You can set an option in the VB Editor to add this line for your
automatically - open the editor and click Tools->Options and look under the
Editor tab.

In this code you don't need a variable - just compare directly to the string
"f".

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Pat said:
Hi Sandra,

Thanks for the tip-- it works great. A quick question:

I literally used just this line of code:
Me!Frame9surg_sterl.Enabled =me.sexcode="f"

Is it Best Practice to declare a variable, and, if so, would it be
declared as a String? Or is it really unnecessary if I've put 'Option
Explicit' for this Event Procedure (in the "After Update' Event)?? This
code isn't part of a Module (as in the 'Module' section of the database),
per se. This code appears w/ the other Event Procedure code tied to this
particular form. Hope I'm not just stuck on semantics, but I'm a bit of
a code novice.

Sandra Daigle said:
Try comparing to the quoted "f" instead of f . If you do not have "Option
Explicit" in the module header your code is comparing the combo to the
undeclared variable f. Make sure that you have "Option Explicit" in the
header of all of your modules to prevent undeclared variables from being
used.

To simplify this code you can instead use:

Me!Frame9surg_sterl.Enabled =me.sexcode="f"

Be sure you also include this code on the current event of the form if
you want the control to be toggled when you navigate to existing records.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Pat said:
Hello,

I have some code that is based on the user's choice from a combo box
field ('sexcode'), whose values are derived from a Value List
("f";"female";"m";"male"). I have code to gray out the next control
('Frame9surg_sterl') if the user chooses "male", but to keep it Enabled
if "female" is chosen. What happens is that 'Frame9surg_sterl' is
grayed out regardless of what the user chooses. I have this working
elsewhere in my database, but this is the first time I'm attempting to
do this using a combo box populated by an alphanumeric value from a
Value List instead of a number. What is my code (below) missing here?

Private Sub sexcode_AfterUpdate()
Dim sexcode As String
If sexcode = f Then
Me!Frame9surg_sterl.Enabled = True
Else
Me!Frame9surg_sterl.Enabled = False
End If
End Sub
 
G

Guest

Thank you, Sandra. This is a great help!

Patrick

Sandra Daigle said:
Hi Pat,

All code is in a module - your code just happens to be in a special type of
module called the form's class module. Regardless, Option Explicit should be
in the header of every module. This tells the compiler to require that all
variables are declared - this makes it very hard for you to accidentally
make a comparision to a non-existant variable (f in your case) rather than a
literal string ("f" which is what you intended).

You can set an option in the VB Editor to add this line for your
automatically - open the editor and click Tools->Options and look under the
Editor tab.

In this code you don't need a variable - just compare directly to the string
"f".

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Pat said:
Hi Sandra,

Thanks for the tip-- it works great. A quick question:

I literally used just this line of code:
Me!Frame9surg_sterl.Enabled =me.sexcode="f"

Is it Best Practice to declare a variable, and, if so, would it be
declared as a String? Or is it really unnecessary if I've put 'Option
Explicit' for this Event Procedure (in the "After Update' Event)?? This
code isn't part of a Module (as in the 'Module' section of the database),
per se. This code appears w/ the other Event Procedure code tied to this
particular form. Hope I'm not just stuck on semantics, but I'm a bit of
a code novice.

Sandra Daigle said:
Try comparing to the quoted "f" instead of f . If you do not have "Option
Explicit" in the module header your code is comparing the combo to the
undeclared variable f. Make sure that you have "Option Explicit" in the
header of all of your modules to prevent undeclared variables from being
used.

To simplify this code you can instead use:

Me!Frame9surg_sterl.Enabled =me.sexcode="f"

Be sure you also include this code on the current event of the form if
you want the control to be toggled when you navigate to existing records.

--
Sandra Daigle
[Microsoft Access MVP]
For the benefit of others please post all replies to this newsgroup.

Pat Dools wrote:
Hello,

I have some code that is based on the user's choice from a combo box
field ('sexcode'), whose values are derived from a Value List
("f";"female";"m";"male"). I have code to gray out the next control
('Frame9surg_sterl') if the user chooses "male", but to keep it Enabled
if "female" is chosen. What happens is that 'Frame9surg_sterl' is
grayed out regardless of what the user chooses. I have this working
elsewhere in my database, but this is the first time I'm attempting to
do this using a combo box populated by an alphanumeric value from a
Value List instead of a number. What is my code (below) missing here?

Private Sub sexcode_AfterUpdate()
Dim sexcode As String
If sexcode = f Then
Me!Frame9surg_sterl.Enabled = True
Else
Me!Frame9surg_sterl.Enabled = False
End If
End Sub
 

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