I definitely like MS Access as an RAD-Tool for small-scope data-driven applications. But one thing I really miss as a .Net-Developer is Regular Expressions. They really come in handy when validating user input. I really do not know why Microsoft did not put these in the standard VBA-Library.
Is there a way to use Regular Expressions in MS Access VBA?
Using Regexes in SQL CriteriaIn Access, creating a user-defined function that can be used directly in SQL queries is easy. Then you can use it in your query criteria, for instance to find in a PartTable table, all parts that are matching variations of screw 18mm like Pan Head Screw length 18 mm or even SCREW18mm etc.
Regular expressions are useful in any scenario that benefits from full or partial pattern matches on strings. These are some common use cases: verify the structure of strings. extract substrings form structured strings.
A regular expression is a pattern that could be matched against an input text. The . Net framework provides a regular expression engine that allows such matching. A pattern consists of one or more character literals, operators, or constructs.
You can use the VBScript Regex Object by adding a reference to the Microsoft VBScript Regular Expressions library.
Example usage:
Dim szLine As String
Dim regex As New RegExp
Dim colregmatch As MatchCollection
With regex
.MultiLine = False
.Global = True
.IgnoreCase = False
End With
szLine = "Analyzed range (from-to) 10 100"
regex.Pattern = "^Analyzed range"
If regex.Test(szLine) Then
regex.Pattern = ".*?([0-9]+).*?([0-9]+)"
Set colregmatch = regex.Execute(szLine)
'From
Debug.Print colregmatch.Item(0).submatches.Item(0)
'To
Debug.Print colregmatch.Item(0).submatches.Item(1)
End If
Source: http://mark.biek.org/blog/2009/01/regular-expressions-in-vba/
You can use CreateObject("vbscript.regexp")
or just reference the scripting library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With