Solved

Linking combo boxes

Posted on 1998-10-12
11
158 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
  • 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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 50 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now