?
Solved

MS Forms 2.0 Combo Box

Posted on 2007-12-03
11
Medium Priority
?
462 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
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.

 

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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

777 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