Results 1 to 12 of 12
  1. #1
    pandorah is offline Novice
    Windows XP Office 365
    Join Date
    Feb 2021
    Posts
    13

    Isolating numbers in brackets via query (or macro)

    Hi there, I have a table with a field called 'Description'. It is a free form field so there are a variety of formats, character length, etc. I'm trying to build a query that will isolate any row that has a number within a bracket at the end of the character line; then add that number to another column (so the number is isolated on it's own).

    Example:
    'Description' Column
    1. This is one row of data (VE)
    2. This is a second row of data (136)
    3. This is a third row of data with text but no brackets
    4. This is a fourth row of (data) with brackets in the middle

    I only want to isolate the second row; putting the 136 in a separate column. So final output:
    'Description' Column = remains as-is
    'Number of Lines' Column =


    1. 0
    2. 136
    3. 0
    4. 0

    Hopefully this makes sense. I'm really not sure how to go about this so any ideas are helpful. Thank you

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Here is one way:

    Expr1: IIf(Right([YourField],1)=")",Replace(Mid([YourField],InStrRev([YourField],"(")+1),")",""),0)

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    pandorah is offline Novice
    Windows XP Office 365
    Join Date
    Feb 2021
    Posts
    13
    Thank you. I'll give it a try and let you know how it goes.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    How are you at writing code? That's the only approach I see.
    I'd find the first ( using Instr function, then find ) and then use Mid with the starting point being the first value and the number of characters to take being the difference between the two. Then I'd test the result to see whether or not it can be interpreted as a number, in which case it will not be if it looks like "123?"
    Then you could write the value to a table.

    Alternatively, one could loop over the first value until the second and test if it's a number but I don't think that's the approach I would use.

    EDIT - seems I misunderstood the requirement as I thought that (123) in the middle would be needed as well.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    pandorah is offline Novice
    Windows XP Office 365
    Join Date
    Feb 2021
    Posts
    13
    Thank you. This is working but it's not removing anything that isn't a number. For example if the string ends with (V)...it is returning V in the other column. I only want to see numbers...if it's not a number it should show as either blank or 0 in the other column. Do you know how I can add to your code below to do that extra step?


    Quote Originally Posted by Gicu View Post
    Here is one way:

    Expr1: IIf(Right([YourField],1)=")",Replace(Mid([YourField],InStrRev([YourField],"(")+1),")",""),0)

    Cheers,

  6. #6
    pandorah is offline Novice
    Windows XP Office 365
    Join Date
    Feb 2021
    Posts
    13
    Hi there, yes I only need the number that is at the end. If there is a number in the middle of the string of characters then it can be ignored. See my other comment above in reply to Gicu. And I would call myself intermediate at code. I've got the basics down but need help when it gets to things that are more complex.

    Quote Originally Posted by Micron View Post
    How are you at writing code? That's the only approach I see.
    I'd find the first ( using Instr function, then find ) and then use Mid with the starting point being the first value and the number of characters to take being the difference between the two. Then I'd test the result to see whether or not it can be interpreted as a number, in which case it will not be if it looks like "123?"
    Then you could write the value to a table.

    Alternatively, one could loop over the first value until the second and test if it's a number but I don't think that's the approach I would use.

    EDIT - seems I misunderstood the requirement as I thought that (123) in the middle would be needed as well.

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Just wrap it in IsNumeric in another calculated field:

    MyNumber:Iif(IsNumeric([Expr1]),[Expr1],0)

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Quote Originally Posted by Gicu View Post
    Just wrap it in IsNumeric in another calculated field:
    MyNumber:Iif(IsNumeric([Expr1]),[Expr1],0)Cheers,
    Nice solution. Will work as long as it's not a problem showing the Exp1 field in the query but that should not be an issue.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    One could use the original expression instead of Expr1 but would make for a long, ugly and hard to follow calculation...

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  10. #10
    pandorah is offline Novice
    Windows XP Office 365
    Join Date
    Feb 2021
    Posts
    13
    This solved it for me. THANK YOU so much for the quick help!!!!


    Quote Originally Posted by Gicu View Post
    One could use the original expression instead of Expr1 but would make for a long, ugly and hard to follow calculation...

    Cheers,

  11. #11
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You're vey welcome, good luck with your project!

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  12. #12
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Quote Originally Posted by Gicu View Post
    Here is one way:

    Expr1: IIf(Right([YourField],1)=")",Replace(Mid([YourField],InStrRev([YourField],"(")+1),")",""),0)

    Cheers,
    Here is an other way:
    Code:
    Expr1: IIf([YourField] Like "*)",Val(Mid([YourField],InStrRev([YourField],"(")+1));0)
    Cheers,
    john

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

Similar Threads

  1. Replies: 5
    Last Post: 08-08-2020, 01:59 AM
  2. Replies: 4
    Last Post: 02-05-2019, 10:00 AM
  3. Replies: 4
    Last Post: 12-05-2014, 11:15 AM
  4. Replies: 6
    Last Post: 06-06-2014, 11:02 AM
  5. Replies: 1
    Last Post: 07-12-2013, 01:15 PM

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