Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2014
    Posts
    34
    Provided Answers: 1

    Unanswered: Report - comma seperate list with not all items included

    I have a report with people's phone numbers, home, cell, and work, but not everyone has all of them. I want it to look like: "333-3333 (home), 777-7777 (cell), 555-5555(work)". If one of the numbers in not present, I'd like to skip the number along with the appropriate comma, it would be "333-3333(home), 555-5555(work)" if there wasn't a cell phone number. Each of the numbers is stored in a separate field.

    When I use a basic IIf statement, I end up with commas in odd places if some of the numbers are present, like "333-3333(home), " or ", 777-7777(cell)" So I think I could write a complicated IIf statement for all of that, but is there an easier way to do it? Thanks!

  2. #2
    Join Date
    Feb 2004
    Location
    New Zealand
    Posts
    1,419
    Provided Answers: 7
    I would create a Function in a Module

    something like
    Code:
    function Join_Them(home,work,cell)
    tmp = trim(home) & "," & trim(work) & "," & trim(cell)
    'now remove any ,, and replace with  one ,
    Join_Them=replace(tmp,",,",",")
    End function
    in a query

    displaynumbers:Join_Them([thefeildnamehome],[thefeildnamework],[[thefeildnamecell])
    hope this help

    See clear as mud


    StePhan McKillen
    the aim is store once, not store multiple times
    Remember... Optimize 'til you die!
    Progaming environment:
    Access based on my own environment: DAO3.6/A97/A2000/A2003/A2007/A2010
    VB based on my own environment: vb6 sp5
    ASP based on my own environment: 5.6
    VB-NET based on my own environment started 2007
    SQL-2005 based on my own environment started 2008
    MYLE
    YOUR PASSWORD IS JUST LIKE YOUR TOOTHBRUSH DON'T SHARE IT.

  3. #3
    Join Date
    Oct 2014
    Posts
    34
    Provided Answers: 1
    I would create a Function in a Module
    Umm, I think that is above my knowledge level, I don't understand at all.

  4. #4
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Quote Originally Posted by KarenElissa View Post
    Umm, I think that is above my knowledge level, I don't understand at all.
    ..in which case perhaps its time to dip your toe in the water and start to learn

    another way of doing the same functional process as Myles code above, and is arguably a little easier to understand is:-

    Code:
    public function formatPhoneNumbers(HomeNo as string, WorkNo as string, MobileNo as string) as string
    formatPhoneNumbers = "" 'set up our return variable
    'test if we have something in HomeNo, if so add it to the return variable
    'NOTE we alwasy add a trailing comma.... you coudl do a fiendishly complex check each time to see if a trailing comma is required, but this code is about making it easier to see what is going on ;)
    if not is null(HomeNo) and len(HomeNo)>0 then 
      formatPhoneNumbers = formatPhoneNumbers & HomeNo & " (Home), "
    endif
    'repeat for the work number
    if not is null(WorkNo) and len(WorkNo)>0 then
      formatPhoneNumbers = formatPhoneNumbers & WorkNo & " (Work), "
    endif
    'and the mobile (cell) number
    if not is null(MobileNo) and len(MobileNo)>0 then
      formatPhoneNumbers = formatPhoneNumbers & MobileNo & " (Mobile), "
    endif
    if len(formatPhoneNumbers) > 0 then 'we need to strip of the trailing comma
      formatPhoneNumbers = left(formatPhoneNumbers, len(formatPhoneNumbers) -1)
    endif
    then call that function in your form / report / query etc....
    mystring = formatPhoneNumbers('+44 161 445 8841', '+001 604 930 1234', '+44 7780 618666')
    which should give mystring as
    +44 161 445 8841 (Home), +001 604 930 1234 (work), +44 7780 618666 (Mobile)
    whereas
    mystring = formatPhoneNumbers('+44 161 445 8841', , '+44 7780 618666')
    which should give mystring as
    +44 161 445 8841 (Home), +44 7780 618666 (Mobile)

    arguably you could wrap this up into a more flexible function which will take any phone number and a descriptor. this woulkd be preferable if say you had more than one home, or cell or work number.

    eg:-
    Code:
    public function formatphonenumber(IncomingformattedNumber as string, PhoneNumber as string, Descriptor as String)
    formatphonenumber = "" 'intiialise our return variable
    if not isnull(PhoneNumber) and len(PhoneNumber)>0 then 'only do somethign if the phone number variable contains something
      if len(IncomingformattedNumber) >0 then ' see if we need to add a leading comma (will need to do so if there is already something in IncomingformattedNumber
        formatphonenumber =   IncomingformattedNumber & ","
      endif
      formatphonenumber = PhoneNumber 'add the current number to the return variable
      if len(Descriptor) > 0 then ' add the descriptor if its set
        formatphonenumber =     formatphonenumber & " (" & descriptor & ")"
      else ' we had a value for the phone number, but no descriptor supplied, so what to do?
        formatphonenumber =     formatphonenumber & " ( **unknown** )"
      endif
    end function
    usage is somethign like
    myphnenumbers = formatphonenumber("","01625 585666", "Home")
    or
    myphnenumbers = formatphonenumber(formatphonenumber(formatphonenum ber("","07780618666", "Mobile"),"01925 63266", "Work")","01625 585666", "Home")
    the function needs to be in a code module. if the function is ONLY EVER going to be called form one form or one report then stuff it in the that form or reports code module., if it is (could be) going to be used in more than one form or report then it MUST go into a public code module so that each form / report / query can access the code
    if the function is going to be used in a query then it must be in a code module
    Last edited by healdem; 09-29-15 at 16:13.
    I'd rather be riding on the Tiger 800 or the Norton

  5. #5
    Join Date
    Oct 2014
    Posts
    34
    Provided Answers: 1
    Quote Originally Posted by healdem View Post
    ..in which case perhaps its time to dip your toe in the water and start to learn
    Usually I would be all for figuring it out, I've taught myself quite a lot in the past year, but this is one of those projects that needs to be done now.

    I was hoping there would be some simple thing I had missed, but apparently not, so I gave up and just wrote a long IIf statement. I'm sure it isn't pretty and wouldn't be practical with more than three options, but is something I can easily do and it works for now.

    Thanks for the suggestions though, hopefully sometime when I have more time I can go back and fix some of these little things and make them better.

  6. #6
    Join Date
    Feb 2004
    Location
    New Zealand
    Posts
    1,419
    Provided Answers: 7
    It be heaps heaps beater putting that iif into a Function

    here what to do

    open the database

    ALt+f11 this will open a vb window

    now click Insert -> Module

    You should now see Option Compare Database top of white screen

    come down a couple of line

    and Paste "healdem" code in
    you should see blue and black text

    if you see some red lines say arrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr bugger

    looking at code the "is null" should be isnull it should change to Isnull you did t right

    don't forget to add the End function


    Thats the Hardest bit done

    now lets test it

    you should see a Immediate window

    if not click View -> Immediate window

    in that white screen
    type

    ? formatPhoneNumbers("1234","5678","901") press the enter key
    it will return something
    1234 (Home), 5678 (Work), 901 (Mobile),

    now we got that working

    to use it in a Query

    create a query add some tables

    DisplayThis:formatPhoneNumbers([feildnameofhomephone],[feildnameofworkphone],[feildnameofcellphone])

    Run the query

    VB 101

    you 2 type of action

    SUB function

    think of sub as Sub Parts of your program

    think of function as calculation

    I_WANT_THIS_TO_BE = formatPhoneNumbers(pass_in_this,pass_in_this,pass_ in_this)

    and the function will return something.

    now go and play
    Last edited by myle; 09-30-15 at 15:34. Reason: spelling
    hope this help

    See clear as mud


    StePhan McKillen
    the aim is store once, not store multiple times
    Remember... Optimize 'til you die!
    Progaming environment:
    Access based on my own environment: DAO3.6/A97/A2000/A2003/A2007/A2010
    VB based on my own environment: vb6 sp5
    ASP based on my own environment: 5.6
    VB-NET based on my own environment started 2007
    SQL-2005 based on my own environment started 2008
    MYLE
    YOUR PASSWORD IS JUST LIKE YOUR TOOTHBRUSH DON'T SHARE IT.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •