Solved

MS Forms 2.0 Combo Box

Posted on 2007-12-03
11
461 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

726 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