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

    Mismatched type? Nz not changing null to different type?

    Hi, Thanks for taking the time to help!
    I'm not sure what I'm doing wrong here. This is my code:


    Code:
    Private Sub DhrScanTxt_Enter()
    
    Dim Status As String
    Debug.Print Me.DhrScanTxt
    If (Me.DhrScanTxt.Value = Empty) Then Me.DhrScanTxt.Value = ""
    Debug.Print Me.DhrScanTxt
    Status = Nz(Me.DhrScanTxt.Value, "Error Null")
    Debug.Print Status
    Debug.Print Me.DhrScanTxt
    
    
    Me.Text26 = Status
    
    
    Dim SoNum As Long
    SoNum = CLng(Left(Status, 6))
    Me.SoNumTxt = SoNum
    
    
    Dim DhrNum As Long
    DhrNum = Right(Left(Status, 9), 2)
    Me.DhrNumTxt = DhrNum
    
    
    Dim StepNum As Long
    StepNum = Right(Status, 3)
    Me.StepNumTxt = StepNum
    
    
    Me.DhrScanTxt.SetFocus
    
    
    End Sub
    Everytime I open the form the field is null so I was trying to replace it with an empty string so I wouldn't get the error "Invalid use of null" but now I get mismatched type.
    The rest of the system works but as you can see in SoNum I am trying to take part of a string and convert it to a long but I keep getting type mismatch when I use Nz.

    If I do this:
    Code:
    Dim Status As String
    If (Me.DhrScanTxt.Value = Null) Then Me.DhrScanTxt.Value = ""
    Status = Me.DhrScanTxt.Value
    
    
    Me.Text26 = Status
    
    
    Dim SoNum As Long
    SoNum = CLng(Left(Status, 6))
    Me.SoNumTxt = SoNum
    Then I get invalid use of Null.

    I'm not sure what I'm doing wrong.

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You need to use the IsNull() syntax

    If IsNull(Me.DhrScanTxt) Then Me.DhrScanTxt = ""

    In the database world nothing actually = Null .
    Even Null = Null evaluates as false.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  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 Minty View Post
    You need to use the IsNull() syntax

    If IsNull(Me.DhrScanTxt) Then Me.DhrScanTxt = ""

    In the database world nothing actually = Null .
    Even Null = Null evaluates as false.
    I thought that is what the Nz function did?

    Also I am still getting a type mismatch?
    Code:
    Private Sub DhrScanTxt_Enter()
    
    Dim Status As String
    If IsNull(Me.DhrScanTxt) Then Me.DhrScanTxt = ""
    Status = Me.DhrScanTxt.Value
    
    
    Me.Text26 = Status
    
    
    Dim SoNum As Long
    SoNum = CLng(Left(Status, 6))
    Me.SoNumTxt = SoNum
    
    
    Dim DhrNum As Long
    DhrNum = Right(Left(Status, 9), 2)
    Me.DhrNumTxt = DhrNum
    
    
    Dim StepNum As Long
    StepNum = Right(Status, 3)
    Me.StepNumTxt = StepNum
    
    
    Me.DhrScanTxt.SetFocus
    
    
    End Sub

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Which line are you getting the Type Mismatch on?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    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 Minty View Post
    Which line are you getting the Type Mismatch on?
    This one:
    Code:
    SoNum = (Left(Status, 6))

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I have a funny feeling Status might be a reserved word, either change it to strStatus or put it in square brackets.
    Personally I would recommend the former...
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  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 Minty View Post
    I have a funny feeling Status might be a reserved word, either change it to strStatus or put it in square brackets.
    Personally I would recommend the former...
    I am still getting the same error in the same spot. I'm very confused haha
    Code:
    Private Sub DhrScanTxt_Enter()
    
    Dim strStatus As String
    If IsNull(Me.DhrScanTxt) Then Me.DhrScanTxt = ""
    strStatus = Me.DhrScanTxt.Value
    
    
    Me.Text26 = strStatus
    
    
    Dim SoNum As Long
    SoNum = CLng((Left(strStatus, 6)))
    Me.SoNumTxt = SoNum
    
    
    Dim DhrNum As Long
    DhrNum = Right(Left(strStatus, 9), 2)
    Me.DhrNumTxt = DhrNum
    
    
    Dim StepNum As Long
    StepNum = Right(strStatus, 3)
    Me.StepNumTxt = StepNum
    
    
    Me.DhrScanTxt.SetFocus
    
    
    End Sub

  8. #8
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I would ensure the value is what you think it is

    Dim SoNum As Long

    Debug.print CLng(Left(strStatus, 6))

    SoNum = CLng(Left(strStatus, 6))
    Me.SoNumTxt = SoNum
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  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 Minty View Post
    I would ensure the value is what you think it is

    Dim SoNum As Long

    Debug.print CLng(Left(strStatus, 6))

    SoNum = CLng(Left(strStatus, 6))
    Me.SoNumTxt = SoNum
    So I did this and I now get type mismatch on the debug.pring haha
    Code:
    Dim SoNum As Long
    Debug.Print CLng(Left(strStatus, 6))
    SoNum = CLng((Left(strStatus, 6)))
    Me.SoNumTxt = SoNum

  10. #10
    Vita's Avatar
    Vita is offline Competent Performer
    Windows 10 Access 2002
    Join Date
    May 2022
    Location
    Massachusetts, USA
    Posts
    296
    I even went as far as doing this:
    Code:
    Dim strStatus As String
    If IsNull(Me.DhrScanTxt) Then Me.DhrScanTxt = ""
    strStatus = Me.DhrScanTxt.Value
    
    
    Me.Text26 = strStatus
    
    
    Dim SoNum As Long
    Debug.Print Me.DhrScanTxt.Value
    Debug.Print strStatus
    Debug.Print Left(strStatus, 6)
    Debug.Print CLng(Left(strStatus, 6))
    SoNum = CLng(Left(strStatus, 6))
    Me.SoNumTxt = SoNum
    And none of them output a result. Maybe its because it is being set to an empty string but converting that to a long should set it to 0 right?

  11. #11
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Nope - try it in the immediate window. Press ctrl + G then type

    ? Clng("")

    You'll get a type mismatch error
    The ? in the immediate window means Print.

    CLng expects something in string from it can convert to a number. A ZLS doesn't represent any number so it errors.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    You are not using NZ() though.?

    Edit: Instead of this Right(Left()) coding, there is a MID() function?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #13
    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 Welshgasman View Post
    You are not using NZ() though.?

    Edit: Instead of this Right(Left()) coding, there is a MID() function?
    Is there? And I was using Nz but it was giving the same result. I will have to add an if before the SoNum to check for ZLS and convert it to a 0 or make it just go to end of sub.
    Thank you again. You are extremely helpful and nice! I will look into the MID() Function and try using Nz. I am still learning lots but I've definitely grown haha.

  14. #14
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    or make it just go to end of sub
    Certainly. Why try to process data that you know is bad?

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,943
    Quote Originally Posted by Vita View Post
    Is there? And I was using Nz but it was giving the same result. I will have to add an if before the SoNum to check for ZLS and convert it to a 0 or make it just go to end of sub.
    Thank you again. You are extremely helpful and nice! I will look into the MID() Function and try using Nz. I am still learning lots but I've definitely grown haha.
    Look at the Val() function
    Code:
    tt=""
    ? val(tt)
     0
    NZ ONLY ​ works if the value is actually Null, not a ZLS.
    Better to actually validate the data though, unless leaving empty is actually allowed?
    In which case, just test for it and skip when empty?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. ODBC Connection is Changing My Data Type
    By Enrightt in forum Access
    Replies: 3
    Last Post: 02-08-2018, 06:03 PM
  2. Changing the join type
    By SunTop in forum Queries
    Replies: 3
    Last Post: 09-28-2016, 12:00 PM
  3. Changing data type with linked file
    By suprdave in forum Import/Export Data
    Replies: 2
    Last Post: 01-20-2016, 10:37 AM
  4. changing multiple fields type to double
    By neefbeef in forum Access
    Replies: 1
    Last Post: 12-07-2014, 03:20 AM
  5. Changing Data Type of Imported Field Name
    By rhubarb in forum Import/Export Data
    Replies: 12
    Last Post: 06-16-2014, 08:20 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