开发者

VB6 Type Mismatch in For loop condition

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) A开发者_如何学运维s 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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