Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - How to open Access in VBS and leave it open

Reply
 
Old 08-22-2008   #1 (permalink)
Bob Bridges


 
 

How to open Access in VBS and leave it open

Part II: I have a daily task that involves using an Access database, a .txt
file in Notepad and a folder, the same ones every day. I've written a few
VBS programs but I'm trying to learn more. So I set out to write myself a
simple VBS to open all three for me each time. But it seems each of the
supposedly simple steps presents me with an unexpected difficulty. Let's
talk about the Access database:

I've tried two ways. I'm used to writing VBS programs for Excel, so I can
easily enough open an Access.Application object, then open my specified
database, then open the database's main form; this seems to work just like in
Excel. The problem is that unlike Excel, when the VBS terminates the Access
application goes away.

In a way this makes sense: The Access.Application object was created by VBS
and when VBS stops the object is deleted, right. So I shouldn't be surprised
that the application is deleted too. But it doesn't happen in Excel. What's
the difference between them, and how can I get VBS to open Access and leave
it open when VBS stops?

Alternatively, I tried using Wscript.Shell like this:

Set acc = WScript.CreateObject("WScript.Shell")
acc.Run "C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE"

But when I run this, WSH tells me "The system cannot find the file
specified" (error code 80070002). It works fine from Run, though.

Which technique should I use, and what am I doing wrong?

My System SpecsSystem Spec
Old 08-22-2008   #2 (permalink)
James Whitlow


 
 

Re: How to open Access in VBS and leave it open

"Bob Bridges" <BobBridges@xxxxxx> wrote in message
news:893DA85B-F7A7-42E2-B0BC-83B83B086614@xxxxxx
Quote:

> Alternatively, I tried using Wscript.Shell like this:
>
> Set acc = WScript.CreateObject("WScript.Shell")
> acc.Run "C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE"
>
> But when I run this, WSH tells me "The system cannot find the file
> specified" (error code 80070002). It works fine from Run, though.
Since your path has spaces in it, you need to wrap it inside quotes:

Set acc = WScript.CreateObject("WScript.Shell")
acc.Run """C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE"""


My System SpecsSystem Spec
Old 08-23-2008   #3 (permalink)
Bob Bridges


 
 

Re: How to open Access in VBS and leave it open

Oh, of course; that's what caused the problem in Part I, too, though it was a
completely different window and path. Thanks.

Anyone know about the other part -- about opening Access.Application in VBS
and having it stay around after the VBS terminates?

--- "James Whitlow" wrote:
Quote:

> Since your path has spaces in it, you need to wrap it inside quotes:
>
> Set acc = WScript.CreateObject("WScript.Shell")
> acc.Run """C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE"""
Quote:

> "Bob Bridges" <BobBridges@xxxxxx> wrote in message
> news:893DA85B-F7A7-42E2-B0BC-83B83B086614@xxxxxx
Quote:

> > Alternatively, I tried using Wscript.Shell like this:
> >
> > Set acc = WScript.CreateObject("WScript.Shell")
> > acc.Run "C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE"
> >
> > But when I run this, WSH tells me "The system cannot find the file
> > specified" (error code 80070002). It works fine from Run, though.
My System SpecsSystem Spec
Old 08-25-2008   #4 (permalink)
Bob Barrows [MVP]


 
 

Re: How to open Access in VBS and leave it open

Bob Bridges wrote:
Quote:

> Oh, of course; that's what caused the problem in Part I, too, though
> it was a completely different window and path. Thanks.
>
> Anyone know about the other part -- about opening Access.Application
> in VBS and having it stay around after the VBS terminates?
You can use OLE Automation:

set obj=createobject("access.application")
obj.visible = true

If you wanted your script to close Access, you would need to use the
Quit method:
obj.Quit

In your case, you want to leave it running so simply don't execute that
statement.


--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


My System SpecsSystem Spec
Old 08-25-2008   #5 (permalink)
James Whitlow


 
 

Re: How to open Access in VBS and leave it open

"Bob Barrows [MVP]" <reb01501@xxxxxx> wrote in message
news:%236QqhQrBJHA.4580@xxxxxx
Quote:

> Bob Bridges wrote:
Quote:

>> Oh, of course; that's what caused the problem in Part I, too, though
>> it was a completely different window and path. Thanks.
>>
>> Anyone know about the other part -- about opening Access.Application
>> in VBS and having it stay around after the VBS terminates?
>
> You can use OLE Automation:
>
> set obj=createobject("access.application")
> obj.visible = true
>
> If you wanted your script to close Access, you would need to use the
> Quit method:
> obj.Quit
>
> In your case, you want to leave it running so simply don't execute that
> statement.
I played around with this over the weekend and my results were the same
as Bob's. If I used 'CreateObject' in VBScript to instantiate Access, that
instance terminates along with VBScript, unlike Excel, Internet Explorer &
other objects I have used. They remain open unless the 'Quit' method is
called. I thought perhaps crashing (using 'Err.Raise1') the VBScript instead
of letting it terminate normally might orphan that instance of Access, but
it did not work. As soon as the error dialogue is closed, Access disappears.


