Solved

MS Forms 2.0 Combo Box

Posted on 2007-12-03
11
457 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

856 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