Solved

Converting this vba to vbs

Posted on 2011-09-27
5
246 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now