My System SpecsSystem Spec
Old 08-25-2008   #6 (permalink)
Bob Barrows [MVP]


 
 

Re: How to open Access in VBS and leave it open

James Whitlow wrote:
Quote:

> "Bob Barrows [MVP]" <reb01501@xxxxxx> wrote in message
> news:%236QqhQrBJHA.4580@xxxxxx
Quote:

>> Bob Bridges wrote:
Quote:

>>> Oh, of course; that's what caused the problem in Part I, too, though
>>> it was a completely different window and path. Thanks.
>>>
>>> Anyone know about the other part -- about opening Access.Application
>>> in VBS and having it stay around after the VBS terminates?
>>
>> You can use OLE Automation:
>>
>> set obj=createobject("access.application")
>> obj.visible = true
>>
>> If you wanted your script to close Access, you would need to use the
>> Quit method:
>> obj.Quit
>>
>> In your case, you want to leave it running so simply don't execute
>> that statement.
>
> I played around with this over the weekend and my results were
> the same as Bob's. If I used 'CreateObject' in VBScript to
> instantiate Access, that instance terminates along with VBScript,
> unlike Excel, Internet Explorer & other objects I have used. They
> remain open unless the 'Quit' method is called. I thought perhaps
> crashing (using 'Err.Raise1') the VBScript instead of letting it
> terminate normally might orphan that instance of Access, but it did
> not work. As soon as the error dialogue is closed, Access disappears.
My mistake - I guess my memory is failing me ... I could have sworn it
would stay open if Quit was not called
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


My System SpecsSystem Spec
Old 08-25-2008   #7 (permalink)
Bob Bridges


 
 

Re: How to open Access in VBS and leave it open

I did some playing around with it, too, an hour or so ago. I think I have
the answer: When I open a database in Access, I have the same problem we're
all talking about here, but when I open a form in that database, it acts like
Excel, ie the app stays open even after the script is completed. And of
course in Excel, my VBS programs always left an xls open when they were
through creating it, so I could check it and perhaps manipulate it on the
spot.

So I'm guessing that almost the same thing is happening in Access as in
Excel, it's just that an AccessObject has to be open to keep Access from
going away when the Application object does. Maybe Excel would close, too,
if I created it in VBS but never opened a particular workbook.

And this just in: When I open it in this way, and then close the form, the
rest of the app disappears immediately. So it's just that form that's
keeping Access alive.

If I learn more, I'll pass it on.

"Bob Barrows [MVP]" wrote:
Quote:

> James Whitlow wrote:
Quote:

> > "Bob Barrows [MVP]" <reb01501@xxxxxx> wrote in message
> > news:%236QqhQrBJHA.4580@xxxxxx
Quote:

> >> Bob Bridges wrote:
> >>> Oh, of course; that's what caused the problem in Part I, too, though
> >>> it was a completely different window and path. Thanks.
> >>>
> >>> Anyone know about the other part -- about opening Access.Application
> >>> in VBS and having it stay around after the VBS terminates?
> >>
> >> You can use OLE Automation:
> >>
> >> set obj=createobject("access.application")
> >> obj.visible = true
> >>
> >> If you wanted your script to close Access, you would need to use the
> >> Quit method:
> >> obj.Quit
> >>
> >> In your case, you want to leave it running so simply don't execute
> >> that statement.
> >
> > I played around with this over the weekend and my results were
> > the same as Bob's. If I used 'CreateObject' in VBScript to
> > instantiate Access, that instance terminates along with VBScript,
> > unlike Excel, Internet Explorer & other objects I have used. They
> > remain open unless the 'Quit' method is called. I thought perhaps
> > crashing (using 'Err.Raise1') the VBScript instead of letting it
> > terminate normally might orphan that instance of Access, but it did
> > not work. As soon as the error dialogue is closed, Access disappears.
>
> My mistake - I guess my memory is failing me ... I could have sworn it
> would stay open if Quit was not called
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
>
>
My System SpecsSystem Spec
Old 08-25-2008   #8 (permalink)
Bob Barrows [MVP]


 
 

Re: How to open Access in VBS and leave it open



