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!

Determine column clicked on in listbox 1

Status
Not open for further replies.

zandsc1

Programmer
Nov 12, 2008
53
US
Is there anyway I can determine which column a user clicked on when they click a listbox? I'm hoping there's a property that I'm just not aware of.

I.e. The user gets a part number and the labor on 4 different steps for that part. They want to see the details on the labor for step 3 so they click on the 4th column (part, labor1, labor2, labor3, labor4). Access selects the entire column, but I want to give the user the ability to see just the details for labor3. Thoughts?

Am I trying to do something beyond Access' capabilities?
 
This will work on any listbox.

Drop this into a CLASS module named exactly "ColumnListBox"
Code:
Option Compare Database
Option Explicit

Private WithEvents mListBox As Access.ListBox
Private mColWidths As New Collection
Private mClickedColumn As Integer
Private mClickedValue As String

Public Sub Init(TheListBox As ListBox)
  Set mListBox = TheListBox
  mListBox.OnMouseDown = "[Event Procedure]"
  loadColumnWidths
End Sub
Private Sub loadColumnWidths()
  Dim aColWidths() As String
  Dim colWidth As Variant
  aColWidths = Split(mListBox.ColumnWidths, ";")
  For Each colWidth In aColWidths
     mColWidths.Add (CLng(colWidth))
  Next colWidth
End Sub
Private Function GetClickedColumn(X As Single)
  Dim TotalWidth As Single
  Dim itm As Variant
  Dim I As Integer
  For Each itm In mColWidths
    TotalWidth = TotalWidth + itm
    If X < TotalWidth Then
      GetClickedColumn = I
      Exit Function
    End If
    I = I + 1
  Next itm
End Function
Private Sub mListBox_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  mClickedColumn = GetClickedColumn(X)
End Sub
Public Property Get ClickedColumn() As Integer
  ClickedColumn = mClickedColumn
End Property
Public Property Get ClickedColumnValue() As Variant
   ClickedColumnValue = mListBox.Column(Me.ClickedColumn)
End Property

In the form to use it, something like:

Code:
Private CLB As New ColumnListBox

Private Sub Form_Load()
  CLB.Init Me.List0
End Sub

Private Sub List0_Click()
  MsgBox CLB.ClickedColumn
  MsgBox CLB.ClickedColumnValue
End Sub
 
Howdy MajP . . .

Very Nice ... Veeery Nice!

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
MajP . . .

You should make it an [blue]FAQ![/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
MajP,

Thank you very much. This is exactly what I was looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top