Solved

MS Forms 2.0 Combo Box

Posted on 2007-12-03
11
454 Views
Last Modified: 2013-12-25
How do I use the MS Forms 2.0 combo box?  I am trying to fill the columns with different items.  I am trying to order by Last Name, UserID.  The Last Name is sorted correctly but the UserID list is sorted in respect to the Last Name and not alphabetically.  I want both the Last Name and UserID sorted alphabetically.

Below is the code.
Set rs1 = New adodb.Recordset

FirmSubTemp = "Select UserID, [First Name], [Last Name] from [User ID] where FirmSub = '" & varTemp & "' order by [Last Name], UserID"

 

'sSQl = sSQl & "Where Name =" & Chr(34) & "Boby's Place" & Chr(34)

rs1.Open FirmSubTemp, conn, adOpenDynamic, adLockOptimistic

 

'================================================================================

 

Do Until rs1.EOF = True

 

If rs1.EOF = False And rs1.BOF = False Then

UserIDCombo.AddItem rs1.Fields("UserID")

FirstName = rs1.Fields("First Name")

LastName = rs1.Fields("Last Name")

FirstLastName = FirstName & " " & LastName

NameCombo.ColumnIndex(0) = FirstLastName

 

End If

 

rs1.MoveNext

Loop

Open in new window

0
Comment
Question by:Tony789
  • 6
  • 5
11 Comments
 
LVL 22

Expert Comment

by:danaseaman
ID: 20397809
the UserID list is sorted in respect to the Last Name and not alphabetically.

This is because of multidimensional sort requested was by "order by [Last Name], UserID".

If UserID was sorted independent then data within each row of the combo would not match the recordset row.

As far as I know you can't have both columns sorted alphabetically at the same time unless you do LastName sort first, populate the combo, load recordset again sorted by UserID and append the data to the Combo. However as above as you read across the columns there will no longer be any relation between  [Last Name] and UserID for each Row of the Combo.
0
 

Author Comment

by:Tony789
ID: 20399549
Ok..I will use separate recordsets.  How do I populate the different columns in the MS Form 2.0 combo box?  Is there a Index command or something?

Thanks
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 20400212
Sample to populate first column using AddItem and subsequent columns using List(i, 1):

Private Sub Form_Load()

   Dim i As Long
 

   ComboBox1.ColumnCount = 3

   ComboBox1.ColumnWidths = "75,75,75"

   For i = 0 To 10

      ComboBox1.AddItem "LastName " & i

      ComboBox1.List(i, 1) = "FirstName " & i

      ComboBox1.List(i, 2) = "ID " & i

   Next

   ComboBox1.ListIndex = 0
 

End Sub

Open in new window

0
 

Author Comment

by:Tony789
ID: 20403551
danaseaman,

The variables I need to list in the combo box are FIRSTLASTNAME and USERID.  Your code did not work.  What is the for loop for?

Thanks
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 20403644
The sample was only to show how to add data to columns. Did you run the sample? It works here with MSForms 2.0 Combo

You will have to loop through the Recordset and using AddItem insert FirstLastName. Then you will have to sort Recorset by UserID and run through the new Recorset, this time using ComboBox1.List(i, 1) and Field UserID where i is a counter starting at 0, incrementing the counter after calling ComboBox1.List(i, 1).
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Tony789
ID: 20403744
I am already using a Do Loop.  Why do I need the For Loop?
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 20403760
You don't. You asked for info on how to add columns to Ms Forms 2.0 Combo.
0
 

Author Comment

by:Tony789
ID: 20403901
Does the below code look correct?

Thanks
Do Until rs1.EOF = True
 

If rs1.EOF = False And rs1.BOF = False Then

UserIDCombo.AddItem rs1.Fields("UserID")

UserIDVar = rs1.Fields("UserID")

FirstName = rs1.Fields("First Name")

LastName = rs1.Fields("Last Name")

FirstLastName = FirstName & " " & LastName
 

NameCombo.ColumnCount = 2

   NameCombo.ColumnWidths = "75,75"

      NameCombo.AddItem FirstLastName

      NameCombo.List(1) = UserIDVar

   NameCombo.ListIndex = 0
 
 
 

End If
 

rs1.MoveNext

Loop

Open in new window

0
 
LVL 22

Expert Comment

by:danaseaman
ID: 20403986
No. That won't work.
 
These lines need to be outside Do Loop:
NameCombo.ColumnCount = 2
   NameCombo.ColumnWidths = "75,75"

This line must be after Do Loop:
NameCombo.ListIndex = 0

This line is incorrect:
NameCombo.List(1) = UserIDVar
should be:
NameCombo.List(i,1) = UserIDVar

increment i before Loop

Give me a few minutes and I will send sample code.
0
 
LVL 22

Accepted Solution

by:
danaseaman earned 250 total points
ID: 20404079
This will load multiple columns. Change connection string to load your database:
Option Explicit
 

Private Sub Form_Load()

   Dim rs1           As ADODB.Recordset

   Dim conn          As ADODB.Connection

   Dim FirstName     As String

   Dim LastName      As String

   Dim FirstLastName As String

   Dim FirmSubTemp   As String

   Dim i             As Long

   

   NameCombo.ColumnCount = 2

   NameCombo.ColumnWidths = "75,75"

   

   Set conn = New ADODB.Connection

   conn.CursorLocation = adUseClient

   conn.Open "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\Program Files\Microsoft Visual Studio\VB98\UserID.mdb;DefaultDir=C:\Program Files\Microsoft Visual Studio\VB98\;PWD=;"

   Set rs1 = New ADODB.Recordset

   FirmSubTemp = "Select UserID, [First Name], [Last Name] from [User ID] order by [Last Name],UserID  "

 

   rs1.Open FirmSubTemp, conn, adOpenDynamic, adLockOptimistic

 

   Do Until rs1.EOF = True

 

      If rs1.EOF = False And rs1.BOF = False Then

         FirstName = rs1.Fields("First Name")

         LastName = rs1.Fields("Last Name")

         FirstLastName = FirstName & " " & LastName

         NameCombo.AddItem FirstLastName

         NameCombo.List(i, 1) = rs1.Fields("UserID")

      End If

      i = i + 1

      rs1.MoveNext

   Loop

   NameCombo.ListIndex = 0

End Sub

Open in new window

0
 

Author Comment

by:Tony789
ID: 20404483
Very nice.  It works.  Thank you for your assistance.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

20 Experts available now in Live!

Get 1:1 Help Now