i cant solve my run-time error '13' type mismatch

  • 13 years ago

    i'm a new to VB6. i encountered this problem while trying to run this program. a pop-out comes out saying "Run-time error '13': type mismatch. i really need help for this... please help... thanks


    here is the program codes:


    Option Explicit


    Dim k As Integer                    ' graph x-axis counter
    Dim intInput As Integer             ' accelerometer output 1
    Dim intInputOLD As Integer          ' accelerometer output 1 one sample earlier
    Dim intInput1 As Integer            ' accelerometer output 2
    Dim intInputOLD1 As Integer         ' accelerometer output 2 one sample earlier
    Dim plotvel As Integer
    Dim plot
    velOLD As Integer
    Dim plotdist As Integer
    Dim plot
    distOLD As Integer
    Dim plotYvel As Integer
    Dim plotY
    velOLD As Integer
    Dim plotYdist As Integer
    Dim plotY
    distOLD As Integer
    Dim gvaluex As Integer
    Dim gvaluey As Integer
    Dim displacement As Double
    Dim displacementold As Double
    Dim vel
    old As Single
    Dim velnew As Single
    Dim acc
    old As Single
    Dim accnew As Single
    Dim xaxis
    a As Integer
    Dim yaxisa As Integer
    Dim xaxiss As Integer
    Dim yaxiss As Integer
    Dim displacementY As Double
    Dim displacementY
    old As Double
    Dim velYold As Single
    Dim velY
    new As Single
    Dim accYold As Single
    Dim accY
    new As Single
    Dim switchaxis As Integer


    Private Sub cmdSend_Click()


    Dim PinNumber As Long
    Dim PinState As Long


    ' Send Out Data
    ' MSComm1.Output = Chr$(255) & Chr$(PinNumber) & Chr$(PinState)


    End Sub



    Private Sub Form_Load()


    ' expect max 8 chars from BasicStamp
       MSComm1.RThreshold = 8              'fire Rx event every 8 bytes
                                       
       MSComm1.InputLen = 8                'input 8 bytes at a time
       Text1.Refresh
       DrawGrid_Click
       
    '  Use COM1
    MSComm1.CommPort = 1


    ' 9600 baud, no parity, 8 data bits, 1 stop bit
    MSComm1.Settings = "9600,N,8,1"


    ' Open the port
    MSComm1.PortOpen = True


    End Sub


    'close the COM port when our project is unloaded


    Private Sub Form_Unload(Cancel As Integer)


    MSComm1.PortOpen = False


    End Sub


           
    Private Sub DrawGrid_Click()
    Dim x As Integer
       
    ' Draw grid for graph


       picBox.Cls                                  'clears the space for graph
       picBox.DrawStyle = 2                        'type of line
       picBox.Scale (0, 6000)-(1000, 3000)         'defines of the graphs [X1][Y1][X2][Y2]
       picBox.ForeColor = vbGreen                  'colour of the grids
       picBox.FillColor = vbWhite
       
       Form1.Picture1.Cls                          'clears Form1 display
       Form1.Picture1.DrawStyle = 2                'type of drawing
       Form1.Picture1.Scale (0, 6000)-(1000, 3000) 'defines of the graphs [X1][Y1][X2][Y2]
       Form1.Picture1.ForeColor = vbMagenta
       Form1.Picture1.FillColor = vbWhite
       
       For x = 0 To 5000 Step 100                  'division of X-axis
       picBox.Line (x, 3000)-(x, 6000)
       Next x
       
       For x = 3000 To 6000 Step 100               'division of Y-axis
       picBox.Line (0, x)-(5000, x)
       Next x
       
    End Sub


    Private Sub Interval_Timer()


    Call picBox_Click
    ' timer to perform input sampling


    End Sub


    Private Sub MSComm1_OnComm()


    Dim strInput As String                      'holds the incoming data


    Dim k As Integer                            'holds k value
    Dim period As Integer                       'holds period value


    'Const intMax As Integer = 6150


    ' If comEvReceive Event then get data and display
    If MSComm1.CommEvent = comEvReceive Then
       
       strInput = MSComm1.Input                'get data
       intInput = CInt(Mid(strInput, 1, 4))    'retrieve x-axis input from STAMP
       intInput1 = CInt(Mid(strInput, 5, 4))   'retrieve y-axis input from STAMP
         
    If (intInput < 5020) And (intInput > 4950) Then
        intInput = 5000                        'stablising input of X-axis at 5000
        End If
       
     If (intInput1 < 5020) And (intInput1 > 4950) Then
        intInput1 = 5000                       'stablising input of y-axis at 5000
        End If
     gvaluex = ((intInput / 10) - 500) * 8            ' calc 1/1000 g
                                                      ' calculation of X-axis
     
     gvaluey = ((intInput1 / 10) - 500) * 8           ' calculation of Y-axis
        Text3.Text = gvaluex / 1000
        Text4.Text = gvaluey / 1000
         
         Text1.Text = intInput
         Text2.Text = intInput1
    '   Line1.Y2 = (intInput - 3000) * 2
     '  Line1.X1 = (intInput1 - 4000) * 2
      ' Shape1.Top = (intInput - 3000) * 2
     '  Shape1.Left = (intInput1 - 4000) * 2
       
       
     '  picBox.ForeColor = vbBlack
      ' picBox.Line -(k, intInput)
               
               
               
      End If


    End Sub


    Private Sub NewFormm_Click()


    Dim x As Integer
       For x = 0 To 1000 Step 100
       Form1.Picture1.Line (x, 3000)-(x, 6000)
       Next x
       
       For x = 3000 To 6000 Step 200
       Form1.Picture1.Line (0, x)-(1000, x)
       Next x


      Form1.Picture1.Line (k, plotdistOLD)-(k + 1, plotdist)
    Form1.Show


    End Sub


    Private Sub picBox_Click()
    Dim x As Integer
    Dim period As Integer
    Dim shape1top As Integer
    Dim shape1left As Integer


    period = Interval.Interval


    accnew = gvaluex / 1000                                    ' Read accelerometer inputs
    accY
    new = gvaluey / 1000
    velnew = velold + (accold + accnew) * (period / 2000)   ' Calculate velocity
    plotvel = velnew * 5000 + 4200                            ' Plot velocity
    If (accnew < 0.01) And (velnew < 0.01) Then
    velnew = 0
    'vel
    old = 0
    End If


    If (velnew > -0.1) And (accnew = 0) Then
    'velnew = 0
    'vel
    old = 0
    End If


    displacement = displacementold + (velold + velnew) * (period / 2000) ' Calculate Displacement
    plot
    dist = displacement * 15000 + 3500
    accold = accnew
    velold = velnew
    displacementold = displacement
    Text6.Text = displacement * 1000
    Text7.Text = acc
    new
    Text8.Text = vel_new


    ' Do the same as above but for Y accelerometer


    velYnew = velYold + (accYold + accYnew) * (period / 2000)
    plotYvel = velYnew * 5000 + 4200
    If (accYnew < 0.01) And (velYnew < 0.01) Then
    velYnew = 0
    'vel
    old = 0
    End If


    If (velYnew > -0.1) And (accYnew = 0) Then
    'velnew = 0
    'vel
    old = 0
    End If
    displacementY = displacementYold + (velYold + velYnew) * (period / 2000)
    plotY
    dist = displacementY * 15000 + 3500
    accYold = accYnew
    velYold = velYnew
    displacementY_old = displacementY





    picBox.ForeColor = vbBlack
       picBox.DrawStyle = 1
       If k = 0 Then
       picBox.Line (0, intInput)-(0, intInput)
      ' picBox.Line (0, intInput1)-(0, intInput1)
       picBox.Line (0, plotvel)-(0, plotvel)
       picBox.Line (0, plotdist)-(0, plotdist)
       intInputOLD = intInput
       intInputOLD1 = intInput1
       plotvelOLD = plotvel
       plotdistOLD = plotdist
       End If
       
       picBox.Line (k, intInputOLD)-(k + 1, intInput)
       intInputOLD = intInput
    '   picBox.Line (k, intInputOLD1)-(k + 1, intInput1)
       intInputOLD1 = intInput1
       picBox.Line (k, plotvelOLD)-(k + 1, plotvel)
       plotvelOLD = plotvel
       picBox.Line (k, plotdistOLD)-(k + 1, plotdist)


       plotdistOLD = plotdist
       
       k = k + 1
       If k = 1000 Then
       Call DrawGrid_Click
       k = 0
       End If
       
       Line1.Y2 = (intInput - 3000) * 2
       Line1.X1 = (intInput1 - 4000) * 2
       
     


     '  yaxisa = 4000 + (intInput - 4800) * 5
      ' xaxis
    a = 4000 + (intInput1 - 4800) * 5
       
       yaxiss = (intInput - 4000) * 3
       xaxiss = (intInput1 - 4000) * 4.5
       Shape1.FillColor = vbRed
         
       Shape1.Top = xaxiss
       Shape1.Left = displacement * 10000000 - 1500
       
       shape1top = displacementY * 1000000
       shape1left = displacement * 1000000
       
       If shape1top > 7500 Then shape1top = 7500
       If shape1left > 10000 Then shape1left = 10000
       Form1.Shape1.Top = shape1top
       Form1.Shape1.Left = shape1left
       
    Text3.Text = Shape1.Top
    Text4.Text = Shape1.Left


    End Sub


    Private Sub picGraph_Click()


    SavePicture picGraph.Image, "GraphPicture.bmp"


    End Sub


    Private Sub StartClick()
       
        Call DrawGrid
    Click
      k = 0
        Line1.Visible = True
        Interval.Enabled = True
    End Sub


    Private Sub Stop_Click()


    Interval.Enabled = False
    displacement = 0
    displacementold = 0
    vel
    old = 0
    velnew = 0
    acc
    old = 0
    accnew = 0
    displacementY = 0
    displacementY
    old = 0
    velYold = 0
    velY
    new = 0
    accYold = 0
    accY
    new = 0
    End Sub


    Private Sub SampleTimeChange()
    Text5.Text = Str(SampleTime.Value) * 10
    Interval.Interval = SampleTime.Value * 10
    End Sub
    Private Sub SampleTime
    Scroll()
    SampleTime_Change
    End Sub


    Private Sub xaxis_Click()
    switchaxis = 1
    End Sub


    Private Sub yaxis_Click()
    switchaxis = 2
    End Sub


    Private Sub both_Click()
    switchaxis = 0
    End Sub


    '   A noise filter
    '        if ABS(X[n]-X[n-1])>k or (ABS(X[n]-X[n+1])>k
    '               then X[n] = (X[n-1]+X[n+1])/2
    '
    Private Sub Text7_Change()
    'Interval.Interval = Text7.Text
    'If Text7.Text = 0 Then
    'Text7.Text = 10
    'End If
    End Sub



    thanks again

  • 13 years ago
    Place a breakpoint on all lines of code that contain type coercions.
    For exampleCode:
    intInput = CInt(Mid(strInput, 1, 4))
    will raise a "Type Mismatch" error if the first four characters of strInput do not represent an integer value.

    Trevor
  • 13 years ago
    i inserted a breakpoint but still the same message pops out... what is actually causing the problem? is it using a wrong function or something close to that?


    thanks
    drew
  • 13 years ago

    Drew,


    The error is caused by trying to assign a value of the wrong type to a variable.


    e.g.
    If intX is an integer variable then
    intX = Cint("1") is OK but intX = Cint("One") will fail.


    When the error occurs, press the Debug button to show which line is at fault then position the mouse pointer over each variable to see what their respective values are.


    Post back the offending line and variable values if you require more assistance.


    Trevor

  • 12 years ago

    your error could be occurring int the lines
    where you have cint(mid(...))
    just to be on the safe side, you could probably add the Val Function around the mid function
    like:


    cint(val(mid(...)))


    hope this helps

  • 12 years ago
    Private Sub MSComm1_oncomm() 'receive data from the serial port

    Dim strInput As String

    Dim k As Integer
    Dim period As Integer

    If MSComm1.CommEvent = comEvReceive Then

    strInput = MSComm1.Input

    intInput = CInt(Val(Mid(strInput, 1, 4))) 'retrieve x-axis input from STAMP

    intInput1 = CInt(Val(Mid(strInput, 5, 4))) 'retrieve y-axis input from STAMP

    Question:
    thanks for helping me out... the error 13 is gone but the value i got from my accelerometer is not what it should be.. i should be getting  around 5000 as my output but i'm getting 200? is there anyway to rectify the problem... thanks in advance
  • 12 years ago

    are you sure you got the underlined integers right?


    Dim strInput As String
    Dim k As Integer
    Dim period As Integer
       If MSComm1.CommEvent = comEvReceive Then
           strInput = MSComm1.Input
           intInput = CInt(Val(Mid(strInput, 1, 4))) 'retrieve x-axis input from STAMP
           intInput1 = CInt(Val(Mid(strInput, 5, 4))) 'retrieve y-axis input from STAMP


    I, myself, don't know anything about the mscomm control, but I do know about the Mid Functions and stuff.


    one more thing, from my experience, you can only put a number up to 255 in a type integer varible, Try changing the type to long.

  • 12 years ago
    hi...

    what i was trying to do is to retrieve the 1st byte and the 5th byte of my input which is coming in from my basic stamp... and after that display it on VB6... i'm not sure if my MSComm codes are right but they were working about a week back...

    Question:
    is there anyone who knows where my problem lies...? currently when i debug my program, it does not display any errors....



    thanks loads
    drew

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic