How to parse data from one field to another

R

Ron

Hi,

I have a table with one memo field: [FiledData]. The memo field contains a
lot of various information, but the one consistent piece of data
is a number in the following format:

##########-##-######

It can appear anywhere in the field.

I would like to find that data in the memo field, extract it and place it in
a separate field: [AccessionNumber]. I would like this to happen on the
"After Update" event of the [FiledData] field in a form.

Any help in this matter is greatly appreciated!
 
M

Martin J

I'd use instr to find the dashes 2 digits apart then do some check that all
the rest of the digits are actually #'s. instr returns a # so you can use mid
function to get the whole a-#. Try writing a function if you have trouble
please post.

HTH
Martin
 
J

John Nurick

Hi Ron,

Try using the rgxReplace() function below in an update query, with
something like this as the "update to" expression:

rgxReplace([FiledData], ".*(\d{10}-\d{2}-\d{6}).*", "$1", False, True,
True)

The second argument is a regular expression that matches your formatted
number in the middle of the other text.

Hi,

I have a table with one memo field: [FiledData]. The memo field contains a
lot of various information, but the one consistent piece of data
is a number in the following format:

##########-##-######

It can appear anywhere in the field.

I would like to find that data in the memo field, extract it and place it in
a separate field: [AccessionNumber]. I would like this to happen on the
"After Update" event of the [FiledData] field in a form.

Any help in this matter is greatly appreciated!


Function rgxReplace(Optional Target As Variant, _
Optional Pattern As String = "", _
Optional Replacement As String = "", _
Optional CaseSensitive As Boolean = False, _
Optional ReplaceAll As Boolean = True, _
Optional Multiline As Boolean = False, _
Optional FailOnError As Boolean = True, _
Optional Persist As Boolean = True) _
As Variant

'Regular expression replacement function suitable for use
'in VBA generally and in Access queries.

'Takes a search string (Target), a regular expression
'(Pattern), and a replacement string (Replacement).
'If a substring of Target matches Pattern, that substring
'is replaced by Replacement. (The substring can be the whole
'of Target.)

'CaseSensitive matches regardless of case
'but does not affect the case of Replacement.

'ReplaceAll controls whether all substrings
'that match Pattern are replaced or only the first.

'Multiline controls whether a Target containing
'newlines is parsed as one multiline string or a
'succession of one-line strings.

'Persist controls whether the compiled RegExp object
'remains in memory ready for the next call to the
'function or whether it is disposed of immediately. This
'means the function can be used in queries without having
'to create, compile, use and destroy
'a new RegExp object for each row being processed.
'But it also means that the object remains in memory
'after the query has run. To destroy the object and release
'the memory, call this function one last time with no arguments.
'
'Calling the function with different arguments (e.g. a new
'Pattern and/or Replacement) recompiles the RegExp object, so
'the function can be used in different queries. However there
'may be problems if two threads are calling the function at
'the same time.

Const rgxPROC_NAME = "rgxReplace"
Static oRE As Object
'Static declaration means we don't have to create
'and compile the RegExp object every time the function
'is called.

On Error GoTo ErrHandler
rgxReplace = Null 'Default return value
'NB: if FailOnError is false, returns Null on error

If IsMissing(Target) Then
'This is the signal to dispose of oRE
Set oRE = Nothing
Exit Function 'with default value
End If

'Create the RegExp object if necessary
If oRE Is Nothing Then
Set oRE = CreateObject("VBScript.Regexp")
End If

With oRE
'Check whether the current arguments (other than Target)
'are different from those stored in oRE, and update them
'(thereby recompiling the regex) only if necessary.
If CaseSensitive = .IgnoreCase Then
.IgnoreCase = Not .IgnoreCase
End If
If ReplaceAll <> .Global Then
.Global = ReplaceAll
End If
If Multiline <> .Multiline Then
.Multiline = Multiline
End If
If Pattern <> .Pattern Then
.Pattern = Pattern
End If

