• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

MS Forms 2.0 Combo Box

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
Tony789
Asked:
Tony789
  • 6
  • 5
1 Solution
 
danaseamanCommented:
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
 
Tony789Author Commented:
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
 
danaseamanCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Tony789Author Commented:
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
 
danaseamanCommented:
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
 
Tony789Author Commented:
I am already using a Do Loop.  Why do I need the For Loop?
0
 
danaseamanCommented:
You don't. You asked for info on how to add columns to Ms Forms 2.0 Combo.
0
 
Tony789Author Commented:
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
 
danaseamanCommented:
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
 
danaseamanCommented:
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
 
Tony789Author Commented:
Very nice.  It works.  Thank you for your assistance.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now