Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expressions in MS Access VBA?

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?

like image 208
Benjamin Brauer Avatar asked Sep 22 '10 15:09

Benjamin Brauer


People also ask

Can you use regex in access?

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.

Which are 3 uses of regular expression?

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.

What is regex in VB net?

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.


2 Answers

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/

like image 83
BenV Avatar answered Oct 02 '22 23:10

BenV


You can use CreateObject("vbscript.regexp") or just reference the scripting library.

like image 32
Fionnuala Avatar answered Oct 02 '22 22:10

Fionnuala