Solved

Linking combo boxes

Posted on 1998-10-12
11
164 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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 Month8 days, 7 hours left to enroll

617 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