Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Linking combo boxes

Posted on 1998-10-12
11
Medium Priority
?
165 Views
Last Modified: 2010-04-30
If I have two combo boxes that are both populated with different arrays from the same file.  How do I link the two so that when I choose for example the last name in the first combo box, the first name automatically appears in the second combo box??
0
Comment
Question by:amy420
[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
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 6

Expert Comment

by:clifABB
ID: 1439404
Can you show how the first and last names are stored in the program?
0
 
LVL 14

Expert Comment

by:waty
ID: 1439405
When you populate your combo box, add in the itemdata of each line, the unique item identifying the row in your table (usally an autonumber under Access), and the following code

Private Sub Combo1_Change()

   Combo2.ListIndex = CBLFindCode(Combo1, Combo1.ItemData)

End Sub



Function CBLFindCode(CBL As Control, Code As Long) As Long
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site         : www.geocities.com/ResearchTriangle/6311/
   ' * E-Mail           : waty.thierry@usa.net
   ' * Date             : 12/08/98
   ' * Time             : 17:40
   ' * Module Name      : ListBox_Module
   ' * Module Filename  : CBLSTBOX.BAS
   ' * Procedure Name   : CBLFindCode
   ' * Parameters       :
   ' *                    CBL As Control
   ' *                    Code As Long
   ' **********************************************************************
   ' * Comments         :
   ' *   Search for a code stored in the itemdata
   ' *   Return the listindex
   ' *
   ' *
   ' **********************************************************************

   Dim nIndex  As Integer
   Dim nCount  As Integer
   Dim Item    As Long

   nCount = CBL.ListCount - 1

   CBLFindCode = -1
   If (nCount = -1) Then
      CBLFindCode = -1
      Exit Function
   End If

   For nIndex = 0 To nCount
      Item = CBL.ItemData(nIndex)
      If Code = Item Then
         CBLFindCode = nIndex
         Exit For
      End If
   Next

End Function

0
 

Author Comment

by:amy420
ID: 1439406
This is how I load them from the file:
Private Sub Load_Names()
Dim strlname(1 to 8) as String, strfname(1 to 8) as String
Dim j as Integer
Open App.Path & "\Names.Txt" For Input as #1

Do While Not EOF(1)
For j = 1 to 7
Input #1, strlname(j), strfname(j)
cbolName.AddItem strlname(j)
cbofName.AddItem strfname(j)
Next
Loop
Close #1
End Sub


This does every thing but automatically fill the second combo box according to the first.  Is this what you meant??
0
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.

 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1439407
Add This to your Code..

Private Sub cbolName_Click()
   cbofName.ListIndex = cbolName.Listindex
End Sub
Private Sub cbofName_Click()
   cbolName.ListIndex = cbofName.Listindex
End Sub  
0
 

Author Comment

by:amy420
ID: 1439408
This is how I load them from the file:
Private Sub Load_Names()
Dim strlname(1 to 8) as String, strfname(1 to 8) as String
Dim j as Integer
Open App.Path & "\Names.Txt" For Input as #1

Do While Not EOF(1)
For j = 1 to 7
Input #1, strlname(j), strfname(j)
cbolName.AddItem strlname(j)
cbofName.AddItem strfname(j)
Next
Loop
Close #1
End Sub


This does every thing but automatically fill the second combo box according to the first.  Is this what you meant??
0
 

Author Comment

by:amy420
ID: 1439409
They already have an array with the same number so adding more keys is not needed.  TheAnswerMan helped me out.  Thanks though..
0
 
LVL 14

Expert Comment

by:waty
ID: 1439410
AnswerMan, your solution is not correct, if the combo are all sorted (the contents won't never be in the same order except a big chance)

amy420, when you populate, do as following :

Private Sub Load_Names()
  Dim strlname(1 to 8) as String, strfname(1 to 8) as String
  Dim j        as Integer
  dim nLine    as Long

  Open App.Path & "\Names.Txt" For Input as #1
 
  nLine = 0
  Do While Not EOF(1)
   For j = 1 to 7
     Input #1, strlname(j), strfname(j)
     cbolName.AddItem strlname(j)
     cbolName.Itemdata(cbolName.NewIndex) = nLine
     cbofName.AddItem strfname(j)
     cbofName.Itemdata(cbofName.NewIndex) = nLine

     nLine = nLine + 1
   Next
  Loop
  Close #1

End Sub

0
 
LVL 6

Expert Comment

by:clifABB
ID: 1439411
waty:
You beat me to it.  However, you don't need the new variable ('nLine') you could just as easily use the value 'j'.

While we're on the subject...
amy420:
I see a potential problem with your loops.  If there are less than 7 names, you will get a ReadPastEnd error.
If there are more than 7 names, the for/next loop will repeat possibly giving you a read past end error (if the number of names is not evenly divisable by 7).

Your code should read like this (adding waty's suggestion):
Private Sub Load_Names()
  Dim strlname(1 to 8) as String, strfname(1 to 8) as String
  Dim j        as Integer

  Open App.Path & "\Names.Txt" For Input as #1
 
  j = 0
  Do While Not EOF(1)
    Input #1, strlname(j), strfname(j)
    cbolName.AddItem strlname(j)
    cbolName.Itemdata(cbolName.NewIndex) = j
    cbofName.AddItem strfname(j)
    cbofName.Itemdata(cbofName.NewIndex) = j
    j = j + 1
  Loop
  Close #1
End Sub

0
 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1439412
No one Said anything about the combobox Being sorted.  It says..
I read them into the combos like this..   if that is the case..
I assume in the textfile that the First Name is the First name of the Same Lines last name.,

According to this.. each listindex will match up.
0
 
LVL 3

Accepted Solution

by:
TheAnswerMan earned 200 total points
ID: 1439413
thanks
0
 

Author Comment

by:amy420
ID: 1439414
I agree with you, they are always sequential......  Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

705 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