tags:

views:

2067

answers:

7

I'm not new to VB6 programming, but I'm not a master at it either. Hopefully someone can help me out with a question that I have concerning a Type Mismatch error that I'm receiving from trying to set an int variable with a int returned from a function.

The integer that I'm trying to set is defined as:

Global AICROSSDOCKStatus As Integer

Now when I try to make this call I get the Runtime Error 13

AICROSSDOCKStatus = ProcessQuery(iocode, pb, AICROSSDOCBOLFN, "")

I've stepped through debugging the program line for line. The ProcessQuery function gets and returns the expected integer, but when the assignment is to be made to the AICROSSDOCKStatus it fails.

On a side note, I've also tried doing a CInt() to the ProcessQuery with the same result.

Does anyone have any suggestions for what I may be able to try?

Edit: Here is the definition of ProcessQuery

Function ProcessQuery(icode As Integer, pb As ADODB.Recordset, TableName As String, sql$) As Integer

Edit 2: I couldn't tell you why this was done in this manner. I inherited the code base. Yikes...


Function ProcessQuery(icode As Integer, pb As ADODB.Recordset, TableName As String, sql$) As Integer
    ProcessQuery = ProcessQuery1(icode, pb, TableName, sql$)
End Function

Function ProcessQuery1(icode As Integer, pb As ADODB.Recordset, TableName As String, sql$) As Integer
''THIS IS THE ORIGINAL SQL CALL ROUTINE!
Dim STATUS As Integer
On Error GoTo ProcessSQLError

STATUS = 0
Select Case icode
   Case BCLOSE
        If pb.State  0 Then
            pb.Close
        End If
        Set pb = Nothing
        STATUS = 3
    Case BOPEN
        STATUS = 9
        Set pb = New ADODB.Recordset
    Case BOPENRO
        STATUS = 9
        Set pb = New ADODB.Recordset
    Case BGETEQUAL, BGETEQUAL + S_NOWAIT_LOCK, BGETGREATEROREQUAL, BGETGREATEROREQUAL + S_NOWAIT_LOCK
        If pb.State  0 Then
            pb.Close
            ''Set pb = Nothing
            ''Set pb = New ADODB.Recordset
        End If
        pb.Open sql$, MISCO_SQL_DB, adOpenDynamic, adLockOptimistic
        If Not pb.EOF Then
            pb.MoveFirst
        Else
            STATUS = 9
        End If
    Case BGET_LE, BGET_LE + S_NOWAIT_LOCK
        If pb.State  0 Then
            pb.Close
        End If
        pb.Open sql$, MISCO_SQL_DB, adOpenDynamic, adLockOptimistic
        If Not pb.BOF Then
            pb.MoveLast

        Else
            STATUS = 9
        End If
    Case BGETFIRST, BGETFIRST + S_NOWAIT_LOCK
        If pb.State  0 Then pb.Close
        sql = "select * from " + TableName
        If InStr(1, gblOrderBy, "ORDER BY") > 0 Then
            sql = sql + gblOrderBy
        Else
            sql = sql + " ORDER BY " + gblOrderBy
        End If

        gblOrderBy = ""
        pb.Open sql$, MISCO_SQL_DB, adOpenDynamic, adLockOptimistic
        If Not pb.EOF Then
                                                        pb.MoveFirst
        End If
    Case BGETLAST, BGETLAST + S_NOWAIT_LOCK
        If pb.State  0 Then
            pb.Close
        End If
        sql = "select * from " + TableName
        If InStr(1, gblOrderBy, "ORDER BY") > 0 Then
            sql = sql + gblOrderBy
        Else
            sql = sql + " ORDER BY " + gblOrderBy
        End If
        gblOrderBy = ""
        pb.Open sql$, MISCO_SQL_DB, adOpenDynamic, adLockOptimistic
        If Not pb.EOF Then
            pb.MoveFirst
            pb.MoveLast
        End If
    Case BGETNEXT, BGETNEXT + S_NOWAIT_LOCK:            pb.MoveNext
    Case BGETPREVIOUS, BGETPREVIOUS + S_NOWAIT_LOCK:    pb.MovePrevious
    Case B_UNLOCK
        ''need to add code here
    Case BINSERT
        If pb.State = 0 Then
            pb.Open TableName, MISCO_SQL_DB, adOpenDynamic, adLockOptimistic
        End If
    Case BDELETE
        STATUS = 8
        pb.Delete
    Case Else
        STATUS = 1
        MsgBox "STOP: UNDEFINDED PROCEDURE" + Str$(icode)
