Results 1 to 12 of 12
  1. #1
    Cleetus is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    5

    Disecting a field in a query

    Is there a way to pull the number (for this example...could be letters) out in a query? The field example is: XX, 11111-01, xxxxxxxx. The commas are not always 3 characters in, but I am looking to pull out whatever is in-between the commas. Any help is appreciated.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    You can use the Mid() function along with the InStr() function to find the position of the comma, and perhaps the InStrRev() function to find the second comma, if it's also the last.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    So what would you want in your example? 11111-01 ?

    But the commas will always be there?

    If so, then this should work:

    NewFieldName:Mid([OriginalFieldName], Instr(1,[OriginalFieldName],",") + 1), InstrRev([OriginalFieldName], ",")-1)

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    oops, I took too long responding

  5. #5
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Needed to edit it anyway:

    MyNewField:Mid([OriginalFieldNameHere], InStr(1, [OriginalFieldNameHere], ",") + 1, InStrRev([OriginalFieldNameHere], ",") - InStr(1, [OriginalFieldNameHere], ",") - 1)

  6. #6
    Cleetus is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    5
    Thanks for the help. Sorry I havent gotten a chance to try this until now. But I get an invalid procedure call message. Would this have to do with versions or some kind of add-ins?

  7. #7
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Quote Originally Posted by Cleetus View Post
    Thanks for the help. Sorry I havent gotten a chance to try this until now. But I get an invalid procedure call message. Would this have to do with versions or some kind of add-ins?
    I tested it so I know it works. How have you used it? Did you change the OriginalFieldNameHere parts to the name of your actual original field? Also, do you have nulls in your field?

  8. #8
    Cleetus is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    5
    Yes I did replace OriginalFieldNameHere with Description. But I do have Nulls, how does that affect it? Should I add an If statement to the front checking for nulls?

  9. #9
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Yes, you would need to not try doing it when nulls exist. So changing to this:

    NewFieldName:IIf(IsNull([Description]),Null,Mid([OriginalFieldName], Instr(1,[OriginalFieldName],",") + 1), InstrRev([OriginalFieldName], ",")-1))

  10. #10
    DerFarm is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    May 2011
    Posts
    8

    Parsing Strings

    Cletus,

    It sounds as though you probably do more of this type of thing than most.

    I have a set of Access VB Code string functions that will do it.

    In your specific case the command would be

    result=Retrieve(targetstring,delimiter,whichfield) ===>

    targetstring=XX, 11111-01, xxxxxxxx
    delimiter=","
    whichfield=2
    result=" 11111-01"

    If you want it, I can post the code file here.

  11. #11
    Cleetus is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    5
    Bob, I tried to modify as there wasn't equal parenthisis. It's telling me sytax error (comma). item ref: IIf(IsNull([Description]),Null,(Mid([Description],InStr(1,[Description],",")+1),InStrRev([Description],",")-1))

    DerFarm - thanks, but I don't know very much about VB in Access, only Excel. I would like to learn more one day.

  12. #12
    Cleetus is offline Novice
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    5
    OK, got it solved, but now I can see my data is not clean enough to be disected. Thanks for all the help.

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

Similar Threads

  1. Select Query > Update Query > Table Field
    By tuggleport in forum Queries
    Replies: 2
    Last Post: 08-09-2012, 07:04 AM
  2. Replies: 1
    Last Post: 08-31-2011, 04:03 PM
  3. show name of query in query field
    By lmnnt in forum Queries
    Replies: 2
    Last Post: 05-24-2011, 08:01 PM
  4. Reduce field size in query field
    By escapades_access in forum Queries
    Replies: 9
    Last Post: 03-13-2011, 12:21 PM
  5. Replies: 3
    Last Post: 04-26-2010, 11:38 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