Solved

Excel User Form Sort and Populate Records

Posted on 2012-03-14
2
237 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sum Based On Criteriya 7 19
Hlookup formula help 14 19
macro for each dropdown 15 44
Help with Excel formula 6 35
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now