'Finally, do the replacement
If IsNull(Target) Then
rgxReplace = Null
Else
rgxReplace = .Replace(CStr(Target), Replacement)
End If
End With

'Tidy up and normal exit
If Not Persist Then Set oRE = Nothing
Exit Function

ErrHandler:
If FailOnError Then
With Err
Select Case .Number
'Replace the default "object-defined error" message
Case 13: .Description = "Type mismatch, probably because " _
& "the ""Target"" argument could not be converted to a string"
Case 5017: .Description = "Syntax error in regular expression"
Case 5018: .Description = _
"Unexpected quantifier in regular expression"
Case 5019: .Description = "Expected ']' in regular expression"
Case 5020: .Description = "Expected ')' in regular expression"
Case Else
If oRE Is Nothing Then 'Failed to create Regexp object
.Description = "Could not create VBScript.RegExp object. " _
& Err.Description
Else 'Unexpected error
.Description = rgxPROC_NAME & ": " & .Description
End If
End Select
Set oRE = Nothing
.Raise Err.Number, rgxPROC_NAME, _
rgxPROC_NAME & "(): " & .Description
End With
Else 'Fail silently
Err.Clear
Set oRE = Nothing
End If
End Function
 
R

Ron

Thanks both to Martin and John for your help.

Right now I have this code in the after update event of my memo box,
[FiledData]:

Private Sub FiledData_AfterUpdate()

Dim SearchString, SearchChar, MyPos

SearchString = [FiledData] ' String to search in.
SearchChar = "-##-" ' Search for "-##-".

MyPos = InStr(1, SearchString, SearchChar, 0)

End Sub

So far, MyPos = 0 when trying to use the "#" wild card character. If I look
for the actual number, it works fine. Could you tell me what I'm doing wrong?

Eventually I'm going to use MyPos and Mid() to find the full string and put
it in the [AccessionNumber] field. If someone could show me some sample code
that would work for that I would appreciate it immensely.

My best,

-Ron

PS - John, I used your rgxReplace statement in an update query, but it
wasn't recognized. Any suggestions?


Martin J said:
I'd use instr to find the dashes 2 digits apart then do some check that all
the rest of the digits are actually #'s. instr returns a # so you can use mid
function to get the whole a-#. Try writing a function if you have trouble
please post.

HTH
Martin

Ron said:
Hi,

I have a table with one memo field: [FiledData]. The memo field contains a
lot of various information, but the one consistent piece of data
is a number in the following format:

##########-##-######

It can appear anywhere in the field.

I would like to find that data in the memo field, extract it and place it in
a separate field: [AccessionNumber]. I would like this to happen on the
"After Update" event of the [FiledData] field in a form.

Any help in this matter is greatly appreciated!
 
K

Kevin K. Sullivan

PS - John, I used your rgxReplace statement in an update query, but it
wasn't recognized. Any suggestions?

The function definition is at the end of his post. Paste

