Error 13 type mismatch

schalkse

Registered User.
Local time
Today, 17:29
Joined
Apr 20, 2005
Messages
51
Hi all,
Can someone help me out with this code? Keep getting a type mismatch error in the function convert at the line "digit = Asc(temp(I)) > 47 And Asc(temp(I)) < 58"
Maybe ik keep overlooking something, dont know.

Thx in advance

Code:
strBar = "702000000036000063053309999000500"

valid = False
message = ""
prev = ""
l = 1
While l > 0
    message = getmsg(strBar)
    If message > "" Then
        convmess = convert(message)
        valid = False
        l = Len(message)
    End If
Wend


Public Function getmsg(message As String)

    While Not valid
        chars = mysplit(message)
        l = Len(message)
        If l > 0 Then
            valid = True
            For I = 1 To l
                If Asc(chars(I)) > 127 Then
                    valid = False
                    char = chars(I)
                    Exit For
                End If
            Next
            If Not valid Then
                MsgBox "Invalid character: " & char, vbCritical, "Error"
                prev = message
                message = ""
            End If
        Else
            valid = True
        End If
    Wend
    getmsg = message
End Function

Public Function convert(chars)

    ReDim chrs(l + 1)
    temp = chars
    Start = 104
    Mode = 0
    M = 1
    For I = 1 To l + 1
        sw = True
        If I < l + 1 Then
            digit = Asc(temp(I)) > 47 And Asc(temp(I)) < 58
            If (Mode = 0 And Not digit) Or (Mode = 1 And digit) Then sw = False
        End If
        If Mode = 0 Then
            If sw Then
                ind = I
                Mode = 1
            Else
                chrs(M) = Asc(temp(I)) - 32
                M = M + 1
            End If
        ElseIf sw Then
            If I - ind < 4 Then
                For j = 0 To I - ind - 1
                    chrs(M + j) = Asc(temp(ind + j)) - 32
                Next
                M = M + I - ind
                If I < l + 1 Then
                    chrs(M) = Asc(temp(I)) - 32
                    M = M + 1
                End If
            Else
                If ind = 1 Then
                    Start = 105
                Else
                    chrs(M) = 99
                    M = M + 1
                End If
                n = Int((I - ind) / 2)
                For j = 0 To n - 1
                    chrs(M + j) = 10 * temp(ind + 2 * j) + temp(ind + 2 * j + 1)
                Next
                M = M + n
                If I < l + 1 Then
                    chrs(M) = 100
                    M = M + 1
                End If
                If (I - ind) Mod 2 = 1 Then
                    chrs(M) = Asc(temp(I - 1)) - 32
                    M = M + 1
                End If
                If I < l + 1 Then
                    chrs(M) = Asc(temp(I)) - 32
                    M = M + 1
                End If
            End If
            Mode = 0
        End If
    Next
    Check = Start
    For I = 1 To M - 1
        Check = Check + chrs(I) * I
    Next
    ch = Chr((Check Mod 103) + 32)
    chrs(M) = Check Mod 103
    For I = 1 To M
        If chrs(I) < 96 Then
            chrs(I) = Chr(chrs(I) + 32)
        Else
            chrs(I) = Chr(chrs(I) + 97)
        End If
    Next
    convert = Chr(Start + 97) & Join(chrs, "") & Chr(203)
End Function
Function mysplit(text)

Dim myarray()
    ReDim myarray(Len(text))
    For I = 1 To Len(text)
        myarray(I) = Mid(text, I, 1)
    Next
    mysplit = myarray
End Function
 
The line

digit = Asc(temp(I)) > 47 And Asc(temp(I)) < 58

is not valid VBA. What are you trying to do here?

It starts off as an assignment statement and then changes to a conditional.

You don't seem to have declared any variables in the code fragment you have posted.

Make sure you have an "Option Explicit " at the start of each module. It saves wasting time looking for typing errors and you really should explicitly DIM each variable to make sure it is a suitable type.
 
