|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Linking several combobox selection (not combo Arrays)
I want to link several combobox selections to find a specific series of values from a
CSV text file. Does anyone know how to do this? |
|
#2
|
|||
|
|||
|
Your question is too vague to be able to answer. Post some more clarification.
__________________
====== Doug G ====== I didn't attend the funeral, but I sent a nice letter saying I approved of it. --Mark Twain |
|
#3
|
|||
|
|||
|
I have three comboboxes
combo1 - Street address combo2 - street number combo3 - Direction of street. User makes selection in all three comboboxes and this selects iterates through a CSV text file to find exact values (selected from combobox). I hope thats clear. |
|
#4
|
|||
|
|||
|
Hi,
A simple solution is to make a button, grab the selected text and loop through the CSV file searching for the values. Code is below, it has a very fast CSV interpreting piece of code. Code:
'---------------------------------------------------------------------------------------
' Procedure : ParseCSV
' DateTime : 29-09-2003 10:35
' Author : Donald Lessau (VBSpeed.com)
' Purpose : Parses a CSV input string
' Inputs : sExpression: string with CSV text.
' asValues(): Array with CSV text split into it.
' Depends : None
' Revisions :
'
'---------------------------------------------------------------------------------------
Public Function ParseCSV(ByRef sExpression As String, ByRef asValues() As String) As Long
Const lAscSpace As Long = 32 ' Asc(" ")
Const lAscQuote As Long = 34 ' Asc("""")
Const lAscSeparator As Long = 44 ' Asc(","), comma
Const lValueNone As Long = 0 ' states of the parser
Const lValuePlain As Long = 1
Const lValueQuoted As Long = 2
' BUFFERREDIM is ideally exactly the number of values in Expression (minus 1)
' so: if you know what to expect, fine-tune here
Const BUFFERREDIM As Long = 64
Dim ubValues As Long
Dim cntValues As Long
Dim abExpression() As Byte
Dim lCharCode As Long
Dim posStart As Long
Dim posEnd As Long
Dim cntTrim As Long
Dim lState As Long
Dim i As Long
' ----------------------
On Error GoTo PROC_ERR
If LenB(sExpression) > 0 Then
abExpression = sExpression ' to byte array
ubValues = -1 + BUFFERREDIM
ReDim Preserve asValues(ubValues) ' init array (Preserve is faster)
For i = 0 To UBound(abExpression) Step 2
' 1. unicode char has 16 bits, but 32 bit Longs process faster
' 2. add lower and upper byte: ignoring the upper byte can lead to misinterpretations
lCharCode = abExpression(i) Or (&H100 * abExpression(i + 1))
Select Case lCharCode
Case lAscSpace
If lState = lValuePlain Then
' at non-quoted value: trim 2 unicode bytes for each space
cntTrim = cntTrim + 2
End If
Case lAscSeparator
If lState = lValueNone Then
' ends zero-length value
If cntValues > ubValues Then
ubValues = ubValues + BUFFERREDIM
ReDim Preserve asValues(ubValues)
End If
asValues(cntValues) = ""
cntValues = cntValues + 1
posStart = i + 2
ElseIf lState = lValuePlain Then
' ends non-quoted value
lState = lValueNone
posEnd = i - cntTrim
If cntValues > ubValues Then
ubValues = ubValues + BUFFERREDIM
ReDim Preserve asValues(ubValues)
End If
asValues(cntValues) = MidB$(sExpression, posStart + 1, posEnd - posStart)
cntValues = cntValues + 1
posStart = i + 2
cntTrim = 0
End If
Case lAscQuote
If lState = lValueNone Then
' starts quoted value
lState = lValueQuoted
' trims the opening quote
posStart = i + 2
ElseIf lState = lValueQuoted Then
' ends quoted value, or is a quote within
lState = lValuePlain
' trims the closing quote
cntTrim = 2
End If
Case Else
If lState = lValueNone Then
' starts non-quoted value
lState = lValuePlain
posStart = i
End If
' reset trimming
cntTrim = 0
End Select
Next
' remainder
posEnd = i - cntTrim
If cntValues <> ubValues Then
ReDim Preserve asValues(cntValues)
End If
asValues(cntValues) = MidB$(sExpression, posStart + 1, posEnd - posStart)
ParseCSV = cntValues + 1
Else
' (Expression = "")
' return single-element array containing a zero-length string
ReDim asValues(0)
ParseCSV = 1
End If
PROC_ERR:
Select Case Err.Number
Case Is <> 0
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ParseCSV of Module modGeneral"
Err.Clear
End Select
End Function
That is the code that parses a line of CSV into a array you can use. Put it in a module so you can access it from anywhere. Code:
Private Sub cmdSelect_Click()
Dim sLine As String
Dim asInput() As String
Dim hInputFile As Long
Dim bFinished As Boolean
Dim sStreet As String
Dim sHouseNumber As String
Dim sStreetDirection As String
' ------------------------------
On Error GoTo PROC_ERR
'
' Grab the data we need to search.
'
sStreet = cboStreet.Text
sHouseNumber = cboHouseNumber.Text
sStreetDirection = cboStreetDirection.Text
'
' Open the file and loop through it to find the data.
'
Open "C:\Temp\InputData.csv" For Input As hInputFile
Do Until EOF(hInputFile) Or (bFinished)
Line Input #hInputFile, sLine
'
' For sophisticated error handling, use a long value for the return
' of the function. It contains the number of elements in the array.
'
Call ParseCSV(sLine, asInput)
'
' Check for the data in the array.
'
If (asInput(0) = sStreet) And (asInput(1) = sHouseNumber) And (asInput(2) = sStreetDirection) Then
'
' You found it.
'
bFinished = True
Else
'
' You did not find it.
'
bFinished = False
End If
Loop
PROC_EXIT:
'
' Clean up and exit gracefully.
'
If (hFile <> 0) Then
Close hFile
End If
PROC_ERR:
Select Case Err.Number
Case Is <> 0
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdSelect_Click of Form frmImport"
Err.Clear
Resume PROC_EXIT
End Select
End Sub
This code will open a CSV file and loop through its contents looking for the search data. The parsing of the CSV is done by the ParseCSV function. I did not include much error handling or checking of the format of the file. I leave that up to you. One comment on the loop though. Some of you might say that you can also use a Exit Do when you find the values you are looking for. I believe that any use of Exit and Goto is evidence of poor logic. It makes your code much more harder to read and follow. By using the technique of an exit condition, your code flows more natural and is less likely to go decruct. If you need hard data, using a exit do usually increases complexity by a multiplier of 2 when using McCabe metrics, and understanding goes from simple to moderate. Bad fix percentage also increases, but not that much. I will go and hide in my cave again. Grtz.© M. |
|
#5
|
|||
|
|||
|
Hi,
Thanks Mythomep I will go and look at the code. |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Linking several combobox selection (not combo Arrays) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|