Solved

Converting this vba to vbs

Posted on 2011-09-27
5
255 Views
Last Modified: 2012-05-12
The code below opens an access database by calling the Access Run Time.  This is exactly what I need to do, but I need to do it from vbScript.  Can anyone help by translating the below code into working .vbs for me?

Thanks.
Sub OpenRunTime()
     Dim accpath As String, dbpath As String
     On Error Resume Next
     dbpath = "C:\My Application\MyApp.mdb"
     Set objAccess = GetObject(dbpath)
     If Err <> 0 Then
       If Dir(dbpath) = "" Then 'dbpath is not valid
         MsgBox "Couldn't find database."
         Exit Sub
       Else  'The full version of Microsoft Access is not installed.
         accpath = "C:\Program Files\Common Files\Microsoft Shared" & _
              "\Microsoft Access Runtime\MSAccess.exe"
         If Dir(accpath) = "" Then
            MsgBox "Couldn't find Microsoft Access."
            Exit Sub
         Else
            Shell pathname:=accpath & " " & Chr(34) & dbpath & Chr(34), _
              windowstyle:=6
            Do 'Wait for shelled process to finish
              Err = 0
              Set objAccess = GetObject(dbpath)
            Loop While Err <> 0
         End If
       End If
     End If
   End Sub

Open in new window

0
Comment
Question by:snyperj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:GundogTrainer
ID: 36710199

There are a few bit that need to be done differently but...

 Can you confirm this is what is should do:

1, check C:\My Application\MyApp.mdb exists
2. check MSAccess.exe exists
3. spawn msaccess.exe to open the mdb file in full screen and wait for it to exit.
4. quit
0
 
LVL 8

Accepted Solution

by:
GundogTrainer earned 500 total points
ID: 36710437
Just a quick explanation of the nasty string in the Wshshell bit at the end.

As its executing it from the shell script it needs to be in quotes to maintain names with spaces, I know you can do it with 2 double quotes but it get confusing real quick if you have any problems so I choose to use char(34) rather than being too clever.
openruntime

Sub OpenRunTime
     Dim accpath , dbpath
'    On Error goto 0
     dbpath = "C:\My Application\MyApp.mdb"
     accpath = "C:\Program Files\Common Files\Microsoft Shared\Microsoft Access Runtime\MSAccess.exe"
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     set LogFile=objfso.opentextfile("dvdlog.txt",2,True)

       If objFSO.FileExists(dbpath) = false then
         MsgBox "Couldn't find database."
         exit sub
       end if
       If objFSO.FileExists(accpath) = false then
            MsgBox "Couldn't find Microsoft Access."
         exit sub
       End If

       'If the database of Exe didnt exist we wount have got here !
     Set WshShell = WScript.CreateObject("WScript.Shell")
     Return = WshShell.Run(chr(34) & accpath & chr(34) & " " & chr(34) & dbpath & chr(34) , 3, true)
   End Sub

Open in new window

0
 

Author Comment

by:snyperj
ID: 36710795
Great job thank you very much- works like a champ.

Can you explain to me what this line does?

set LogFile=objfso.opentextfile("dvdlog.txt",2,True)



0
 
LVL 8

Expert Comment

by:GundogTrainer
ID: 36710835
Doh...
thats the old grabed a line of code that I always use when opening FileSystem Objects and forget to delete it !!! (I tend to keep a log in most of my scripts as an audit trail) its doing nothing - please delete it with my appologies.


0
 

Author Comment

by:snyperj
ID: 36710852
No worries, I figured as much!  Thanks again.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question