Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linking combo boxes

Posted on 1998-10-12
11
Medium Priority
?
166 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
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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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

916 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