Connecting Tech Pros Worldwide Help | Site Map

Extract Numbers from Memo field

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 16th, 2006, 08:35 PM
Dave
Guest
 
Posts: n/a
Default Extract Numbers from Memo field

Hope someone can help!
I have a memo fiels in which there are a few numbers including dates but
what I want to do is extract a number which is 6 figures long. Can anyone
help me?
Thanks Dave



  #2  
Old July 16th, 2006, 08:55 PM
Larry Linson
Guest
 
Posts: n/a
Default Re: Extract Numbers from Memo field

"Dave" wrote
Quote:
I have a memo fiels in which there are
a few numbers including dates but
what I want to do is extract a number
which is 6 figures long. Can anyone
help me?
What indication do you have exactly where the text representing that number
_is_ in the Memo Field? The Left, Right, and Mid functions work nicely to
extract a text string from a larger text string, but you have to know the
character position where it begins (and where it ends, or how long it is).
If it is preceded by some title/label, it may be possible to use the InStr
function to locate the title/label's location, then add the necessary length
to determine the first character of the numeric string.

Larry Linson
Microsoft Access MVP


  #3  
Old July 16th, 2006, 09:45 PM
Dave
Guest
 
Posts: n/a
Default Re: Extract Numbers from Memo field

The only indication I have is that it is 6 figures long and looking at a
sample in the field it seems to be the only one that is this number of
figures. It is all I have to work with.

"Dave" <david8232@btinternet.comwrote in message
news:deWdnQqeHrxhOSfZRVnytA@bt.com...
Quote:
Hope someone can help!
I have a memo fiels in which there are a few numbers including dates
but what I want to do is extract a number which is 6 figures long. Can
anyone help me?
Thanks Dave
>

  #4  
Old July 16th, 2006, 10:05 PM
Lyle Fairfield
Guest
 
Posts: n/a
Default Re: Extract Numbers from Memo field

Dave wrote:
Quote:
Hope someone can help!
I have a memo fiels in which there are a few numbers including dates but
what I want to do is extract a number which is 6 figures long. Can anyone
help me?
Thanks Dave
This requires that VB Script be installed. TTBOMK it is installed by
default with Windows >=2000.

Public Function SixDigitNumerals(ByVal vInput As String) As Variant
' assumes VBScript is installed
Dim RE As Object
Dim SDNMatches As Object
Dim SDN As Variant
Dim SDNS As String
Set RE = CreateObject("VBScript.RegExp")
With RE
.Global = True
.Pattern = "(\b|\D)\d{6}(\D|\b)"
End With
Set SDNMatches = RE.Execute(vInput)
For Each SDN In SDNMatches
SDNS = SDNS & "," & Mid$(SDN, 2, 6)
Next SDN
SDNS = Replace(SDNS, ",", "", , 1)
SixDigitNumerals = Split(SDNS, ",")

End Function

Sub test()
Dim s As Variant
Dim ss As Variant
s = SixDigitNumerals("a666666da55555a7777777g000000")
For Each ss In s
Debug.Print ss
Next ss
End Sub

It returns 666666 and 000000

It does not return the five digit 55555 nor the seven digit 7777777. It
could be adjusted according to your needs.

The function is not mature. I dashed it off while waiting for supper,
so it's only very slightly advanced beyond air code.

Maybe it needs the two objects to be released. I tend not to do this
unless VBA forces me to.

I wrote this in Google's TextBox ... at times it went nuts and showed
everything several times ... I hope it posts OK.

  #5  
Old July 17th, 2006, 10:25 AM
Lyle Fairfield
Guest
 
Posts: n/a
Default Re: Extract Numbers from Memo field

The function posted previously did not handle "666666abc..." correctly.
This is a revision with notes:

Public Function SixDigitNumerals(ByVal vInput As String) As Variant

' creates pointers to objects
Dim RE As Object
Dim RE2 As Object

' creates pointers to objects
Dim SDNMatches As Object
Dim SDNMatches2 As Object

' creates a pointer to a variant
Dim SDN As Variant

' creates a pointer to a string
Dim SDNS As String

' references the VBScript Library
' reserves space in memory
' for twp VBScript Regular Expression Objects
' adn their objects and properties
' and loads default values
Set RE = CreateObject("VBScript.RegExp")
Set RE2 = CreateObject("VBScript.RegExp")

' establishes the pattern
' which Regular Expression 2 will attempt to match
' this pattern is
' 6 digits
RE2.Pattern = "\d{6}"

With RE
' sets the Global Property
' of Regular Expression 1 to True
' thus directing that all searches and matches
' will find all matching patterns
' rather than just the first one
.Global = True

' establishes the pattern
' which the Regular Expression will attempt to match
' this pattern is

' (\^|\D) -begin at a input beginning
' or a non-digit character

' \d{6} -six digits

' (\D|\b) -end at a non-digit character
' or at input end
.Pattern = "(^|\D)\d{6}(\D|$)"
End With

' performs a search, matching
' in vInput for the pattern
' established above
' storing results in SDNMatches
' a collection like object
Set SDNMatches = RE.Execute(vInput)