--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
"Bob Bridges" <rhbridg.RemoveThisNode@xxxxxx> wrote in message
news:B0D17071-DD58-47F9-AB28-7950C1306E30@xxxxxx
Quote:

> I did some playing around with it, too, an hour or so ago. I think I
have
Quote:

> the answer: When I open a database in Access, I have the same problem
we're
Quote:

> all talking about here, but when I open a form in that database, it
acts like
Quote:

> Excel, ie the app stays open even after the script is completed. And
of
Quote:

> course in Excel, my VBS programs always left an xls open when they
were
Quote:

> through creating it, so I could check it and perhaps manipulate it on
the
Quote:

> spot.
>
> So I'm guessing that almost the same thing is happening in Access as
in
Quote:

> Excel, it's just that an AccessObject has to be open to keep Access
from
Quote:

> going away when the Application object does. Maybe Excel would close,
too,
Quote:

> if I created it in VBS but never opened a particular workbook.
>
> And this just in: When I open it in this way, and then close the
form, the
Quote:

> rest of the app disappears immediately. So it's just that form that's
> keeping Access alive.
>
> If I learn more, I'll pass it on.
>
> "Bob Barrows [MVP]" wrote:
>
Quote:

> > James Whitlow wrote:
Quote:

> > > "Bob Barrows [MVP]" <reb01501@xxxxxx> wrote in message
> > > news:%236QqhQrBJHA.4580@xxxxxx
> > >> Bob Bridges wrote:
> > >>> Oh, of course; that's what caused the problem in Part I, too,
though
Quote:
Quote:
Quote:

> > >>> it was a completely different window and path. Thanks.
> > >>>
> > >>> Anyone know about the other part -- about opening
Access.Application
Quote:
Quote:
Quote:

> > >>> in VBS and having it stay around after the VBS terminates?
> > >>
> > >> You can use OLE Automation:
> > >>
> > >> set obj=createobject("access.application")
> > >> obj.visible = true
> > >>
> > >> If you wanted your script to close Access, you would need to use
the
Quote:
Quote:
Quote:

> > >> Quit method:
> > >> obj.Quit
> > >>
> > >> In your case, you want to leave it running so simply don't
execute
Quote:
Quote:
Quote:

> > >> that statement.
> > >
> > > I played around with this over the weekend and my results were
> > > the same as Bob's. If I used 'CreateObject' in VBScript to
> > > instantiate Access, that instance terminates along with VBScript,
> > > unlike Excel, Internet Explorer & other objects I have used. They
> > > remain open unless the 'Quit' method is called. I thought perhaps
> > > crashing (using 'Err.Raise1') the VBScript instead of letting it
> > > terminate normally might orphan that instance of Access, but it
did
Quote:
Quote:
Quote:

> > > not work. As soon as the error dialogue is closed, Access
disappears.
Quote:
Quote:

> >
> > My mistake - I guess my memory is failing me ... I could have sworn
it
Quote:
Quote:

> > would stay open if Quit was not called
> > --
> > Microsoft MVP -- ASP/ASP.NET
> > Please reply to the newsgroup. The email account listed in my From
> > header is my spam trap, so I don't check it very often. You will get
a
Quote:
Quote:

> > quicker response by posting to the newsgroup.
> >
> >
> >

My System SpecsSystem Spec
Old 08-25-2008   #9 (permalink)
Bob Bridges


 
 

Re: How to open Access in VBS and leave it open

It looks like you meant to reply here, but hit the button too soon...?

--- "Bob Barrows [MVP]" wrote:
Quote:

>
> --
> Microsoft MVP -- ASP/ASP.NET
> Please reply to the newsgroup. The email account listed in my From
> header is my spam trap, so I don't check it very often. You will get a
> quicker response by posting to the newsgroup.
>
> --- "Bob Bridges" <rhbridg.RemoveThisNode@xxxxxx> wrote:
Quote:

> > I did some playing around with it, too, an hour or so ago. I think I
> > have the answer: When I open a database in Access, I have the same
> > problem we're all talking about here, but when I open a form in that
> > database, it acts like Excel, ie the app stays open even after the script
> > is completed. And of course in Excel, my VBS programs always left an
> > xls open when they were through creating it, so I could check it and
> > perhaps manipulate it on the spot.
> >
> > So I'm guessing that almost the same thing is happening in Access as
> > in Excel, it's just that an AccessObject has to be open to keep Access
> > from going away when the Application object does. Maybe Excel would
> > close, too, if I created it in VBS but never opened a particular workbook.
> >
> > And this just in: When I open it in this way, and then close the
> > form, the rest of the app disappears immediately. So it's just that form
> > that's keeping Access alive.
> >
> > If I learn more, I'll pass it on.
> >
> > --- "Bob Barrows [MVP]" wrote:
Quote:

