Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 22nd, 2007, 09:05 AM
Authorized User
 
Join Date: Oct 2006
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default Heirarchical (?) List Box-How to

Not sure if heirarchical is the correct term...

What I want to do is have a list box or its equivalent that will display a list of names and allow the user to change the ordering of the list by promoting (either dragging and drop up or using a button) or demoting (dragging down or a button). Is dragging even an option?

The changed order would then be used for report ordering.

 
Old October 22nd, 2007, 09:55 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Yikes. I wrote a bunch of code recently for this issue. If you search my posts you should find it within the last month or so. There is quite a bit of code to this. THEN you have to figure out how to group based on this data. That should be straightforward if you use a table to hold the results. Let me know where you need the gaps filled in.


mmcdonal
 
Old October 23rd, 2007, 08:26 PM
Registered User
 
Join Date: Oct 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for the lengthy code, but this works very well. I have a form with a list box (lstMain) and several buttons (item up, item down, add, edit, delete). The text box (txtLineData) displays the item in the list box that is selected. It is used for adding and editing the items.

The list box is populated by the LoadList sub procedure. Here is the code for this form;

Option Compare Database
Option Explicit
Dim lOrigId As Long
Dim iState As Integer

Const iAdd As Integer = 1
Const iEdit As Integer = 2
Const iDelete As Integer = 3

Private Sub cmdAdd_Click()

    Me.txtLineData = ""
    Me.txtLineData.Visible = True
    Me.txtLineData.SetFocus
    Me.cmdAdd.Enabled = False
    Me.cmdEdit.Enabled = False
    Me.cmdDelete.Enabled = False
    Me.cmdSave.Visible = True
    Me.cmdCancel.Visible = True
    iState = iAdd

End Sub

Private Sub cmdCancel_Click()

    Me.lstMain.SetFocus
    Me.cmdAdd.Enabled = True
    Me.cmdDelete.Enabled = True
    Me.cmdEdit.Enabled = True
    Me.cmdCancel.Visible = False
    Me.cmdSave.Visible = False
    Me.txtLineData = ""
    Me.txtLineData.Visible = False

End Sub

Private Sub cmdDelete_Click()

    If MsgBox("Are you sure that you want to delete this record?", vbQuestion + vbYesNo, "Delete confirmation...") = vbYes Then

        DoCmd.SetWarnings False
        DoCmd.RunSQL "Delete * From PandLTemplate Where ID = " & Me.lstMain.Column(1)
        DoCmd.SetWarnings True
        Call LoadList

    End If

End Sub

Private Sub cmdDown_Click()

    'move the item down
    Dim i, io As Integer
    Dim s As String

    If Me.lstMain.ListIndex > Me.lstMain.ListCount - 2 Or Me.lstMain.ListIndex = -1 Then

        Exit Sub

    End If

    'get selected index

    i = Me.lstMain.ListIndex

    'get item at index, again assuming the item is a string

    s = Me.lstMain.Column(0, i)
    io = Me.lstMain.Column(1, i)

    'removing it from the list at the selected index

    Me.lstMain.RemoveItem (i)

    'inserting at the index below

    Me.lstMain.AddItem s & ";" & Str(io), i + 1

    'selecting moved item

    Me.lstMain.Selected(i + 1) = True

End Sub

Private Sub cmdEdit_Click()

    If IsNull(Me.lstMain.ItemsSelected) = False Then

        Me.lstMain.SetFocus
        Me.cmdAdd.Enabled = False
        Me.cmdEdit.Enabled = False
        Me.cmdDelete.Enabled = False
        Me.cmdSave.Visible = True
        Me.cmdCancel.Visible = True
        Me.txtLineData = Me.lstMain.Column(0)
        lOrigId = Me.lstMain.Column(1)
        Me.txtLineData.Visible = True
        Me.txtLineData.SetFocus
        Me.txtLineData.SelStart = Len(Me.lstMain.Column(0))
        iState = iEdit

    End If

End Sub

