Excel User Form Sort and Populate Records

Posted on 2012-03-14
Last Modified: 2012-03-14

I have an excel file with a form that captures details of course bookings and displays the attendees in a list box.

Through the great help of an EE member, I got the populating component done but it does not sort the records in alphabetical order.

Can someone please help?

Question by:creativefusion
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 42

Accepted Solution

dlmille earned 500 total points
ID: 37722835
First, we need to update the subroutine with the ADO query (again, caveats against doing ADO against an open workbook, but... here goes):

Option Explicit
Sub FillCustomerCombox()

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim WB As String
Dim v As Variant

    'Identify the workbook you are referencing
    WB = Application.ThisWorkbook.FullName

    'Open connection to the workbook
    cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
             "Data Source=" & WB & ";" & _
             "Extended Properties=""Excel 8.0;HDR=Yes"";"

    'Load the selected range into the recordset
    rst.Open "SELECT DISTINCT [CompanyName] FROM [Database] WHERE [CompanyName] <> '' ORDER BY [CompanyName]", cnn, adOpenStatic


    With frmCourseBooking.ListBox1
            .AddItem rst![CompanyName]
        Loop Until rst.EOF Or IsNull(rst![CompanyName])
    End With

    Set rst = Nothing
    Set cnn = Nothing

End Sub

Open in new window

Note, we're pulling the field CompanyName from the Database (which need to be defined as a range name) and looking for non-null values thus the WHERE clause.

As a result, you can now call this sub to populate your listbox from the UserForm.

Also, on adding an individual, we can clear the listbox, then call this sub again to repopulate, dynamically.
Private Sub cmdOK_Click()
        If IsEmpty(ActiveCell) = False Then
            ActiveCell.Offset(1, 0).Select
        End If
    Loop Until IsEmpty(ActiveCell) = True
    ActiveCell.Value = txtName.Value
    ActiveCell.Offset(0, 1) = txtPhone.Value
    ActiveCell.Offset(0, 2) = cboDepartment.Value
    ActiveCell.Offset(0, 3) = cboCourse.Value
    If optIntroduction = True Then
        ActiveCell.Offset(0, 4).Value = "Intro"
    ElseIf optIntermediate = True Then
        ActiveCell.Offset(0, 4).Value = "Intermed"
        ActiveCell.Offset(0, 4).Value = "Adv"
    End If
    If chkLunch = True Then
        ActiveCell.Offset(0, 5).Value = "Yes"
        ActiveCell.Offset(0, 5).Value = "No"
    End If
    If chkVegetarian = True Then
        ActiveCell.Offset(0, 6).Value = "Yes"
        If chkLunch = False Then
            ActiveCell.Offset(0, 6).Value = ""
            ActiveCell.Offset(0, 6).Value = "No"
        End If
    End If
    'Me.ListBox1.RowSource = ""
    Call FillCustomerCombox
End Sub

Private Sub UserForm_Initialize()

'Dim CompName

    'With Sheets("Bookings")
    '    CompName = UNIQUE(.Range("a1").CurrentRegion.Columns(1).Offset(1))
    'End With

    txtName.Value = ""
    txtPhone.Value = ""
    With cboDepartment
        .AddItem "Sales"
        .AddItem "Marketing"
        .AddItem "Administration"
        .AddItem "Design"
        .AddItem "Advertising"
        .AddItem "Dispatch"
        .AddItem "Transportation"
    End With
    cboDepartment.Value = ""
    With cboCourse
        .AddItem "Access"
        .AddItem "Excel"
        .AddItem "PowerPoint"
        .AddItem "Word"
        .AddItem "FrontPage"
    End With
    cboCourse.Value = ""
    optIntroduction = True
    chkLunch = False
    chkVegetarian = False
    'Me.ListBox1.List = CompName
    Call FillCustomerCombox

End Sub

Open in new window

See attached.



Author Closing Comment

ID: 37722859
Awesome help. Thanks so much Dave.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question