> > > My mistake - I guess my memory is failing me ... I could have sworn
> > > it would stay open if Quit was not called
> >
Quote:

> > > --- James Whitlow wrote:
> > > > I played around with this over the weekend and my results were
> > > > the same as Bob's. If I used 'CreateObject' in VBScript to
> > > > instantiate Access, that instance terminates along with VBScript,
> > > > unlike Excel, Internet Explorer & other objects I have used. They
> > > > remain open unless the 'Quit' method is called. I thought perhaps
> > > > crashing (using 'Err.Raise1') the VBScript instead of letting it
> > > > terminate normally might orphan that instance of Access, but it
> > > > did not work. As soon as the error dialogue is closed, Access
> > > > disappears.
Quote:
Quote:
Quote:

> > > > --- "Bob Barrows [MVP]" <reb01501@xxxxxx> wrote:
> > > >> You can use OLE Automation:
> > > >>
> > > >> set obj=createobject("access.application")
> > > >> obj.visible = true
> > > >>
> > > >> If you wanted your script to close Access, you would need to use
> > > >> the Quit method:
> > > >>
> > > >> obj.Quit
> > > >>
> > > >> In your case, you want to leave it running so simply don't
> > > >> execute that statement.
My System SpecsSystem Spec
Old 08-25-2008   #10 (permalink)
Bob Barrows [MVP]


 
 

Re: How to open Access in VBS and leave it open

Actually, meant to cancel it but hit Send by mistake - I haven't come up
with a solution.

Bob Bridges wrote:
Quote:

> It looks like you meant to reply here, but hit the button too soon...?
>
> --- "Bob Barrows [MVP]" wrote:
Quote:

>>
>> --
>> Microsoft MVP -- ASP/ASP.NET
>> Please reply to the newsgroup. The email account listed in my From
>> header is my spam trap, so I don't check it very often. You will get
>> a quicker response by posting to the newsgroup.
>>
>> --- "Bob Bridges" <rhbridg.RemoveThisNode@xxxxxx> wrote:
Quote:

>>> I did some playing around with it, too, an hour or so ago. I think
>>> I have the answer: When I open a database in Access, I have the
>>> same problem we're all talking about here, but when I open a form
>>> in that database, it acts like Excel, ie the app stays open even
>>> after the script is completed. And of course in Excel, my VBS
>>> programs always left an
>>> xls open when they were through creating it, so I could check it and
>>> perhaps manipulate it on the spot.
>>>
>>> So I'm guessing that almost the same thing is happening in Access as
>>> in Excel, it's just that an AccessObject has to be open to keep
>>> Access from going away when the Application object does. Maybe
>>> Excel would close, too, if I created it in VBS but never opened a
>>> particular workbook.
>>>
>>> And this just in: When I open it in this way, and then close the
>>> form, the rest of the app disappears immediately. So it's just
>>> that form that's keeping Access alive.
>>>
>>> If I learn more, I'll pass it on.
>>>
>>> --- "Bob Barrows [MVP]" wrote:
>>>> My mistake - I guess my memory is failing me ... I could have sworn
>>>> it would stay open if Quit was not called
>>>
>>>> --- James Whitlow wrote:
>>>>> I played around with this over the weekend and my results were
>>>>> the same as Bob's. If I used 'CreateObject' in VBScript to
>>>>> instantiate Access, that instance terminates along with VBScript,
>>>>> unlike Excel, Internet Explorer & other objects I have used. They
>>>>> remain open unless the 'Quit' method is called. I thought perhaps
>>>>> crashing (using 'Err.Raise1') the VBScript instead of letting it
>>>>> terminate normally might orphan that instance of Access, but it
>>>>> did not work. As soon as the error dialogue is closed, Access
>>>>> disappears.
>
Quote:
Quote:

>>>>> --- "Bob Barrows [MVP]" <reb01501@xxxxxx> wrote:
>>>>>> You can use OLE Automation:
>>>>>>
>>>>>> set obj=createobject("access.application")
>>>>>> obj.visible = true
>>>>>>
>>>>>> If you wanted your script to close Access, you would need to use
>>>>>> the Quit method:
>>>>>>
>>>>>> obj.Quit
>>>>>>
>>>>>> In your case, you want to leave it running so simply don't
>>>>>> execute that statement.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Can't open/save attachments in windows mail. Or open or run downlo Vista mail
Cannot open password protected Access DB .NET General
can't open access control editor. Access is denied General Discussion
Word 2002 and Open Office Writer Open Command Crashes App Vista General
Can't Open Hyperlinks from Access 07 Vista security


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

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