Private Sub cmdSave_Click()

    If iState = iAdd Then

        If IsNull(Me.txtLineData) = False Then

            DoCmd.SetWarnings False
            DoCmd.RunSQL "Insert into PandLTemplate (Descr, OrderNo) Values ('" & Me.txtLineData & "', 200)"
            DoCmd.SetWarnings True

        End If


    ElseIf iState = iEdit Then

        If IsNull(Me.txtLineData) = False And IsNull(lOrigId) = False And lOrigId > 0 Then

            DoCmd.SetWarnings False
            DoCmd.RunSQL "Update PandLTemplate Set Descr = '" & Me.txtLineData & "' Where ID = " & lOrigId
            DoCmd.SetWarnings True

        End If

    ElseIf iState = iDelete Then

    End If
    Me.lstMain.SetFocus
    Me.cmdAdd.Enabled = True
    Me.cmdDelete.Enabled = True
    Me.cmdEdit.Enabled = True
    Me.cmdCancel.Visible = False
    Me.cmdSave.Visible = False
    Me.txtLineData = ""
    Me.txtLineData.Visible = False

    Me.lstMain.Selected(0) = False
    Me.cmdUp.SetFocus
    lOrigId = 0
    Me.lstMain.RowSource = ""
    Call LoadList

End Sub

Private Sub cmdUp_Click()

    'move the item up
    Dim i, io As Integer
    Dim s As String

    If Me.lstMain.ListIndex < 1 Then

        Exit Sub

    End If

    'get selected index

    i = Me.lstMain.ListIndex

    'get item at index, assuming the item is a string

    s = Me.lstMain.Column(0, i)
    io = Me.lstMain.Column(1, i)

    'removing it from the list at the selected index

    Me.lstMain.RemoveItem (i)

    'inserting at the index above

    Me.lstMain.AddItem s & ";" & Str(io), i - 1

    'selecting moved item

    Me.lstMain.Selected(i - 1) = True

End Sub

Private Sub Form_Close()

    Dim i As Integer
    DoCmd.SetWarnings False

    Me.lstMain.Selected(0) = True

    For i = 0 To Me.lstMain.ListCount - 1

        DoCmd.RunSQL "Update PandLTemplate Set OrderNo = " & Me.lstMain.ListIndex & " Where ID = " & Me.lstMain.Column(1)
        Me.lstMain.Selected(i) = True

    Next i
    DoCmd.SetWarnings True

End Sub

Private Sub Form_Load()

    Me.cmdCancel.Visible = False
    Me.cmdSave.Visible = False
    Me.txtLineData.Visible = False

    Me.lstMain.RowSource = ""
    Me.lstMain.Requery
    Call LoadList
    Me.lstMain.Selected(0) = True

End Sub

Private Sub LoadList()

    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset

    Set cn = CurrentProject.AccessConnection
    Set rs = New ADODB.Recordset

    With rs
        Set .ActiveConnection = cn
        .LockType = adLockOptimistic
        .CursorType = adOpenKeyset
    End With

    rs.Open "Select Descr, ID, OrderNo From PandLTemplate Order By OrderNo"

    Me.lstMain.RowSource = ""
    Me.lstMain.Requery
    Do While rs.EOF = False

        Me.lstMain.AddItem rs("Descr") & ";" & rs("ID")

        rs.MoveNext

    Loop
    If rs.State = adStateOpen Then rs.Close
    If cn.State = adStateOpen Then cn.Close
    Set rs = Nothing
    Set cn = Nothing

End Sub

++++++++++ I hope this helps you. It works great for me. +++++++







Similar Threads
Thread Thread Starter Forum Replies Last Post
Grab Values From List Box into Text Box phungleon VB How-To 2 June 19th, 2008 10:33 PM
multi-column list box values moved to 2nd list box sbmvr Access VBA 1 May 14th, 2007 01:58 PM
select box/List box alphabetic sort sasidhar79 Javascript How-To 3 November 10th, 2004 03:04 AM
Populate List Box by Combo Box Selection mmcdonal Access 2 June 15th, 2004 12:08 PM
Search using drop down list box and a text box tcasp Classic ASP Basics 1 July 31st, 2003 02:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.