Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Dynamic Hide/Unhide multiple columns on subform 1

Status
Not open for further replies.

Reggaefloww

Programmer
Apr 8, 2006
16
US
Hi all

I have used Microsoft Access for a few years and I would like to use VBA to hide/unhide several columns on a subform. I'm sure there is a simple answer to it, but I can't seem to find anyone who has spelled it out yet. I would like to use a multiselect list box to hide/unhide fields in a subform. I've looked for this all over the internet, and only Allen Browne seemed to know what it was. The only problem was he didn't write out how to do it. Thanks guys.

Reggaefloww
 
Opps, this is my first post to this forum. I created the post as a programmer instead of as a technical user:).
 
My subform is named mySubform

My list 2 holds the names of my controls
As I click through my multi select it shows or hides fields in a data sheet view.
Code:
Private Sub List2_AfterUpdate()
  Dim intCounter As Integer
  Dim strControl As String
  For intCounter = 0 To List2.ListCount - 1
    If List2.Selected(intCounter) Then
      blnHide = True
    End If
    strControl = List2.ItemData(intCounter)
    Me.mySubForm.Form.Controls(strControl).ColumnHidden = blnHide
    blnHide = False
  Next intCounter
End Sub
 
By the way, that is a pretty neat idea. I think I will use it in the future.
 
One more thing. On the on open event you probably want to write code to unhide all columns. You can end up hiding all or most columns and when you open the form the user may not know to click in the list box to unhide the selections.
 
Actually I could have done it a lot more compact, but I thought it might be hard to follow.

Code:
Private Sub List2_AfterUpdate()
  Dim intCounter As Integer
  For intCounter = 0 To List2.ListCount - 1
    me.mySubForm.Form.Controls(List2.ItemData(intCounter)).ColumnHidden = List2.Selected(intCounter)
  
  Next intCounter
End Sub
 
Thanks MajP

Each solution solved the problem. It was a great help. How would you hide all the subform columns when you opened the main form? As you'll notice, I also gave you a star:).
 
One way would be to use the same code in the form's on open event but set everything to false.

Private Sub form_open()
Dim intCounter As Integer
For intCounter = 0 To List2.ListCount - 1
me.mySubForm.Form.Controls(List2.ItemData(intCounter)).ColumnHidden = False
Next intCounter
End Sub
 
I guess I should start a new post lol, since you did solve the original problem.
 
Oops. I said columnhidden = false, I should have said True if you want to hide not show.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top