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

    Code to remove "#" or "APT" from Line 2 of addresses

    Hey guys,



    I wrote the following function to remove the "#" or "APT" from line 2 of an address (i.e. "APT 201" or "#201" goes to just "201").
    Code:
    Public Function AptName(Apt As String) As String    
        If Apt Like "APT *" Then
            AptName = Right(Apt, Len(Apt) - 4)
        Else
            If Apt Like "[#]*" Then
            AptName = Right(Apt, Len(Apt) - 1)
            Else
            AptName = Apt
            End If
        End If
    
    
    
    
    
    
    End Function
    That last bit, "AptName = Apt" was a recent addition to try to get rid of the #Errors I am getting.. but obviously it didn't work. The majority of the addresses in our system don't have a Line2 address. That field is null in those cases. I keep getting an #Error. Why? How can I fix this?

    Click image for larger version. 

Name:	Screen Shot 2014-07-12 at 11.05.34 AM.png 
Views:	7 
Size:	13.3 KB 
ID:	17230

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Just an observation and I have not tested it but it looks like you are counting characters to include white space
    AptName = Right(Apt, Len(Apt) - 4)

    So maybe the trim function would be more appropriate
    AptName = Trim(Right(Apt, Len(Apt) - 3))

    I think that's how it would go. You would have to test it.


    As for the #Error
    Instead of using the LIKE operator, I would probably use the InStr() function

    So maybe
    If InStr(AptName, "#") Then

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    A String can't take a Null. Change

    Public Function AptName(Apt As Variant) As String

    And you you may want to test for Null in the function, or have it also return Variant.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    snipe's Avatar
    snipe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    287
    pbaldy for the win.


    Thank you very much! Works great now!

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 2
    Last Post: 11-14-2012, 04:47 PM
  2. Replies: 11
    Last Post: 03-29-2012, 02:32 PM
  3. Replies: 0
    Last Post: 01-11-2012, 12:34 PM
  4. Replies: 16
    Last Post: 07-22-2011, 09:23 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