views:

145

answers:

2

I have been trying to find out why in the following code, the third time through the loop I am getting a Error type 13 Mismatch when the line "For lCount = 0 To maxCount" is being evaluated. I had originally thought the problem was in getting the value from the vArray, but testing shows it to be triggered by the "For" line. I haven't a clue as to how the type would be changing during the processing of the loop. Thanks!

    Public Function FindCodeIndex(vArray As Variant, MatchValue As String) As Integer
    ''This function locates a value in a combo box returning the index or -1 if not found
    Dim lCount As Long
    Dim maxCount As Long
    Dim arrayStr As String


    On Error GoTo ErrorHandler

    maxCount = UBound(vArray)


    For lCount = 0 To maxCount
    arrayStr = vArray(1, lCount)

        If UCase$(arrayStr) = UCase$(MatchValue) Then
            FindCodeIndex = Int(lCount)
            Exit Function
        End If
    Next lCount

    FindCodeIndex = -1

    Exit Function


ErrorHandler:

MsgBox "Unexpected error in frmComment::FindCodeIndex()" & vbCrLf & _
           "Error Code: " & CStr(Err.Number) & " Error Desc: " & Err.Description
+1  A: 
Public Function FindCodeIndex(Array() As String, ByVal MatchValue As String) As Long

    Dim index As Long
    Dim upper_bound As Long

    upper_bound= UBound(Array)
    MatchValue = UCase(MatchValue)

    For index = 0 To upper_bound
        If UCase(Array(index)) = MatchValue Then
            FindCodeIndex = index 
            Exit Function
        End If
    Next index 

    FindCodeIndex = -1

End Function
ChaosPandion
VB definitely doesn't like that solution, complaining about Array() not being defined. Most recent test shows that setting a Long = -1 is also throwing an error(?)
Timbuck
@Timbuck. `Array` is not a valid identifier in VB6, because it conflicts with the built-in `Array` function.
Mike Spross
Exactly, which is why I'm unsure why ChaosPandion put this code here, unless I'm missing something.
Timbuck
A: 

The function mentions that the code is being written for a ComboBox (are you actually copying each item in the List() method into an array and sending this to your function?). This seems a little over-complicated if you are using the standard VB ComboBox. Just use the following code:

Private Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal uMsg As Long, ByRef wParam As Any, ByRef lParam As Any) As Long

Private Const CB_FINDSTRINGEXACT As Long = &H158

Public Function FindCodeIndex(ByRef cmb As ComboBox, ByRef sMatchValue As String) As Long
'This function locates a value in a combo box returning the index or -1 if not found

    FindCodeIndex = SendMessage(cmb.hWnd, CB_FINDSTRINGEXACT, ByVal -1, ByVal sMatchValue

End Function

It is a lot quicker and smaller to use the Windows API in this case.

Mark Bertenshaw
That's somewhat bad commenting on my part - and also related to a bad decision made about ten years ago by another developer. Effectively what I have is an array with two rows, the first being a list of code descriptions and the second being the associated list of codes. The function above looks through the array for the matching code value, and sets the combobox listindex = to the position of the array the item was found. The combobox shows the code descriptions.
Timbuck