1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Visual Basic Type Mismatch

Discussion in 'Programming & Webmastering' started by Dark_Webster, Jul 20, 2008.

  1. Dark_Webster

    Joined:
    Jul 29, 2007
    Messages:
    393 (0.12/day)
    Thanks Received:
    51
    Location:
    Portugal
    Hey guys, could you help me with a problem?

    I'm trying to make a VB program that generates lottery numbers. The thing is, it has a PIN textbox for when the right PIN is introduced, the button for randomize numbers becomes active.

    But when I delete the PIN, the program gives me Error 13 Type Mismatch:
    I think that if you delete the PIN, the program can't compare because the textbox has Nil value.

    Code:
    Private Sub PIN_Change()
    [COLOR="Lime"]If PIN = 5454 Then [/COLOR]Command1.Visible = True _
    Else: Command1.Visible = False
    Label1 = Nil
    Label2 = Nil
    Label3 = Nil
    Label4 = Nil
    Label5 = Nil
    Label6 = Nil
    Label7 = Nil
    End Sub
    The green text is where VB says where is the error. By the way, the DataFormat of the textbox is in Number.

    Someone can help me out?? I started to learn VB by my own iniciative.
     
    Last edited: Jul 20, 2008
    calvary1980 says thanks.
  2. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,546 (2.34/day)
    Thanks Received:
    1,384
    If PIN is a textbox you need to use PIN.text.
     
    calvary1980 and Dark_Webster say thanks.
    10 Year Member at TPU
  3. Dark_Webster

    Joined:
    Jul 29, 2007
    Messages:
    393 (0.12/day)
    Thanks Received:
    51
    Location:
    Portugal
    Thanks anyway, but it sill gives me the same error using PIN.Text. The only way that I'm capable of changing the PIN is to select the other already written and write the new one.
     
    calvary1980 says thanks.
  4. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,546 (2.34/day)
    Thanks Received:
    1,384
    if you do msgbox(pin.text) and run the sub, does it output anything?
     
    calvary1980 says thanks.
    10 Year Member at TPU
  5. Dark_Webster

    Joined:
    Jul 29, 2007
    Messages:
    393 (0.12/day)
    Thanks Received:
    51
    Location:
    Portugal
    Yes, it starts to output the numbers that i write into the textbox into a messagebox.
     
    calvary1980 says thanks.
  6. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,546 (2.34/day)
    Thanks Received:
    1,384
    Turn it into

     
    calvary1980 and Dark_Webster say thanks.
    10 Year Member at TPU
  7. Dark_Webster

    Joined:
    Jul 29, 2007
    Messages:
    393 (0.12/day)
    Thanks Received:
    51
    Location:
    Portugal
    Sorry to say this, but that didn't worked as well. What I think is that if Pin.Text reaches Nil value, it will cause this error.
     
    calvary1980 says thanks.
  8. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,546 (2.34/day)
    Thanks Received:
    1,384
    Actually, that could be the issue

    change it to

    Of course if you leave the msgbox out it'll just go on not saying a thing. Personally I like to make a big multiline textbox and name it errorbox or something similar and output errors to it. So I can always look back at things that went wrong.
     
    calvary1980 and Dark_Webster say thanks.
    10 Year Member at TPU
  9. Dark_Webster

    Joined:
    Jul 29, 2007
    Messages:
    393 (0.12/day)
    Thanks Received:
    51
    Location:
    Portugal
    It now gives me an error refering to "Private Sub PIN_Change()" : Sub or function not defined

    Also i'm using VB6 (yes i know its a bit old)
     
    calvary1980 says thanks.
  10. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,546 (2.34/day)
    Thanks Received:
    1,384
    What are you developing in? If it's VS send me your program.
     
    calvary1980 says thanks.
    10 Year Member at TPU
  11. Dark_Webster

    Joined:
    Jul 29, 2007
    Messages:
    393 (0.12/day)
    Thanks Received:
    51
    Location:
    Portugal
    I'm developing this in VB6, because I thought that this one would be the best for start programming in VB.
     
    calvary1980 says thanks.
  12. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,546 (2.34/day)
    Thanks Received:
    1,384
    I'd recommend VS, offers everything all other .NET languages do and keeps the simplicity of VB. It's a shame that when you understand VB you have to adapt to another platform to do more with it.

    If I'd open your application it would just try to convert it, hence not helping you in any way :)
     
    calvary1980 and Dark_Webster say thanks.
    10 Year Member at TPU
  13. Dark_Webster

    Joined:
    Jul 29, 2007
    Messages:
    393 (0.12/day)
    Thanks Received:
    51
    Location:
    Portugal
    Ok, thanks a lot anyway. I'm going to see if I can do something this error anyway, and what VS do you recommend: 2005 or 2008?
     
    calvary1980 says thanks.
  14. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,546 (2.34/day)
    Thanks Received:
    1,384
    I use 2005, not sure what 2008 offers though.

    Type mismatch means that you're trying to convert some variable in an impossible way, ie string with text to number and the likes. Nil might cause this, and apparently VB6 doesn't understand try/catch that well. Does it have a function like notnull() to catch the error?
     
    calvary1980 says thanks.
    10 Year Member at TPU
  15. Oliver_FF

    Oliver_FF New Member

    Joined:
    Oct 15, 2006
    Messages:
    546 (0.15/day)
    Thanks Received:
    65
    Code:
    If PIN.text = [B]"[/B]5454[B]"[/B] Then
    
    ;)

    You can't compare a String with a number :cool:
     
    calvary1980 says thanks.
  16. smartali89

    smartali89

    Joined:
    Dec 1, 2007
    Messages:
    528 (0.16/day)
    Thanks Received:
    13
    Location:
    Karachi, Pakistan
    Private Sub PIN_Change()

    If PIN.Text = "5454" Then
    Command1.Visible = True
    Else
    Command1.Visible = False
    Label1.Caption = ""
    Label2.Caption = ""
    Label3.Caption = ""
    Label4.Caption = ""
    Label5.Caption = ""
    Label6.Caption = ""
    Label7.Caption = ""
    End If
    End Sub

    Label in VB6 doesn't has the property "Text"
     
    calvary1980 says thanks.
  17. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,546 (2.34/day)
    Thanks Received:
    1,384
    Another reason to use vb.net :)
     
    calvary1980 says thanks.
    10 Year Member at TPU
  18. Oliver_FF

    Oliver_FF New Member

    Joined:
    Oct 15, 2006
    Messages:
    546 (0.15/day)
    Thanks Received:
    65
    Label's have Caption, TextBox's have Text :toast:

    It's terrible I remember all this, haven't written any VB for years and years - it was my first language :laugh:
     
    calvary1980 says thanks.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)