473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String search expression with wildcards for calculated field

Hi all,

I have a table with Field1 containing strings. Within each string is an error code that I need to extract for use in Field2 in the same table.

i.e.

Field1

the err 000 111 222 333 444 occurred at 12:56:00 jan 05

Field2

000 111 222 333 444

There position of the code in the string is random and I need to pattern match the format as other numbers are contained in the strings too. The codes however always have the same format:

"### ### ### ### ###"

so my question is how to extract the codes from Field1.

The instr function doesn't take wildcards and the like function only tells you if the pattern exists not where it is.

I found John Nuricks rgxExtract() macro but the Expression entry in table Design View doesn't recognize it.

Please, is there a way of extracting the variable codes from strings?

Thanks for your time. Much appreciated.

Mark.
Aug 7 '10 #1

✓ answered by ADezii

  1. First and foremost, some basic assumptions:
    1. Your Table name is tblStrings (can be any Name).
    2. tblStrinigs consists of a single Field named [Field1].
    3. [Field1] is defined as: {TEXT 255}, Required.
  2. Copy-N-Paste the following Function to a Standard Code Module:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fExtractECodes(strString As String)
    2. Dim varString As Variant
    3. Dim intCounter As Integer
    4. Dim strBuild As String
    5. Dim intNumOfMatches As Integer
    6.  
    7. varString = Split(strString, " ")
    8.  
    9. For intCounter = LBound(varString) To UBound(varString)
    10.   If Len(varString(intCounter)) = 3 And IsNumeric(varString(intCounter)) Then
    11.     intNumOfMatches = intNumOfMatches + 1
    12.       strBuild = strBuild & " " & varString(intCounter)
    13.   End If
    14. Next
    15.  
    16. If intNumOfMatches = 5 Then
    17.   fExtractECodes = Trim(strBuild)
    18. Else
    19.   fExtractECodes = Null
    20. End If
    21. End Function
  3. Create a Query with [Field2] as a Calculated Field. The Value in [Field1] will be passed to the Public Function, and an appropriate Value returned.
    Expand|Select|Wrap|Line Numbers
    1. Field2: fExtractECodes([Field1])
  4. The Logic in fExtractECodes() will search for a specific Pattern of five, sequential, 3-character, space-delimited, digit blocks, namely:
    Expand|Select|Wrap|Line Numbers
    1. ### ### ### ### ###
    then return the correct results.
  5. Sample Data (tblStrings):
    Expand|Select|Wrap|Line Numbers
    1. Field1
    2. the err 000 111 222 333 444 occurred at 12:56:00 jan 05
    3. Not even a digit, let alone sets of 3
    4. fgfgfg wdees sddsds 111 222 333 444 555
    5. dfshhg  fdmnbhj bj 444 555 777 888 jhfjhf
    6. 111 333 444 44 666 1 digit short on the 4th set
    7. 123 444 666 777 888 dasdsds fgghgf hhjjh jh jhkjh jhjhhj hjhj
    8. She sells sea shells at the sea shore
    9. Front 238 999 765 4 2 digits short on back end
    10. 123 444 666 777 888 dasdsds fgghgf hhjjh jh jhkjh jhjhhj hjhj
  6. Query Definition:
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblStrings.Field1, fExtractECodes([Field1]) AS Field2
    2. FROM tblStrings;
  7. Output after running Query:
    Expand|Select|Wrap|Line Numbers
    1. Field1                                                           Field2
    2. the err 000 111 222 333 444 occurred at 12:56:00 jan 05          000 111 222 333 444
    3. Not even a digit, let alone sets of 3    
    4. fgfgfg wdees sddsds 111 222 333 444 555                          111 222 333 444 555
    5. dfshhg  fdmnbhj bj 444 555 777 888 jhfjhf    
    6. 111 333 444 44 666 1 digit short on the 4th set    
    7. 123 444 666 777 888 dasdsds fgghgf hhjjh jh jhkjh jhjhhj hjhj    123 444 666 777 888
    8. She sells sea shells at the sea shore    
    9. Front 238 999 765 4 2 digits short on back end    
    10. 123 444 666 777 888 dasdsds fgghgf hhjjh jh jhkjh jhjhhj hjhj    123 444 666 777 888
  8. If you simply wish to return the Records in [Field1] that meet the criteria, without actually extracting the Values themselves, then:
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblStrings.Field1
    2. FROM tblStrings
    3. WHERE tblStrings.Field1 Like "*### ### ### ### ###*";

