using variables with text boxes

  • Thread starter Thread starter Rick B
  • Start date Start date
R

Rick B

I have a data entry form where selecting a particular item from a combo box
will change a specific text box visible property to true. (By default they
are all hidden)

My question is that I have maybe 60 or 80 different text boxes each
representing a different field. I have named each of the text boxes the same
name as its corresponding field name with the exception of adding "txt_" in
front of it. I have applied the following code to the change property of the
combo box however there are tons of these IF statements. Is there a way one
could use variables to write this instead of having to write a ton of IF
statements? This would also help if the field names ever have to be
modified.

If cmbCategory = "Category1" Then
Forms!frmDataEntry![txt_Category1].Visible = True
Else
Forms!frmDataEntry![txt_Category1].Visible = False
End If

Thank you in advance,

-rick
 
Rick said:
I have a data entry form where selecting a particular item from a combo box
will change a specific text box visible property to true. (By default they
are all hidden)

My question is that I have maybe 60 or 80 different text boxes each
representing a different field. I have named each of the text boxes the same
name as its corresponding field name with the exception of adding "txt_" in
front of it. I have applied the following code to the change property of the
combo box however there are tons of these IF statements. Is there a way one
could use variables to write this instead of having to write a ton of IF
statements? This would also help if the field names ever have to be
modified.

If cmbCategory = "Category1" Then
Forms!frmDataEntry![txt_Category1].Visible = True
Else
Forms!frmDataEntry![txt_Category1].Visible = False
End If


Something about this sounds kinda funky, but I think you can
do it all with a short loop:

For K = 0 To cmbCategory.ListCount - 1
strName = cmbCategory.Column(cmbCategory.BoundColumn-1,K)
Me("txt_" & strName ).Visible = (cmbCategory = strName)
Next K
 
Thank you very much....this works perfectly

-rick

Marshall Barton said:
Rick said:
I have a data entry form where selecting a particular item from a combo box
will change a specific text box visible property to true. (By default they
are all hidden)

My question is that I have maybe 60 or 80 different text boxes each
representing a different field. I have named each of the text boxes the same
name as its corresponding field name with the exception of adding "txt_" in
front of it. I have applied the following code to the change property of the
combo box however there are tons of these IF statements. Is there a way one
could use variables to write this instead of having to write a ton of IF
statements? This would also help if the field names ever have to be
modified.

If cmbCategory = "Category1" Then
Forms!frmDataEntry![txt_Category1].Visible = True
Else
Forms!frmDataEntry![txt_Category1].Visible = False
End If


Something about this sounds kinda funky, but I think you can
do it all with a short loop:

For K = 0 To cmbCategory.ListCount - 1
strName = cmbCategory.Column(cmbCategory.BoundColumn-1,K)
Me("txt_" & strName ).Visible = (cmbCategory = strName)
Next K
 
Give the following a try

Dim lngSub As Long

' Set all text boxes to invisible
For lngSub = 0 To cmbCategory.ListCount - 1
Controls("txt_" & cmbCategory.Column(0,lngSub)).Visible
= False
Next

' Set the selected one to visible
Controls("txt_" & cmbCategory.Column(0,
cmbCategory.ListIndex)).Visible = True

I have assumed that the combo box is on frmDataEntry and
have not qualified the Controls collection.

Hope This Helps
Gerald Stanley MCSD
 
Back
Top