[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Excel User Form Sort and Populate Records

Posted on 2012-03-14
2
Medium Priority
?
251 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 42

Accepted Solution

by:
dlmille earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

825 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