Hide and change backcolor of txtBox

S

SoggyCashew

Hello, I have 5 text boxes on my form with a value of 0. I wanted the
txtBoxes visible = false and if there is something other than 0 in the box
then change that text box to visible = true and the txtBox background to red
with a white forecolor. How would I do this? Thanks!
 
D

Damon Heron

Depends on where the data is coming from. If it is part of records that you
are paging thru, then in the current event of the form,
put:
Private Sub Form_Current()
Dim MyObject As Control

For Each MyObject In Me ' Iterate through each element.
With MyObject
If .ControlType = acTextBox Then
If .Value = 0 Then '
.Visible = False
Else
.Visible = True
End If
End If
End With
Next

End Sub

In design view, set the backcolor and forecolor to red and white, since they
won't be visible, so there is no need to do it in code.

Damon
 
S

SoggyCashew

Damon, This wont work because this is on a form that has LOTS of text boxes
due to it being a calendar. I just need it to work with 5 perticular text
boxes not all of them on the form. Is there a way to change your code to
acomplish this? I thought about using conditional formating and it would work
but how would I get the visible part to work in conditional formating if its
possible? Thanks!
--
Thanks,
Chad


Damon Heron said:
Depends on where the data is coming from. If it is part of records that you
are paging thru, then in the current event of the form,
put:
Private Sub Form_Current()
Dim MyObject As Control

For Each MyObject In Me ' Iterate through each element.
With MyObject
If .ControlType = acTextBox Then
If .Value = 0 Then '
.Visible = False
Else
.Visible = True
End If
End If
End With
Next

End Sub

In design view, set the backcolor and forecolor to red and white, since they
won't be visible, so there is no need to do it in code.

Damon
 
D

Damon Heron

Try using the tag property of each texbox that you want to check. Then
amend my code to

If .ControlType = acTextBox Then
if .tag= "tag you added" then
If .Value = 0 Then '
.Visible = False
Else
.Visible = True
End If
End If
End if

Damon

SoggyCashew said:
Damon, This wont work because this is on a form that has LOTS of text
boxes
due to it being a calendar. I just need it to work with 5 perticular text
boxes not all of them on the form. Is there a way to change your code to
acomplish this? I thought about using conditional formating and it would
work
but how would I get the visible part to work in conditional formating if
its
possible? Thanks!
 

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