我在行可能包含一个或多个匹配项的字段上使用正则表达式函数运行查询,但我无法让 Access 返回任何匹配项,除了集合的第一个或最后一个(对我来说是随机的)。
样本数据:
tbl_1 (queried table)
row_1 abc1234567890 some text
row_2 abc1234567890 abc3459998887 some text
row_3 abc9991234567 abc8883456789 abc7778888664 some text
tbl_2 (currently returned results)
row_1 abc1234567890
row_2 abc1234567890
row_3 abc7778888664
tbl_2 (ideal returned results)
row_1 abc1234567890
row_2 abc1234567890
row_3 abc3459998887
row_4 abc9991234567
row_5 abc8883456789
row_6 abc7778888664
这是我的访问 VBA 代码:
Public Function OrderMatch(field As String)
Dim regx As New RegExp
Dim foundMatches As MatchCollection
Dim foundMatch As match
regx.IgnoreCase = True
regx.Global = True
regx.Multiline = True
regx.Pattern = "\b[A-Za-z]{2,3}\d{10,12}\b"
Set foundMatches = regx.Execute(field)
If regx.Test(field) Then
For Each foundMatch In foundMatches
OrderMatch = foundMatch.Value
Next
End If
End Function
我的 SQL 代码:
SELECT OrderMatch([tbl_1]![Field1]) AS Order INTO tbl_2
FROM tbl_1
WHERE OrderMatch([tbl_1]![Field1])<>False;
我不确定我的正则表达式模式是否错误,我的 VBA 代码是否错误,或者我的 SQL 代码是否错误。