All the variable are declared as public in a module. Havent included them.
It's a code to calculate and convert the value of strBar into Code128 barcode.

Have to admit that it originally comes from a VBA script.
 
Last edited:
To rep[eat

The line

digit = Asc(temp(I)) > 47 And Asc(temp(I)) < 58

is not valid VBA.

What are you trying to do with that line??

Is digit defined as a boolean? It helps us to help you if you share information.

If digit is a boolean then try

digit = (Asc(temp(I)) > 47) And (Asc(temp(I)) < 58)
 
Also assuming that the function "temp" returns a single-character string...
 
Yes, digit is defined as a boolean
The line checks if its a number between 0 and 9 (ascii between 47 and 58)

To be complete, here are my declared variables
Code:
Public valid As Boolean
Public message As String
Public prev As String
Public l As Variant
Public digit As Boolean
Public char As Variant
Public Barco As String
Public temp As Variant
Public chars As Variant
Public I As Single
Public start As Variant
Public mode As Variant
Public M As Variant
Public SW As Boolean
Public ind As Variant
Public j As Single
Public n As Single
Public check As Variant
Public ch As Variant
To rep[eat

The line

digit = Asc(temp(I)) > 47 And Asc(temp(I)) < 58

is not valid VBA.

What are you trying to do with that line??

Is digit defined as a boolean? It helps us to help you if you share information.

If digit is a boolean then try

digit = (Asc(temp(I)) > 47) And (Asc(temp(I)) < 58)
 
digit = Asc(temp(I)) > 47 And Asc(temp(I)) < 58

although this looks strange, it ought to work

{its a lot easier in pascal digit = temp(i) in ("0" .. "9") }

-------------
two possibilites

1.

perhaps its the symbol I thats the problem

that's declared as a single but ought to be integer or long in this usage, maybe

------------
2.

or maybe its temp(i) thats the problem

to slice a string in this way you would normally have mid(temp,I,1)

i dont thing you can take an array index of a variant in this way

--------------
I would try these two possibilities
 
Have declared I as integer now, but still the same error.
So it's temp(i) thats the problem.
Maybe i need to declare Temp as something else or?

Have tried what you suggested, now get the same error on this line:
Code:
chrs(M + j) = 10 * temp(ind + 2 * j) + temp(ind + 2 * j + 1)

although this looks strange, it ought to work

{its a lot easier in pascal digit = temp(i) in ("0" .. "9") }

-------------
two possibilites

1.

perhaps its the symbol I thats the problem

that's declared as a single but ought to be integer or long in this usage, maybe

------------
2.

or maybe its temp(i) thats the problem

to slice a string in this way you would normally have mid(temp,I,1)

i dont thing you can take an array index of a variant in this way

--------------
I would try these two possibilities
 
you can say temp(i) if temp is an array

but i dont think a variant can be an array - its just a single entity, so you cant say temp(i) - although you might be able to assign the variant to another variable, or typecast it to something else

cant you make temp a string, then use the mid function to slice it
 
If i do that, i get stuck on TEMP stating that a matrix is expected.

What do i need to change then in my code to make array's where needed?

you can say temp(i) if temp is an array

but i dont think a variant can be an array - its just a single entity, so you cant say temp(i) - although you might be able to assign the variant to another variable, or typecast it to something else

cant you make temp a string, then use the mid function to slice it
 
well I don't know hoew you are using TEMP

but you have defined temp as variant, and i don't think you can use the syntax temp(I) to return part of the variant - eg what if TEMP holds a number or a date - there is no I th position in a number. A variant also isnt an array, and the expression temp(i) is expecting to use an array (hence presumable the no matrix error ...)

access doesnt have a char datatype, but if temp is an array of chars i think you would have to declare it

temp() as string(1) 'ie an array of 1 char strings - or just
temp as string 'ie a single string

if the latter then you again can't use temp(I) - you have to use mid to slice the string
 

Users who are viewing this thread

Back
Top Bottom