Results 1 to 13 of 13
  1. #1
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2003
    Join Date
    Sep 2013
    Posts
    287

    Invalid use of null?

    Hey guys.



    I can't figure out what is causing the error in this code.

    Code:
    Public Function NewCode(ByVal EmpCode As String) As Variant
    
    
    Dim nums As String
    Dim lefttwo As String
    Dim rightthree As String
    Dim newlet As String
    
    
    
    
    nums = "0" & Left(Mid(EmpCode, 3), 4)
    lefttwo = Left(nums, 2)
    rightthree = Right(nums, 3)
    
    
    newlet = DLookup("Letter", "NumToAlpha", "Number = '" & lefttwo & "'")
    
    
    NewCode = newlet & rightthree
    
    
    End Function
    I added the ByVal because I was getting a ByRef argument type mismatch when I would call the function.

    This function takes RL71730, drops the RL and the last 0, adds a 0 at the beginning, and converts the 07 to a letter from the table, and concatenates that letter with the last 3 digits. Final result: H173.
    Click image for larger version. 

Name:	Screen Shot 2015-05-12 at 12.21.00 PM.png 
Views:	8 
Size:	40.1 KB 
ID:	20657Click image for larger version. 

Name:	Screen Shot 2015-05-12 at 12.21.08 PM.png 
Views:	8 
Size:	16.7 KB 
ID:	20658

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Look at your last two lines, "call newcode..." and "?Dlookup...".
    You need to have the value RL71730 enclosed in double-quotes. Otherwise, it is treated as a variable or a field, and not the literal value "RL71730".

  3. #3
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2003
    Join Date
    Sep 2013
    Posts
    287
    That errors out too:

    Click image for larger version. 

Name:	Screen Shot 2015-05-12 at 12.45.35 PM.png 
Views:	10 
Size:	10.1 KB 
ID:	20659

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    And the immediate cause of your error is that a String variable can't take a Null, which is what your DLookup() is returning. Joe has you on the path to fixing that.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2003
    Join Date
    Sep 2013
    Posts
    287
    That DLookup line you mentioned was me trying to find the error. Quotes didn't fix that either.

  6. #6
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2003
    Join Date
    Sep 2013
    Posts
    287
    pbaldy, that was another issue. Why is that dlookup returning a null? 07 clearly corresponds to "H" in the table.

    Click image for larger version. 

Name:	Screen Shot 2015-05-12 at 12.48.53 PM.png 
Views:	10 
Size:	6.0 KB 
ID:	20660

  7. #7
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You need to have the value RL71730 enclosed in double-quotes
    not
    Code:
    call newcode('RL71730')
    but rather:
    Code:
    call newcode("RL71730")

  8. #8
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2003
    Join Date
    Sep 2013
    Posts
    287
    Ah. Ok that eliminated the error, but I am getting a null result. Still can't figure out why the dlookup is not pulling up "H" as it should.

  9. #9
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Note that your Lookup table ("NumToAlpha") and DLOOKUP function are totally unnecessary. Since the numbers and letters are all in corresponding order, you can just use ASCII functions to get what you need, i.e.
    Code:
    Public Function NewCode(ByVal EmpCode As String) As Variant
    
    Dim nums As String
    Dim lefttwo As String
    Dim rightthree As String
    Dim newlet As String
    
    nums = "0" & Left(Mid(EmpCode, 3), 4)
    lefttwo = Left(nums, 2)
    rightthree = Right(nums, 3)
    
    newlet = Chr(lefttwo + 65)
    
    NewCode = newlet & rightthree
    
    End Function

  10. #10
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2003
    Join Date
    Sep 2013
    Posts
    287
    Wow.. Didn't even think to do that. Thanks Joe! Working like a charm now.

  11. #11
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Great! Glad it works out for you!

  12. #12
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Going by your code

    Code:
    newlet = DLookup("Letter", "NumToAlpha", "Number = '" & lefttwo & "'")
    you apparently have a Field named 'Number;' this is a Reserved Word in Access, and these should never be used as Field names! Sooner or later it's apt to jump up and bite you where you'd rather not be bitten!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  13. #13
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2003
    Join Date
    Sep 2013
    Posts
    287
    Thanks Linq. I always forget about the reserved words. That table got deleted anyways with the ASCII fix. Thanks for the reminder tho!

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

Similar Threads

  1. Replies: 3
    Last Post: 11-13-2013, 08:56 AM
  2. Invalid use of null
    By GraeagleBill in forum Programming
    Replies: 3
    Last Post: 03-24-2013, 11:05 PM
  3. Invalid use of Null
    By justauser in forum Forms
    Replies: 2
    Last Post: 11-28-2012, 12:33 PM
  4. Invalid use of Null
    By Wayne311 in forum Programming
    Replies: 4
    Last Post: 01-27-2011, 05:10 PM
  5. Invalid Use of Null!?!
    By Kipster1203 in forum Access
    Replies: 4
    Last Post: 05-13-2010, 06:09 AM

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