For Each SDN In SDNMatches
' performs a search
' on each of the matches found
' matching just the 6 digits
Set SDNMatches2 = RE2.Execute(SDN)

' adds a comma
' and the 6 digit substring
' to our working string
SDNS = SDNS & "," & SDNMatches2(0)
Next SDN

' removes the first comma from the working string
SDNS = Replace(SDNS, ",", "", , 1)

' splits SDNS into a variant array
' of all 6 digit numerals in vInput
' and assigns the array to the function
' as its value
' (returns the array)
SixDigitNumerals = Split(SDNS, ",")
End Function

  #6  
Old July 17th, 2006, 11:45 AM
RoyVidar
Guest
 
Posts: n/a
Default Re: Extract Numbers from Memo field

"Lyle Fairfield" <lylefairfield@aim.comwrote in message
<1153131907.198144.188290@m73g2000cwd.googlegroups .com>:
Quote:
The function posted previously did not handle "666666abc..."
correctly. This is a revision with notes:
>
Public Function SixDigitNumerals(ByVal vInput As String) As Variant
>
' creates pointers to objects
Dim RE As Object
Dim RE2 As Object
>
' creates pointers to objects
Dim SDNMatches As Object
Dim SDNMatches2 As Object
>
' creates a pointer to a variant
Dim SDN As Variant
>
' creates a pointer to a string
Dim SDNS As String
>
' references the VBScript Library
' reserves space in memory
' for twp VBScript Regular Expression Objects
' adn their objects and properties
' and loads default values
Set RE = CreateObject("VBScript.RegExp")
Set RE2 = CreateObject("VBScript.RegExp")
>
' establishes the pattern
' which Regular Expression 2 will attempt to match
' this pattern is
' 6 digits
RE2.Pattern = "\d{6}"
>
With RE
' sets the Global Property
' of Regular Expression 1 to True
' thus directing that all searches and matches
' will find all matching patterns
' rather than just the first one
.Global = True
>
' establishes the pattern
' which the Regular Expression will attempt to match
' this pattern is
>
' (\^|\D) -begin at a input beginning
' or a non-digit character
>
' \d{6} -six digits
>
' (\D|\b) -end at a non-digit character
' or at input end
.Pattern = "(^|\D)\d{6}(\D|$)"
End With
>
' performs a search, matching
' in vInput for the pattern
' established above
' storing results in SDNMatches
' a collection like object
Set SDNMatches = RE.Execute(vInput)
>
For Each SDN In SDNMatches
' performs a search
' on each of the matches found
' matching just the 6 digits
Set SDNMatches2 = RE2.Execute(SDN)
>
' adds a comma
' and the 6 digit substring
' to our working string
SDNS = SDNS & "," & SDNMatches2(0)
Next SDN
>
' removes the first comma from the working string
SDNS = Replace(SDNS, ",", "", , 1)
>
' splits SDNS into a variant array
' of all 6 digit numerals in vInput
' and assigns the array to the function
' as its value
' (returns the array)
SixDigitNumerals = Split(SDNS, ",")
End Function
I've taken the liberty of doing a small alteration of the pattern -
grouping, then doing some alterations of the function, utilizing the
submatches collection of the match object (watch for linebreaks).
http://msdn.microsoft.com/library/de...0e7e82faa5.asp

Function rvsSixDigits(ByVal v_strIn As String) As Variant

Dim re As Object
Dim SDNMatches As Object
Dim SDN As Object
Dim SDNS As String

Set re = CreateObject("vbscript.regexp")

With re
.Global = True
.Pattern = "(^|\D)(\d{6})(\D|$)"
Set SDNMatches = .Execute(v_strIn)
For Each SDN In SDNMatches
SDNS = SDNS & "," & SDN.SubMatches(1)
Next SDN
SDNS = Replace(SDNS, ",", "", , 1)
End With

rvsSixDigits = Split(SDNS, ",")

End Function

--
Roy-Vidar


  #7  
Old July 17th, 2006, 01:25 PM
Lyle Fairfield
Guest
 
Posts: n/a
Default Re: Extract Numbers from Memo field

RoyVidar wrote:
Quote:
Function rvsSixDigits(ByVal v_strIn As String) As Variant
>
Dim re As Object
Dim SDNMatches As Object
Dim SDN As Object
Dim SDNS As String
>
Set re = CreateObject("vbscript.regexp")
>
With re
.Global = True
.Pattern = "(^|\D)(\d{6})(\D|$)"
Set SDNMatches = .Execute(v_strIn)
For Each SDN In SDNMatches
SDNS = SDNS & "," & SDN.SubMatches(1)
Next SDN
SDNS = Replace(SDNS, ",", "", , 1)
End With
>
rvsSixDigits = Split(SDNS, ",")
>
End Function
Very nice; I didn't know VBScript's RegExp had submatches, and have
never been successful in VBS with using $n.
I think if one wanted an example of why we strive for elegant solutions
one might read this thread.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.