Solved

Excel User Form Sort and Populate Records

Posted on 2012-03-14
2
240 Views
Last Modified: 2012-03-14
All,

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?

CF
Test.xls
0
Comment
Question by:creativefusion
2 Comments
 
LVL 41

Accepted Solution

by:
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

    rst.MoveFirst

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

    rst.Close
    Set rst = Nothing
    cnn.Close
    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()
    ActiveWorkbook.Sheets("Bookings").Activate
    Range("A1").Select
    Do
        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"
    Else
        ActiveCell.Offset(0, 4).Value = "Adv"
    End If
    If chkLunch = True Then
        ActiveCell.Offset(0, 5).Value = "Yes"
    Else
        ActiveCell.Offset(0, 5).Value = "No"
    End If
    If chkVegetarian = True Then
        ActiveCell.Offset(0, 6).Value = "Yes"
    Else
        If chkLunch = False Then
            ActiveCell.Offset(0, 6).Value = ""
        Else
            ActiveCell.Offset(0, 6).Value = "No"
        End If
    End If
    Range("A1").Select
    '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

    txtName.SetFocus
End Sub

Open in new window


See attached.

Cheers,

Dave
setPrintAreaEntireWorkbook-r1.xls
0
 

Author Closing Comment

by:creativefusion
ID: 37722859
Awesome help. Thanks so much Dave.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

777 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