Regular expressions

  • Thread starter Thread starter Petr Danes
  • Start date Start date
P

Petr Danes

I've just started seriously messing around with reg exps in Access VBA and
predictably, am opening all kinds of worm cans. Is there a difference
between the following?

Dim RE as RegExp
Set RE = New RegExp

and

Dim RE as Object
Set RE = CreateObject("VBScript.Regexp")

Various uses give me differing error messages, but I've been unable to find
any references to either in the online help, and googling around the net
hasn't shed any light on this particular topic.

Pete
 
hi Pete,

Petr said:
Is there a difference between the following?

Dim RE as RegExp
Set RE = New RegExp

and

Dim RE as Object
Set RE = CreateObject("VBScript.Regexp")
Sure. The first one is called "early binding". It requires that you set
a reference first, before using it. The advantage is that you'll get
IntelliSense. A drawback is that you must have this special reference on
all computers.
So "early binding" gives you type safety as the type is known at compile
time.

The later is called "late binding". You don't need a reference. The type
is resolved at runtime and it match different versions.

mfG
--> stefan <--
 
The later is called "late binding". You don't need a reference. The type
is resolved at runtime and it match different versions.

To add to Stefan's comments, when you use late binding you can trap
for an error when instantiating the object. This allows you to
discover if the PC doesn't have that reference installed and let the
user know with a message. With early binding, if the reference is
missing your whole application will break with compile errors.

Armen Stein
Microsoft Access MVP
www.JStreetTech.com
 
Sure. The first one is called "early binding".

Is that the only difference? I do actually know about early vs. late
binding, but when I was playing with it earlier, IntelliSense didn't work
and I was getting different error messages depending on which method I used,
so I thought I was addressing two different object libraries. When I try it
now, though, setting up two variables, one of each kind, in a test routine,
IntelliSense does work (for the early-bound one). The error messages, when I
intentionally munch the regexp string, are different in text but both throw
the same 5020 error. I'm not certain now if that was the case before, just
that the text was different, but it probably was.

I've run into differences before, where early and late binding acted
differently when automating Word and Access, and with a third-party library
called AutoIT. Sometimes one method simply doesn't work and the other does,
but maybe I'm just doing something wrong.

The later is called "late binding". You don't need a
reference. The type is resolved at runtime and it
match different versions.

I swap this Access mdb between several machines, some running 2003, some
2007. I use automation of a Word template from Access, automation of Access
back from the Word template and early binding in both. When I compile on a
2003 machine and move to a 2007 machine, everything works, but when I
compile on a 2007 machine, then carry it back to a 2003 machine, the Word
reference is broken (missing) and I have to reset it. This is an app that
will be going into service and will be moved between machines in the same
way by users. If I used late binding (correctly), would that solve this
issue permanently? The speed difference is not significant and if it will
fix this problem, I'll live without IntelliSense, or design with early
binding, then switch to late for the final user's version.

Pete
 
hi Pete,

Petr said:
If I used late binding (correctly),
would that solve this issue permanently? The speed difference is not
significant and if it will fix this problem, I'll live without
IntelliSense, or design with early binding, then switch to late for the
final user's version.
Yes. Late binding resolves automatically against the "current" installed
version. The only caveat is only to use methods which all possible
versions have in common. For Office automation I'm using 2003 as
development plattform in most cases.

While developing I'm using early binding which is later removed. So the
code looks often like this:

Dim app As Object ' Excel.Application

Set app = CreateObject("Excel.Application") ' new Excel.Application()

The only drawback is, that I'm using named constants which I have to
redeclare after dropping the libraries reference.


mfG
--> stefan <--
 
Great, thanks Stefan. The early-switch-to-late just occurred to me and I
wasn't sure if it was a reasonable way to proceed. It's good to know that it
is a viable development procedure and that it works correctly when done
right. Many thanks for the help.

Pete
 
Back
Top