End Select
If STATUS = 0 Then
    If pb.EOF Or pb.BOF Then STATUS = 9
End If

ProcessQuery1 = STATUS
Exit Function

ProcessSQLError:
    MsgBox TableName + ": " + Error(Err), vbCritical, "Error "
    ProcessQuery1 = 9

End Function
A: 

Well, I can't say I know what's breaking, but here's a possible debugging step: Assign a locally defined integer first, then assign AICROSSDOCKStatus to the local int. If the run-time err 13 happens at the first assignment, something REALLY weird is going on - if it happens at the second, then you might want to see if any of your global variables are arrays that you might be overrunning bounds on.

Good luck!

Mike
A: 

Try msgbox ProcessQuery(iocode, pb, AICROSSDOCBOLFN, "") and see if the value turns out to be an int?

I am sure it won't be an int & that is the result it is failing. My guess is that, it will have come characters which makes it a non-numeric value.

You can try IsNumeric function on the results to check, if it is a numeric value.

shahkalpesh
A: 

Try using an intermediate Variant which should take any kind of type from your function. It should at least allow you to investigate the real type of the return value.

Toby Allen
+1  A: 

This will tell you the name of the type your function is actually returning:

MsgBox TypeName(ProcessQuery(...))

Confining the ProcessQuery() function to int might be helpful in any case, to prevent such errors right from the start:

Function ProcessQuery(whatever arguments) As Integer
  ''// ...
End Function
Tomalak
When I try this in ProcessQuery on the ProcessQuery1 I get the expected 'Integer'. When I try TypeName(ProcessQuery(...)) I get the error.
Chris
Then I guess the error occurs *within* ProcessQuery1(). Uncomment the "On Error Goto" statement to see where it is. By the way, the function ProcessQuery1() is... quite a mess, to put it mildly. The Type Mismatch is not at all the only problem it has.
Tomalak
You're not kidding about it having problems.It made it through ProcessQuery1, it has the error when the 'End Function' is reached in ProcessQuery.
Chris
Since ProcessQuery() and ProcessQuery1() seem to have the exact same signatures... Can you for the fun of it call ProcessQuery1() directly in your code?
Tomalak
Still getting the same error.
Chris
Tomalak
Tried changing it around to take care of that. I'm running this with the following params (BOPEN, <RecordSet>, <String>, ""). So it _should_ just goto BOPEN set the status to 9 give me a new RecordSet and return the integer. All the other cases work fine in returning the correct value.
Chris
Hm. By definition your functions cannot return anything but an Integer. It is just impossible that they don't. The assignment error must be inside the function body. Use the debugger to step through the function line by line if necessary.
Tomalak
A: 

It's a little unusual for an API to return a VB6 Integer, which is a 16-bit quantity. Fairly often, someone translating a C function prototype will confuse a c "int" with a VB6 Integer, when the equivalent is really a Long in VB6.

Jim Mack
A: 

The problem was not in the fact that it was having trouble with the returns from a function but the parameters that were being passed by reference. I needed a Recordset(pb) to pass by reference, but I had declared (pb) as a Record.

Chris
A: 

Thank you! You saved the day for me! :) The real answer indeed was that the mismatch occurs when passing a value as opposed to what you are getting back.

Mark