Question : Extract email address from an Access memo field.

Hi Experts,

How can I extract email address from qryEmail.Body (memo field) and append to tblEmail.Bounce in the same Access 2007 database?

Thank you!

Answer : Extract email address from an Access memo field.

Is the email address the ONLY item in that memo field? I"m betting it's not, so you'd have to have some method to find that Email address in the Memo field, and AFAIK there is no builtin macro action that will do that - you'll need to use Regular Expressions, which can evaluate a text string/file and return to you a matched string, based on the "expression" you supply.

The code attached below will do this. Just copy/paste those items into a new Standard Module (name that module basTextFunctions, or something like that), and then use the FindEmailInString function to return the first Email address located in the string. To do that, assuming you have a Form on which the Memo field is present, you could include a button to show the value:

Sub MyButton_Click()
  Msgbox  FindEmailInString(Me.YourMemoField)
End Sub

Note the code for that Regular Expression came from John Nurick's excellent page here: http://www.j.nurick.dial.pipex.com/Code/index.htm

To read more about Regular Expressions, see our own Patrick Matthew's article on Reg Ex: http://www.experts-exchange.com/articles/Programming/Languages/Visual_Basic/Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html?sfQueryTermInfo=1+30+express+regular





1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
Public Function FindEmailInString(StringToSearch As String) As String

Dim sExp As String

sExp = "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"
FindEmailInString rgxExtract(StringToSearch, sExp)

End Function
Public Function rgxExtract(Optional ByVal Target As Variant, _
    Optional Pattern As String = "", _
    Optional ByVal Item As Long = 0, _
    Optional CaseSensitive As Boolean = False, _
    Optional FailOnError As Boolean = True, _
    Optional Persist As Boolean = False) _
  As Variant
   
  'Regular expression matching function suitable for use
  'in VB/A generally and in Access queries.
  'By John Nurick. Updated 14 Jan 06.
   
  'Takes a search string (Target) and a regular expression
  '(Pattern), and an optional Item argument.
  '- If Item is omitted and a substring of Target matches Pattern,
  '  returns that substring.
  '- If Pattern includes grouping parentheses, a substring of Target
  '  matches Pattern, and Item is an integer, returns the submatch
  '  specified by Item (first submatch is item 0). If there aren't
  '  enough submatches, returns Null. Negative values of Item start
  '  counting with the last submatch.
  '- If no match, returns Null.
  '- Returns Null on error unless FailOnError is True.
  '  Always matches against the entire Target (i.e. Global and
  '  Multiline are True).
  
  'CaseSensitive matches regardless of case.
 
  '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) 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 = "rgxExtract"
  Static oRE As Object 'VBScript_RegExp_55.RegExp
    'Static declaration means we don't have to create
    'and compile the RegExp object every single time
    'the function is called.
  Dim oMatches As Object 'VBScript_RegExp_55.MatchCollection
   
  On Error GoTo ErrHandler
  rgxExtract = 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
    .Global = True
    .Multiline = True
'    If Multiline <> .Multiline Then
'      .Multiline = Multiline
'    End If
    If Pattern <> .Pattern Then
      .Pattern = Pattern
    End If
 
  'Finally, execute the match
    If IsNull(Target) Then
      rgxExtract = Null
    Else
      Set oMatches = oRE.Execute(Target)
      If oMatches.Count > 0 Then
        If oMatches(0).SubMatches.Count = 0 Then
          'No ( ) group in Pattern: return the match
          If Item < 0 Then 'we're counting from last item
            'convert to count from the first item
            Item = oMatches.Count + Item
          End If
          Select Case Item
            Case Is < 0
              'Negative Item originally passed exceeded the
              'number of matches
              rgxExtract = Null
              If FailOnError Then
                Err.Raise 9
              End If
            Case Is >= oMatches.Count
              'Positive Item exceeded the number of matches
              rgxExtract = Null
              If FailOnError Then
                Err.Raise 9
              End If
            Case Else
              rgxExtract = oMatches(Item)
          End Select
         
        Else  'There are one or more ( ) captured groups in Pattern
              'return the one specified by Item
          With oMatches(0).SubMatches
            If Item < 0 Then 'we're counting from last item
              'convert to count from the first item
              Item = .Count + Item
            End If
            Select Case Item
              Case Is < 0
                'Negative Item originally passed exceeded the
                'number of submatches
                rgxExtract = Null
                If FailOnError Then
                  Err.Raise 9
                End If
              Case Is >= .Count
                'Positive Item exceeded the number of submatches
                rgxExtract = Null
                If FailOnError Then
                  Err.Raise 9
                End If
              Case Else 'valid Item number
                rgxExtract = .Item(Item)
            End Select
          End With
        End If
      Else
        rgxExtract = Null
      End If
    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 9: .Description = "Subscript out of range (the Item number requested " _
          & "was greater than the number of matches found, or than the number of " _
          & "(...) grouping/capturing parentheses in the Pattern)."
        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
Random Solutions  
 
programming4us programming4us