Results 1 to 11 of 11
  1. #1
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296

    Question Confusing type mismatch

    Hi I have this code below that is supposed to check if the SONum that was input starts with a 0 but I get a type mismatch and I am not sure why.

    Code:
    Dim ZeroCheck As String: ZeroCheck = Left(SONum, 1) = "0"
    Select Case Len(SONum)
        Case 6
            If ZeroCheck Then
                SO_Err = True 'MsgBox ("Invalid Sales Order." & vbCrLf & "Please check that you didn't miss a number")
                GoTo ERR_InvalidSONum
            ElseIf Not ZeroCheck Then
                SONum = Format(SONum, "0000000")
                GoTo FormatCorrect
            Else
                MsgBox "How did you get here? Contact and admin and tell them"
                SO_Err = True
                GoTo ERR_InvalidSONum
            End If
        Case 7
            If ZeroCheck Then
                GoTo FormatCorrect
            ElseIf Not ZeroCheck Then
                SO_Err = True 'MsgBox ("Invalid Sales Order." & vbCrLf & "Please check if you accidentally double typed a number")
                GoTo ERR_InvalidSONum
            Else
                SO_Err = True 'MsgBox "How did you get here? Contact and admin and tell them"
                GoTo ERR_InvalidSONum
            End If
        Case Else
            SO_Err = True 'MsgBox ("Invalid Sales Order." & vbCrLf & "Too many numbers in the first part")
            GoTo ERR_InvalidSONum
    End Select
    I even tried changing the ZeroCheck around and I get the below results
    Code:
    ZeroCheck = "Left(SONum, 1) = ""0""" '(Line 4 mismatch)
    
    
    ZeroCheck = Left(SONum, 1) = "0" '(Line 7 mismatch)
    If I change line 7 to this it works which is fine. I just want to understand what I am doing wrong so I can learn for the future.
    Code:
    ElseIf ZeroCheck = False Then
    Thanks for helping me understand!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    numbers cannot be assigned/compared to strings.

    false is a number. 0
    "0" is a string

  3. #3
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by ranman256 View Post
    numbers cannot be assigned/compared to strings.

    false is a number. 0
    "0" is a string
    I'm sorry but I am still confused.

    If I use [ZeroCheck = Left(SONum, 1) = "0"] then I am comparing "0" (a string) to SONum (a string) right?
    but then on line 7 I try to see if the expression is not true. My understanding is that since say for example 123456 does not start with 0 the expression would return false(boolean) which would then get inverted by the Not making the expression true.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    What exactly is SONum? Give us a few examples.
    After testing, I recommend you:
    Change this line (from String to Boolean)
    Dim ZeroCheck As Boolean: ZeroCheck = Left(sonum, 1) = "0"

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    This syntax is used for True and False, which are text equivalents of Boolean values -1 and 0 respectively.
    If ZeroCheck Then

    You've declared ZeroCheck as a string. I think that's your issue but quite difficult to grasp which lines you're referring to as the ones that raise an error.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by orange View Post
    What exactly is SONum? Give us a few examples.
    SONum is just a string of numbers.


    The select case is setup to only catch strings in these 4 formats
    "123456" (6 numbers)
    "012345" (6 numbers beginning with 0)
    "1234567" (7 numbers)
    "0123456" (7 numbers beginning with 0)

  7. #7
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by orange View Post
    What exactly is SONum? Give us a few examples.
    After testing, I recommend you:
    Change this line (from String to Boolean)
    Dim ZeroCheck As Boolean: ZeroCheck = Left(sonum, 1) = "0"
    This seems to have fixed it. I am guessing since it was defined as a string it was inputting that expression in the first If, then on the ElseIf since it was returning false it was storing the "False" as a string since that is what it was told to do.
    Swapping it to boolean made it store the result of that expression rather than the expression itself. Which is what I was going for. Thank you all for your patience!

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    So the numbers are string data type thus they're not number data type? If that's incorrect, your use of the word string will confuse people. If you have a "series" of numbers they can still be string data or number data. If you know exactly what the data type of the field is, that should help.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    Quote Originally Posted by Micron View Post
    So the numbers are string data type thus they're not number data type? If that's incorrect, your use of the word string will confuse people. If you have a "series" of numbers they can still be string data or number data. If you know exactly what the data type of the field is, that should help.
    They are a string type. SONum is only a numerical part of a larger string. I just keep it a string so I can use it to print a final result accurately.

  10. #10
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,803
    Swapping it to boolean made it store the result of that expression rather than the expression itself.
    I don't think so. You were getting "False" or "True" not False or True (0 or -1). So even if the string starts with zero, this will never be True but it will be "True"
    If ZeroCheck Then

    True is what you are testing for by default.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I think we are seeing only parts of a bigger picture. Maybe Vita wants a different result???
    I set up some step-thru tests using the supplied code.

    when using sonum = "3123459"
    I noted that ZeroCheck = Left(sonum, 1) = "0" was "False".
    And I got a datatype issue when I got to ElseIf Not ZeroCheck
    So, my solution was to make ZeroCheck a boolean, and then it all worked for my tests.
    Zerocheck was True or False.
    I did try other SONum values including leading "0".

    Code:
    Sub vita()
    Dim so_err As Boolean
    Dim sonum As String: sonum = "3123459"
    Dim ZeroCheck As Boolean: ZeroCheck = Left(sonum, 1) = "0"
    Select Case Len(sonum)
        Case 6
            If ZeroCheck Then
                so_err = True 'MsgBox ("Invalid Sales Order." & vbCrLf & "Please check that you didn't miss a number")
                GoTo ERR_InvalidSONum
            ElseIf Not ZeroCheck Then
                sonum = Format(sonum, "0000000")
                GoTo formatcorrect
            Else
                MsgBox "How did you get here? Contact and admin and tell them"
                so_err = True
                GoTo ERR_InvalidSONum
            End If
        Case 7
            If ZeroCheck Then
                GoTo formatcorrect
            ElseIf Not ZeroCheck Then
                so_err = True 'MsgBox ("Invalid Sales Order." & vbCrLf & "Please check if you accidentally double typed a number")
                GoTo ERR_InvalidSONum
            Else
                so_err = True 'MsgBox "How did you get here? Contact and admin and tell them"
                GoTo ERR_InvalidSONum
            End If
        Case Else
            so_err = True 'MsgBox ("Invalid Sales Order." & vbCrLf & "Too many numbers in the first part")
            GoTo ERR_InvalidSONum
    End Select
    formatcorrect:
    ERR_InvalidSONum:
    End Sub
    Last edited by orange; 01-03-2023 at 07:15 PM. Reason: spelling

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Intermittant #Type! type mismatch error.
    By Ranger351w in forum Programming
    Replies: 4
    Last Post: 09-27-2020, 07:53 PM
  2. Type Mismatch
    By Alex Motilal in forum Programming
    Replies: 5
    Last Post: 02-04-2017, 05:57 PM
  3. Type 13 (Type Mismatch) error
    By sdel_nevo in forum Programming
    Replies: 5
    Last Post: 01-22-2016, 10:01 AM
  4. type mismatch
    By seeker63 in forum Programming
    Replies: 2
    Last Post: 12-05-2013, 02:54 PM
  5. Type Mismatch
    By Alex Motilal in forum Programming
    Replies: 10
    Last Post: 02-13-2011, 05:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums