Solved

Excel User Form Sort and Populate Records

Posted on 2012-03-14
2
246 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
[X]
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
2 Comments
 
LVL 42

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
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‚Ķ

705 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