Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting this vba to vbs

Posted on 2011-09-27
5
Medium Priority
?
257 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 2000 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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

719 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