Function rgxReplace(Optional Target As Variant, _
.. . . thru . . .
End Function

into a new module.

You can then use it in an Update query as he describes.

HTH,
Thanks for sweet code, John!

Kevin
 
M

Martin J

I wrote this simple search function for you.

Public Function mysearch() As String
Dim srhstr As String, srcstr As String, start As Integer
srhstr = "a-sdkfjwierjfsdfjg 1234567890-03-0000000 asjfhgbdfnge
3-4455463-45" 'string to search
srcstr = "-" 'what to search for
start = InStr(1, srhstr, srcstr)
While InStr(start + 1, srhstr, srcstr) > 0
If InStr(start + 1, srhstr, srcstr) - start = 3 Then mysearch =
Mid$(srhstr, start - 10, 19)
start = InStr(start + 3, srhstr, srcstr)
Wend
End Function

srcstr and srhstr can be switch to function parameters. this will find the
last instance of dashes 2 digits apart and makes no checks in terms of format
(#,letters ) that is returned.

HTH
Martin J

Ron said:
Thanks both to Martin and John for your help.

Right now I have this code in the after update event of my memo box,
[FiledData]:

Private Sub FiledData_AfterUpdate()

Dim SearchString, SearchChar, MyPos

SearchString = [FiledData] ' String to search in.
SearchChar = "-##-" ' Search for "-##-".

MyPos = InStr(1, SearchString, SearchChar, 0)

End Sub

So far, MyPos = 0 when trying to use the "#" wild card character. If I look
for the actual number, it works fine. Could you tell me what I'm doing wrong?

Eventually I'm going to use MyPos and Mid() to find the full string and put
it in the [AccessionNumber] field. If someone could show me some sample code
that would work for that I would appreciate it immensely.

My best,

-Ron

PS - John, I used your rgxReplace statement in an update query, but it
wasn't recognized. Any suggestions?


Martin J said:
I'd use instr to find the dashes 2 digits apart then do some check that all
the rest of the digits are actually #'s. instr returns a # so you can use mid
function to get the whole a-#. Try writing a function if you have trouble
please post.

HTH
Martin

Ron said:
Hi,

I have a table with one memo field: [FiledData]. The memo field contains a
lot of various information, but the one consistent piece of data
is a number in the following format:

##########-##-######

It can appear anywhere in the field.

I would like to find that data in the memo field, extract it and place it in
a separate field: [AccessionNumber]. I would like this to happen on the
"After Update" event of the [FiledData] field in a form.

Any help in this matter is greatly appreciated!
 
R

Ron

Just what I needed - thanks so much Martin!

Martin J said:
I wrote this simple search function for you.

Public Function mysearch() As String
Dim srhstr As String, srcstr As String, start As Integer
srhstr = "a-sdkfjwierjfsdfjg 1234567890-03-0000000 asjfhgbdfnge
3-4455463-45" 'string to search
srcstr = "-" 'what to search for
start = InStr(1, srhstr, srcstr)
While InStr(start + 1, srhstr, srcstr) > 0
If InStr(start + 1, srhstr, srcstr) - start = 3 Then mysearch =
Mid$(srhstr, start - 10, 19)
start = InStr(start + 3, srhstr, srcstr)
Wend
End Function

srcstr and srhstr can be switch to function parameters. this will find the
last instance of dashes 2 digits apart and makes no checks in terms of format
(#,letters ) that is returned.

HTH
Martin J

Ron said:
Thanks both to Martin and John for your help.

Right now I have this code in the after update event of my memo box,
[FiledData]:

Private Sub FiledData_AfterUpdate()

Dim SearchString, SearchChar, MyPos

SearchString = [FiledData] ' String to search in.
SearchChar = "-##-" ' Search for "-##-".

MyPos = InStr(1, SearchString, SearchChar, 0)

End Sub

So far, MyPos = 0 when trying to use the "#" wild card character. If I look
for the actual number, it works fine. Could you tell me what I'm doing wrong?

Eventually I'm going to use MyPos and Mid() to find the full string and put
it in the [AccessionNumber] field. If someone could show me some sample code
that would work for that I would appreciate it immensely.

My best,

-Ron

PS - John, I used your rgxReplace statement in an update query, but it
wasn't recognized. Any suggestions?


Martin J said:
I'd use instr to find the dashes 2 digits apart then do some check that all
the rest of the digits are actually #'s. instr returns a # so you can use mid
function to get the whole a-#. Try writing a function if you have trouble
please post.

HTH
Martin

:

Hi,

I have a table with one memo field: [FiledData]. The memo field contains a
lot of various information, but the one consistent piece of data
is a number in the following format:

##########-##-######

It can appear anywhere in the field.

I would like to find that data in the memo field, extract it and place it in
a separate field: [AccessionNumber]. I would like this to happen on the
"After Update" event of the [FiledData] field in a form.

Any help in this matter is greatly appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top