I have a project that is driving me crazy! There is something wrong but I can’t find it!!!
Are there anyone who can help me and have a small look what it can be that is not done the right way?
I have a ListBox where I add a combination (for example SPN190). Then I click on a searchbutton ‘cmdFind’, I open a text file and search it for the specified SPN-combination. Then I want to have the whole row from the text file printed in a ListView ‘lvResults’.
The problem is that nothing comes up in the ‘lvResults’ and I do not know where the problem is, so please….HELP!
- Code: Select all
Private Sub cmdFind_Click()
On Error GoTo ErrorHandler
If lstCombo.ListCount = 0 Then
MsgBox "Please enter some combinations to search for", vbExclamation
Exit Sub
End If
With objCD
.CancelError = True
.DialogTitle = "Open File"
.Filter = "Text Files (*.txt)|*.txt"
.ShowOpen
InitCombos
If Len(.Filename) > 0 Then
FindCombo .Filename
End If
End With
Exit Sub
ErrorHandler:
End Sub
'-------------------------------------------------------
'ListBox to strCombos() array.
Private Sub InitCombos()
Dim intLoop As Long
ReDim strCombos(0 To lstCombo.ListCount - 1) As String
For intLoop = 0 To lstCombo.ListCount - 1
strCombos(intLoop) = lstCombo.List(intLoop)
Next intLoop
End Sub
'--------------------------------------------------------------
'Checks if a combination exists in the array.
Private Function ComboInList(Combination As String) As Boolean
Dim lonLoop As Long, lonUB As Long
Dim strToComp As String
strToComp = LCase$(Left$(Combination, 6))
lonUB = SafeUBString(strCombos())
For lonLoop = 0 To lonUB
If LCase$(strCombos(lonLoop)) = strToComp Then
ComboInList = True
Exit For
End If
Next lonLoop
End Function
'------------------------------------------------------------------------
'The actual sub that will search the file for the combinations in strCombos().
Private Sub FindCombo(FilePath As String)
Dim intFF As Long
Dim strLine As String
Dim lonSpacePos As Long
Dim strLineFull As String
Dim strArray() As String
Dim PGNPos As Long
Dim Resolution As Long
Dim Offset As Long
Dim PGNNumber As String
Dim lonLineCounter As Long
Dim intI As Long
Dim SPNNumber As String
Dim DataLength As Long
Dim Unit As String
Dim Description As String
Dim mItem As Variant 'belongs to lvResults
With lvResults
.ListItems.Clear
'Get available file handle.
intFF = FreeFile
'Open file.
Open FilePath For Input As #intFF
'Clear any previous items.
.ListItems.Clear
'Keep looping until we get to End Of File (EOF).
Do While Not EOF(intFF)
'Get the current line from file and put it into strLine.
Line Input #intFF, strLine
strLineFull = strLine
If Len(strLine) > 0 Then 'Make sure it isn't an empty line.
'Get text from start to before the first space
'This will give us the combination.
lonSpacePos = InStr(1, strLine, " ")
If lonSpacePos > 0 Then
strLine = Mid$(strLine, 1, lonSpacePos - 1)
'We now have the combination in strLine.
'Check if it is in the list.
If ComboInList(strLine) Then 'ComboInList is a function
strArray = Split(strLineFull, " ")
If UBound(strArray) > 7 Then
For intI = 8 To UBound(strArray)
strArray(7) = strArray(7) & " " & strArray(intI)
Next intI
End If
SPNNumber = Right(strArray(0), 4)
PGNNumber = Right(strArray(1), 4)
PGNPos = CInt(strArray(2))
DataLength = CInt(strArray(3))
Resolution = CInt(strArray(4))
Offset = CInt(strArray(5))
Unit = CInt(strArray(6))
Description = CInt(strArray(7))
'Get the result in the ListView lvResults
Set mItem = .ListItems.Add()
'mItem.Text = Right$(strArray(0), 4)
mItem.Text = strArray(0)
'mItem.SubItems(1) = Right$(strArray(1), 4)
mItem.SubItems(1) = strArray(1)
mItem.SubItems(2) = strArray(2)
mItem.SubItems(3) = strArray(3)
mItem.SubItems(4) = strArray(4)
mItem.SubItems(5) = strArray(5)
mItem.SubItems(6) = strArray(6)
mItem.SubItems(7) = strArray(7)
End If
End If
End If
Loop
Close #intFF
End With
End Sub


