473,385 Members | 1,796 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,385 software developers and data experts.

MS Access Find specific text format in string

Hi. Thank you in advance for helping or attempting to help.

I have an access DB with a memo field. This memo field contains lots of information, which is hand-typed and not consistent.

I need to pull from this field a string the matches the following format

####-####

(That being 4 numbers, a dash, then 4 numbers.)

I would prefer to use a built in function, but am comfortable with using VBA.

Examples:
123 text more text account 1234-5678 text more text
test more text 12321324234 acct: 2345-6789 text more text
test-moretext 1234 5678 Account Number 3456-7890 text more

Results would return:
1234-5678
2345-6789
3456-7890
Oct 18 '18 #1
4 2891
PhilOfWalton
1,430 Expert 1GB
An interesting half hour.

Not sure what you want to do with the output, but this should work (anyway it does with your example.
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Function ExtractNumbers() As String
  5.  
  6.     Dim InputStr As String
  7.     Dim Lngi As Long
  8.     Dim Lngj As Long
  9.     Dim Intk As Integer
  10.     Dim SavedNumbers As String
  11.  
  12.     InputStr = "123 text more text account 1234-5678 text more text "
  13.     InputStr = InputStr & "test more text 12321324234 acct: 2345-6789 text more text "
  14.     InputStr = InputStr & "test-moretext 1234 5678 Account Number 3456-7890 text more"
  15.  
  16.     For Lngi = 1 To Len(InputStr)
  17.         If Not IsNumeric(Mid(InputStr, Lngi, 1)) And Mid(InputStr, Lngi, 1) <> "-" Then
  18.             Lngj = 1
  19.             GoTo NextLngI
  20.         End If
  21.         'Stop
  22.         If Lngj + 9 > Len(InputStr) Then        ' Past the end
  23.             Exit Function
  24.         End If
  25.  
  26.         For Lngj = 0 To 8
  27.        ' Debug.Print Mid(InputStr, Lngi + Lngj, 1)
  28.             If Lngj <= 4 Then
  29.                 If Not IsNumeric(Mid(InputStr, Lngi + Lngj, 1)) And Mid(InputStr, Lngi + Lngj, 1) <> "-" Then         ' Not a number
  30.                     GoTo NextLngI
  31.                 End If
  32.             End If
  33.             If Lngj = 4 Then                            ' Look for dash
  34.                 If Mid(InputStr, Lngi + Lngj, 1) <> "-" Then         ' Not a dash
  35.                     GoTo NextLngI
  36.                 End If
  37.             End If
  38.             If Lngj > 5 Then
  39.                 If Not IsNumeric(Mid(InputStr, Lngi + Lngj, 1)) And Mid(InputStr, Lngi + Lngj, 1) <> "-" Then          ' Not a number
  40.                     GoTo NextLngI
  41.                 End If
  42.             End If
  43.         Next Lngj
  44.  
  45.         Stop
  46.         For Intk = 0 To 8
  47.             SavedNumbers = SavedNumbers & Mid(InputStr, Lngi + Intk, 1)
  48.         Next Intk
  49.  
  50.         Debug.Print SavedNumbers
  51.         SavedNumbers = ""
  52. NextLngI:
  53.     Lngi = Lngi + Lngj - 1
  54.     Next Lngi
  55.  
  56. End Function
Note that the function should really start off with
Expand|Select|Wrap|Line Numbers
  1. Function ExtracNumbers(InputStr as String) As String
and the "InputStr" should not be mentioned on lines 6,12,13 & 14.

Phil
Oct 19 '18 #2
NeoPa
32,556 Expert Mod 16PB
Hi.

I haven't tested this but it should do the job for you.
Expand|Select|Wrap|Line Numbers
  1. Public Function ExtractVals(ByVal strInput As String) As String
  2.     Dim lngNext As Long
  3.     Dim strWork As String
  4.  
  5.     Do While strInput Like "*####-####*"
  6.         lngNext = InStr(lngNext + 1, strInput, "-")
  7.         If lngNext > 4 Then
  8.             strWork = Mid(strInput, lngNext - 4, 9)
  9.             If strWork Like "####-####" Then
  10.                 ExtractVals = ExtractVals & VbNewLine & strWork
  11.                 lngNext = lngNext + 4
  12.             End If
  13.         End If
  14.     Loop
  15.     If ExtractVals > "" Then ExtractVals = Mid(ExtractVals, 3)
  16. End Function
Oct 19 '18 #3
I appreciate your help with this. I tried to put this into use, but it seems to hang up every time I attempt to use it. (Even on a sample record set containing only 8 records)
Dec 10 '18 #4
NeoPa
32,556 Expert Mod 16PB
When I tested it, it came up with similar results :-( It was busy in an interminable loop.

Try this revised version. I've tested it this time ;-)
Expand|Select|Wrap|Line Numbers
  1. 'ExtractVals() extracts values that match the format ####=#### from strInput.
  2. Public Function ExtractVals(ByVal strInput As String) As String
  3.     Dim lngNext As Long
  4.     Dim strWork As String
  5.  
  6.     lngNext = 1
  7.     Do While Mid(strInput, lngNext) Like "*####-####*"
  8.         lngNext = InStr(lngNext, strInput, "-")
  9.         If lngNext > 4 Then
  10.             strWork = Mid(strInput, lngNext - 4, 9)
  11.             If strWork Like "####-####" Then
  12.                 ExtractVals = ExtractVals & vbNewLine & strWork
  13.                 lngNext = lngNext + 4
  14.             End If
  15.         End If
  16.         lngNext = lngNext + 1
  17.     Loop
  18.     If ExtractVals > "" Then ExtractVals = Mid(ExtractVals, 3)
  19. End Function
I tried the following test in the Immediate Pane with the results shown :
Expand|Select|Wrap|Line Numbers
  1. ?ExtractVals("-4444-333 try 1111-777789 if that's0000-8753-9999-");
  2. 1111-7777
  3. 0000-8753
Dec 10 '18 #5

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

Similar topics

3
by: Robert Oschler | last post by:
What's a good way to find a specific text node element in a web page's DOM tree? I thought of traversing each node but there has to be a faster way. Is there a "find text node by nodeValue"...
4
by: searider86 | last post by:
Does anyone know if there is a snippet of code that automatically removes the wrap text format in excel? I have an access form that displays data and allows the user to click on a cmdbutton to...
7
by: Eddy Soeparmin | last post by:
Hi, I need to display a DateTime field in 'mm/dd/yyyy' in a DataGrid.. On myGrid1 - Properties - Columns - myColumn1 - Text format string: I tried to put 'mm/dd/yyyy' in there and it displays...
2
by: Mike Kansky | last post by:
Is there a built in function in .NET to find URL in a string so i can format it as <a href="url" ? useful for Forum Posts and other human entered text.
10
by: tom | last post by:
Im trying understand format string vulnerability. Source along Erickson's HACKING: The Art of Exploitation. #include <stdlib.h> int main(int argc, char *argv){ char text; static int...
12
by: param | last post by:
Hi All, I want to use %x as directive to get the locale specific date representation. But, at the same time, i want to know the format of the output for interpreting it through program. Is there...
1
by: Arielle | last post by:
The Problem: Currently working on a way to catalog a large flux of files into a database. The files are for the most part uniform but some of the flags in the database fields vary depending on the...
6
by: and1 | last post by:
hello .. hmm currently i'm working on vba in excel, apparently i use ADO to extract a table data from access to excel and it works fine. the problem is when i use the extracted data to create a chart...
2
by: edkreuz | last post by:
I have moved a database from work to home and it went from 2010 to 2013. Most of my fields are functioning correctly in my forms, but I have one field that is a combo dropdown that will not display...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.