Help with a field control on form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to create a form on which there are several fields. I, however,
would like to have these fields controled by a yes/no control. I would
really like to have all of the fields stacked one in front of the other. I
would then like to click a yes/no or command button to make the wanted field
visible/accesible. Is there a way to do this using VB code or with a macro?
Thank you in advance.
 
The following example assumes a command button named cmdEnableToggle
(code in the button's Click event), and 4 controls to be
enabled/disabled by it. Can be easily modified for any number of
controls, by changing the number and adding / removing control names.

Private Sub cmdEnableToggle_Click()
Dim ctl(4) As String
Dim vStatus As Boolean
ctl(1) = "Text0"
ctl(2) = "Text2"
ctl(3) = "Text4"
ctl(4) = "Text6"
vStatus = Me.Controls(ctl(1)).Visible
If vStatus = True Then
Me.cmdEnableToggle.Caption = "Enable"
Me.cmdEnableToggle.ForeColor = vbBlue
Else
Me.cmdEnableToggle.Caption = "Disable"
Me.cmdEnableToggle.ForeColor = vbRed
End If
For i = 1 To 4
Me.Controls(ctl(i)).Visible = Not vStatus
Next
End Sub

Alternatively, if you want to toggle all textboxes on the form, you
could use something like:

Private Sub cmdEnableToggle_Click()
Dim ctl As Control
Dim vStatus As Boolean
vStatus = Me.Text0.Visible
If vStatus = True Then
Me.cmdEnableToggle.Caption = "Enable"
Me.cmdEnableToggle.ForeColor = vbBlue
Else
Me.cmdEnableToggle.Caption = "Disable"
Me.cmdEnableToggle.ForeColor = vbRed
End If
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then ctl.Visible = Not vStatus
Next
End Sub

where Text0 is the name of any of them, just so the current status is
detected.
HTH,
Nikos
 
Nikos,

Thank you very much. The code you provided did exactly what I wanted it to
do. It was a great help. Thanks again
 

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

Back
Top