|
-
Jan 3rd, 2011, 02:05 AM
#1
Thread Starter
New Member
Noob trying to filter Combo boxes
Hi. so, I'm a total noob when it comes to visual basic coding. I've been stepping thru a book for about a month and have a pretty good understanding of everything in it. Now, I'm trying my own hand at my own program, no book example to follow. And I've run into my first problem. I have 2 combo boxes, I want to filter the second combo box based on the users selection of the first combo box. For example, let's say I have vehicle makes in the first combo box, Ford, Chevy, Toyota.... and in the second combo box I have the models, F150, Fiesta, Blazer, S10, Corolla, Camry etc. The idea here is that if the user selects the Ford item from combo box 1 then he should only be able to see F150, Fiesta...etc. Please help, step by step would be awesome!
-
Jan 3rd, 2011, 03:42 AM
#2
Re: Noob trying to filter Combo boxes
I am assuming you have two tables where the data for both the combo boxes comes from. So you have a table tblMake which tracks the vehicle make, and another table tblModel which tracks the model of the vehicle against each make. tblMake would probably have a primary key, let's call it MakeID. Similarly tblModel could have a primary key, ModelID. Most importantly, tblModel must have a field MakeID which contains the appropriate MakeIDs to link the two records. A sample of data would look something like this:
Code:
tblMake
MakeID MakeName
01 Ford
02 Toyota
tblModel
ModelID MakeID ModelName
01 01 Focus
02 01 Taurus
03 01 Fiesta
04 02 Camry
05 03 Corolla
The simplest you could do is
1. Load up the first combo box with data from the first table, tblMake.
2. Handle the SelectedIndexChanged event of the combo to retrieve the make the user has selected
3. Use the index above to load up the second combo box with relevant data from the second table, tblModel.
Steps 2 and 3 would go into the SelectedIndexChanged event handler.
.
-
Jan 3rd, 2011, 03:56 AM
#3
Re: Noob trying to filter Combo boxes
The first thing you must do is to somehow relate the Model to the Make, ie for each Model you must know the Make. How you do this will depend upon where the data is coming from. In 'real life' as there are so many different Makes and Models the data would probably be in a Database which would make your life much easier, but you'd need to understand SQL and ADO, which as a newbie, may be a bit of a challenge at the moment.
Instead, let's assume you have the data in a flat file in a format like:
Code:
[Make=Ford]
Anglia (1940–1967, Europe)
Aspire (1994–1997, rebadged Kia Avella)
[Make=Toyota]
AA (1936-1943)
AB (1936-1943)
AC (1943-1947)
AE (1941-1943)
Allex (2001-2006)
[Make=Chevrolet]
Agile (2009–present) Developed by General Motors in Brazil and built in Argentina.
Alero (1999–2004) A rebadged Olds' sold in select countries in EuropeApache (1958–1959)
Astro (1985–2005)
Where the list of Models for a given Make appear immediately after a [Make=...] record.
There are many ways to 'split' up and relate the Makes and Models, here's an example using the data above. I don't know how much of this is new to you, and there may be much simpler ways, but it demonstrates implementation of the following techniques / features of VB6:
(1) File I/O - reading a file
(2) Using the Split Function to divide a string into an array based upon a terminator - Split()
(3) Parsing Data to ensure it's in the correct Format and reporting errors
(4) Using Dynamic Arrays - strRecords and colModels
(5) Searching for a Sub-String within a String - InStr
(6) Copying a Sub-String from a String - Mid$
(7) Coding the Click Event of a ComboBox - cmbMake_Click
(8) Using the ComboBox ListIndex property to select a (Collection) Array element
(9) Simple Adding and Retrieving Items to and from a Collection
Whilst it's a lot to take in in one go, as you progress and gain more experience you'll probably be using many of the above techniques frequently !
Hopefully the comments will help you understand the Logic and flow 
Code:
Option Explicit
Private colModels() As New Collection
Private Sub cmbMake_Click()
'
' User has selected a Make
' Populate the Model ComboBox with the appropriate Models
'
Dim intI As Integer
cmbModel.Clear
For intI = 1 To colModels(cmbMake.ListIndex).Count
cmbModel.AddItem colModels(cmbMake.ListIndex).Item(intI)
Next intI
cmbModel.Text = cmbModel.List(0)
End Sub
Private Sub Form_Load()
Dim intFile As Integer
Dim intI As Integer
Dim intPos As Integer
Dim intPos1 As Integer
Dim intCount As Integer
Dim strData As String
Dim strMake As String
Dim strRecords() As String
Dim boError As Boolean
'
' Clear the contents of the ComboBoxes
'
cmbMake.Clear
cmbModel.Clear
'
' Allocate a FileNumber from the list of free numbers
'
intFile = FreeFile
'
' Open and read the entire contentsof the file
'
Open App.Path & "\" & "VehicleList.txt" For Input As intFile
strData = Input(LOF(intFile), intFile)
Close intFile
'
' Each record ends with a Carriage Return Line feed pair
' The Split Function will split the data into records
' and put each record intoan element of the Array strRecords
'
strRecords = Split(strData, vbCrLf)
'
' We now have an array containing all the records of the file
' Go through Record by Record and:
' Remove leading and trailing spaces from each record (Trim$)
' Ignore any blank records
' If the record starts with a "[" then it's a 'Make' record
' parse the record to extract the actual Make
' ie the data between the '=' and the ']'
' then create a new Collection to hold the Models for this make
' (The element number of the Collection Array is = intCount)
' then add the Model to the Model ComboBox if there weren't any errors
'
intCount = -1
Do
strRecords(intI) = Trim$(strRecords(intI))
If strRecords(intI) = "" Then
intI = intI + 1
Else
If Mid(strRecords(intI), 1, 1) = "[" Then
intPos = InStr(strRecords(intI), "=")
If intPos <> 0 Then
intPos1 = InStr(strRecords(intI), "]")
If intPos1 <> 0 Then
strMake = Mid$(strRecords(intI), intPos + 1, intPos1 - (intPos + 1))
'
' This is a new Make so addanotherelement to the Collection Array
' increment the Record element pointer (intI)
' to pick up the first Model
'
cmbMake.AddItem strMake
intCount = intCount + 1
intI = intI + 1
ReDim Preserve colModels(intCount)
Else
MsgBox "Format Error in Data File, Missing ]"
boError = True
End If
Else
MsgBox "Format Error in Data File, Missing ="
boError = True
End If
End If
'
' If there were no errors, add this Model to the collection
'
If boError = False Then
colModels(intCount).Add strRecords(intI)
intI = intI + 1
Else
'
' if there were errors then stop processing the data
' (by setting the counter to a value greater than the
' number of records)
'
intI = UBound(strRecords) + 1
End If
End If
Loop Until intI > UBound(strRecords)
'
' Select the first Item in the Make ComboBox
' and populate the Model ComboBox with the Models
'
cmbMake.Text = cmbMake.List(0)
cmbMake.ListIndex = 0
End Sub
Copy and Paste the Code into the Declarations Section of a new project's Form, and draw two ComboBoxes on the Form (cmbMake and cmbModels)
Copy and Paste the Text file example into a Notepad document and save it as
VehicleList.txt in the same folder as the new Project.
Run it. When you click on a Make the Models will appear in the cmbModels ComboBox. You can add as many more Makes and Models as you like to the text file without changing the code at all.
-
Jan 3rd, 2011, 04:29 AM
#4
Re: Noob trying to filter Combo boxes
 Originally Posted by honeybee
Steps 2 and 3 would go into the SelectedIndexChanged event handler.
I'm really hoping that, since this thread is in the VB6 and earlier section, OP is not using a .Net Version of VB 
-
Jan 3rd, 2011, 07:46 PM
#5
Re: Noob trying to filter Combo boxes
 Originally Posted by Doogle
I'm really hoping that, since this thread is in the VB6 and earlier section, OP is not using a .Net Version of VB  
He/she might be. I thought of that also.
-
Jan 16th, 2011, 11:26 PM
#6
Thread Starter
New Member
Re: Noob trying to filter Combo boxes
I think I am using the .Net version of VB. I downloaded the free version "Visual Basic Express" from Microsoft's web site. How would I find out for sure what version of VB I am using? I'm assuming one version of VB is more powerful / different than the other.
-
Jan 17th, 2011, 03:09 AM
#7
Re: Noob trying to filter Combo boxes
If it's Visual Basic Express, it's definitely not VB6. It's also not VB.Net 1.x. It should be either VB.Net 2.0 or above, depending on when you downloaded it. For your problem, it doesn't really matter if you are using VB.Net 2.0 or VB.Net 3.0 or VB.Net 3.5.
Have you done anything based on the suggestions given here?
.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|