how to refer to a control, which I get the name from a variable?

Y

Yue Zhao

I need to enable or disable a bunch of controls, according to the value
of other controls. for instance, when the checkbox called
Curr_Med_Atripla_Checkbox has a value -1, then I want to enable the
textbox called Curr_Med_Atripla_Length_Textbox.

So here is what I did, and of course,,, it didn't work.
It seems the way I was refering to the control name in Sub
EnableOtherQues was wrong: Access was trying to find the control called
"Me!CtrlNameSub", instead of "Me!Curr_Med_Atripla_Checkbox", even when
CtrlNameSub="Curr_Med_Atripla_Checkbox".

So, what is the right way to do that? Thanks a lot!

' ******
' my codes:
' ******

private sub form_curr()

....

Call EnableOtherQues("Atripla", -1, True, False, _
"Curr_Med_", "_Checkbox", "Curr_Med_", "_Length_Textbox")
end sub

Private Sub EnableOtherQues(VarName, Value, UnderValue, _
Optional OwValue, _
Optional PrefixSub as string, _
Optional SuffixSub as string, _
Optional PrefixObj as string, _
Optional SuffixObj as string)
' When one question is answered with a certain value,
enable/disable another question

' Generate the names of the Subject Control and Object Control
Dim CtrlNameSub As String, CtrlNameObj As String

CtrlNameSub = VarName
If Not IsNull(PrefixSub) Then CtrlNameSub = PrefixSub & CtrlNameSub
If Not IsNull(SuffixSub) Then CtrlNameSub = CtrlNameSub & SuffixSub

CtrlNameObj = VarName
If Not IsNull(PrefixObj) Then CtrlNameObj = PrefixObj & CtrlNameObj
If Not IsNull(SuffixObj) Then CtrlNameObj = CtrlNameObj & SuffixObj

' Enable and Disable
If Me!CtrlNameSub.value = Value Then
Me!CtrlNameObj.Enabled = UnderValue
Else
If Not IsNull(OwValue) Then
Me!CtrlNameObj.Enabled = OwValue
End If
End If

End Sub
 
D

Dirk Goldgar

Yue Zhao said:
I need to enable or disable a bunch of controls, according to the
value of other controls. for instance, when the checkbox called
Curr_Med_Atripla_Checkbox has a value -1, then I want to enable the
textbox called Curr_Med_Atripla_Length_Textbox.

So here is what I did, and of course,,, it didn't work.
It seems the way I was refering to the control name in Sub
EnableOtherQues was wrong: Access was trying to find the control
called "Me!CtrlNameSub", instead of "Me!Curr_Med_Atripla_Checkbox",
even when CtrlNameSub="Curr_Med_Atripla_Checkbox".

So, what is the right way to do that? Thanks a lot!

' ******
' my codes:
' ******

private sub form_curr()

...

Call EnableOtherQues("Atripla", -1, True, False, _
"Curr_Med_", "_Checkbox", "Curr_Med_", "_Length_Textbox")
end sub

Private Sub EnableOtherQues(VarName, Value, UnderValue, _
Optional OwValue, _
Optional PrefixSub as string, _
Optional SuffixSub as string, _
Optional PrefixObj as string, _
Optional SuffixObj as string)
' When one question is answered with a certain value,
enable/disable another question

' Generate the names of the Subject Control and Object Control
Dim CtrlNameSub As String, CtrlNameObj As String

CtrlNameSub = VarName
If Not IsNull(PrefixSub) Then CtrlNameSub = PrefixSub &
CtrlNameSub If Not IsNull(SuffixSub) Then CtrlNameSub =
CtrlNameSub & SuffixSub
CtrlNameObj = VarName
If Not IsNull(PrefixObj) Then CtrlNameObj = PrefixObj &
CtrlNameObj If Not IsNull(SuffixObj) Then CtrlNameObj =
CtrlNameObj & SuffixObj
' Enable and Disable
If Me!CtrlNameSub.value = Value Then
Me!CtrlNameObj.Enabled = UnderValue
Else
If Not IsNull(OwValue) Then
Me!CtrlNameObj.Enabled = OwValue
End If
End If

End Sub

' Enable and Disable
If Me.Controls(CtrlNameSub).Value = Value Then
Me.Controls(CtrlNameObj).Enabled = UnderValue
Else
If Not IsNull(OwValue) Then
Me.Controls(CtrlNameObj).Enabled = OwValue
End If
End If


Although I'm not happy with your use of "Value" as the name of an
argument. There's too much room for confusion there, if you ask me.
 
D

Douglas J. Steele

If CtrlNameSub contains the name of the control, use:

Me.Controls(CtrlNameSub)
 
Y

Yue Zhao

Thank you very much, Daouglas and Dirk!

It works now, and it is a very good reminder of Dirk that I shouldn't
have used 'Value' as the name of an argument.
 

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