3 2693
ADezii
8,834 Expert 8TB
  1. First and foremost, some basic assumptions:
    1. Your Table name is tblStrings (can be any Name).
    2. tblStrinigs consists of a single Field named [Field1].
    3. [Field1] is defined as: {TEXT 255}, Required.
  2. Copy-N-Paste the following Function to a Standard Code Module:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fExtractECodes(strString As String)
    2. Dim varString As Variant
    3. Dim intCounter As Integer
    4. Dim strBuild As String
    5. Dim intNumOfMatches As Integer
    6.  
    7. varString = Split(strString, " ")
    8.  
    9. For intCounter = LBound(varString) To UBound(varString)
    10.   If Len(varString(intCounter)) = 3 And IsNumeric(varString(intCounter)) Then
    11.     intNumOfMatches = intNumOfMatches + 1
    12.       strBuild = strBuild & " " & varString(intCounter)
    13.   End If
    14. Next
    15.  
    16. If intNumOfMatches = 5 Then
    17.   fExtractECodes = Trim(strBuild)
    18. Else
    19.   fExtractECodes = Null
    20. End If
    21. End Function
  3. Create a Query with [Field2] as a Calculated Field. The Value in [Field1] will be passed to the Public Function, and an appropriate Value returned.
    Expand|Select|Wrap|Line Numbers
    1. Field2: fExtractECodes([Field1])
  4. The Logic in fExtractECodes() will search for a specific Pattern of five, sequential, 3-character, space-delimited, digit blocks, namely:
    Expand|Select|Wrap|Line Numbers
    1. ### ### ### ### ###
    then return the correct results.
  5. Sample Data (tblStrings):
    Expand|Select|Wrap|Line Numbers
    1. Field1
    2. the err 000 111 222 333 444 occurred at 12:56:00 jan 05
    3. Not even a digit, let alone sets of 3
    4. fgfgfg wdees sddsds 111 222 333 444 555
    5. dfshhg  fdmnbhj bj 444 555 777 888 jhfjhf
    6. 111 333 444 44 666 1 digit short on the 4th set
    7. 123 444 666 777 888 dasdsds fgghgf hhjjh jh jhkjh jhjhhj hjhj
    8. She sells sea shells at the sea shore
    9. Front 238 999 765 4 2 digits short on back end
    10. 123 444 666 777 888 dasdsds fgghgf hhjjh jh jhkjh jhjhhj hjhj
  6. Query Definition:
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblStrings.Field1, fExtractECodes([Field1]) AS Field2
    2. FROM tblStrings;
  7. Output after running Query:
    Expand|Select|Wrap|Line Numbers
    1. Field1                                                           Field2
    2. the err 000 111 222 333 444 occurred at 12:56:00 jan 05          000 111 222 333 444
    3. Not even a digit, let alone sets of 3    
    4. fgfgfg wdees sddsds 111 222 333 444 555                          111 222 333 444 555
    5. dfshhg  fdmnbhj bj 444 555 777 888 jhfjhf    
    6. 111 333 444 44 666 1 digit short on the 4th set    
    7. 123 444 666 777 888 dasdsds fgghgf hhjjh jh jhkjh jhjhhj hjhj    123 444 666 777 888
    8. She sells sea shells at the sea shore    
    9. Front 238 999 765 4 2 digits short on back end    
    10. 123 444 666 777 888 dasdsds fgghgf hhjjh jh jhkjh jhjhhj hjhj    123 444 666 777 888
  8. If you simply wish to return the Records in [Field1] that meet the criteria, without actually extracting the Values themselves, then:
    Expand|Select|Wrap|Line Numbers
    1. SELECT tblStrings.Field1
    2. FROM tblStrings
    3. WHERE tblStrings.Field1 Like "*### ### ### ### ###*";
Aug 7 '10 #2
Hi ADezii,

First of all THANKS!!!!

Exactly what I was trying to do. Excellent layout to your answer, I really learned a lot with this problem and really
appreciate your reply.

I got caught up trying to solve it with expression builder but with your example found writing directly in VBA and SQL is much more flexible.

All the best,

Mark
Aug 8 '10 #3
ADezii
8,834 Expert 8TB
Glad the solution suited your needs.
Aug 8 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Manfred | last post by:
Hi Would like to add a new Field to Table which is 75 % (or another Percentage) higher than the Value in another Field in same Table.Is this possible with Expression or would I have to enter the...
5
by: Richard Holliingsworth | last post by:
Hello: Thanks for reading this post. I need to create a metrics (form or report - I don't care which) to display calculated fields about the database (A2002 front end to SQL Server 2K) 1) I...
4
by: Apple | last post by:
Can I edit an calculated field in my form if needed. Thank you in advance for your help. Sincerely Apple
2
by: John | last post by:
I am using Access 2000. One table in my database has a field called RankName. Values inlcude: Officer, Sergeant, Lieutenant. I need create a report that groups these three RankNames into two...
2
by: Chakravarthy | last post by:
Given an XML like the below, one can search for London with the xPath expression using //destination/code ... Now my requirement is, how can we search for all descriptions has word "London" which...
4
by: john | last post by:
I've searched google but can't find what I need. In my form I have a field Date Of Birth. Netxt to that field I would like to put a calculated field that shows the age of that particular person,...
1
by: Nelly | last post by:
Please help! I am trying to sort a query by a field calculated as e.g. / I keep getting an error about subqueries not allowed for this type of expression, but only when I set to sort. I...
5
by: Henrik | last post by:
The problem is (using MS Access 2003) I am unable to retrieve long strings (255 chars) from calculated fields through a recordset. The data takes the trip in three phases: 1. A custom public...
5
by: CSmith | last post by:
I have a calculated field in a query, it calculates the # of days an employee has been employed using the following statement =datediff("d", , NOW()) by default. When an employee is terminated, I...
3
by: myemail.an | last post by:
I use Access 2007 and create queries with a number of calculated fields/expressions (I'm still a novice so please forgive me if my wording is imprecise...), like: MyCalculation = Field1 - Field2. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.