In my previous post launch-open-an-ms-access-database-using-a-vbscript I went over the basic concept of using a VBScript to launch a database.
In this post I would like to demonstrate the true power of VBScript for this type of application.
Say, you are a developer within a structured company network environment and you want to standardize the front-end setup on your users computers without having to sit down at each of their computers, one by one. Well, VBScript to the rescue!
The script below may seem scary, but really isn’t. It start by determining the user’s My Documents location. This is where I have chosen to place the Front-End application of the database. You could always alter this aspect, but it has served me very well in numerous routines. Once it has determine the location it determines the location of the VBScript itself (which will be installed with the Master copy of the Front-end). As such, it copies the Master copy to the user’s My Documents. Then it determines the location of the msaccess.exe required to launch the database and then finally launches it!
Once again, I do hope this helps someone out there as I found it hard to come across this information several years ago when I needed it.
'*******************************************************************************
'Date: 2008-05-27
'Author: Daniel Pineault / CARDA Consultants Inc.
'Purpose: This script should be located on a network share in the same
' directory as the Front-End which it will automatically copy
' to each user's MyDoc\Subfolder\ and then launch
' Give your users a link to this script and it will do the rest
'Copyright: You are free to use the following code as you please so long as
' this header remains unaltered.
'Revision: 2008-05-27 Initial Release
'*******************************************************************************
Const MY_DOCUMENTS = &H5&
Const PROGRAM_FILES = &H26&
Dim objShell
Dim objFolder
Dim objFolderItem
Dim objNetwork
Dim objFSO
Dim objShellDb
Dim DelFoldr
Dim sMyDocPath
Dim sProgPath
Dim sVBSPath
Dim sAccPath
Dim sFrontEnd
Dim sFolder
Dim sSec
Dim sUser
Dim sPath
Dim sComTxt
'Specify the Fullpath and filename of the database to launch
sFrontEnd = "test.mdb" 'Database name to open
sFolder = "Databases" 'MyDoc subfolder where the
'front-end will be copied to
'If your database is secured by an mdw file specify it below, otherwise
'leave its value blank
sSec = "Security.mdw"
'Determine the location/path of the user's MyDocuments folder
'*******************************************************************************
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_DOCUMENTS)
Set objFolderItem = objFolder.Self
sMyDocPath = objFolderItem.Path 'My Documents path
sPath = sMyDocPath & "\" & sFolder & "\"'Path to front-end
Set objFolder = objShell.Namespace(PROGRAM_FILES)
Set objFolderItem = objFolder.Self
sProgPath = objFolderItem.Path 'Program Files Path
'Determine path of this VBScript
'*******************************************************************************
sVBSPath = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName) _
- (Len(WScript.ScriptName) + 1)))
'Ensure lastest version of front-end is installed
Set objNetwork = CreateObject("Wscript.Network")
sUser = objNetwork.UserName 'User's network username
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Copy a network version of the Front-end to the MyDocs/SubFolder
'*******************************************************************************
'Create application folder if it does not already exist
If objFSO.FolderExists(sPath) = False then
Set objFolderCreate = objFSO.CreateFolder(sPath)
End If
'Delete the existing copy to ensure we have a fresh copy
If objFSO.FileExists(sPath & sFrontEnd) then
'Delete the file to copy a new fresh copy
Set DelFile = objFSO.GetFile(sPath & sFrontEnd)
DelFile.Delete
Do While objFSO.FileExists(sPath & sFrontEnd) = True
WScript.Sleep 100
Loop
End if
'Copy a fresh copy of the FE
objFSO.CopyFile sVBSPath & "\" & sFrontEnd, sPath & _
sFrontEnd, OverWriteExisting
Do While objFSO.FileExists(sPath & sFrontEnd) = False
WScript.Sleep 100
Loop
'Determine the location of the MS Access executable
'*******************************************************************************
' Set objShellDb = CreateObject("WScript.Shell")
' 'Determine in which folder the Access executable is located
' If objFSO.FileExists(sProgPath &_
' "\Microsoft Office\OFFICE11\msaccess.exe") Then
' sAccPath = sProgPath & "\Microsoft Office\OFFICE11"
' Elseif objFSO.FileExists(sProgPath &_
' "\Microsoft Office\OFFICE10\msaccess.exe") Then
' sAccPath = sProgPath & "\Microsoft Office\OFFICE10"
' Elseif objFSO.FileExists(sProgPath &_
' "\Microsoft Office\OFFICE\msaccess.exe") Then
' sAccPath = sProgPath & "\Microsoft Office\OFFICE"
' End if
sAppEXE = "MSACCESS.EXE"
sRegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\" & sAppEXE & "\Path"
sAccPath = objShellDb .RegRead(sRegKey)
'Launch database
'*******************************************************************************
'Build the command to launch the database
sComTxt = chr(34) & sAccPath & "msaccess.exe" & chr(34) &_
" " & chr(34) & sPath & sFrontEnd & chr(34)
if isNull(sSec)=False AND sSec<>"" Then
sComTxt = sComTxt & " /wrkgrp " & chr(34) & sVBSPath &_
"\" & sSec & chr(34)
if isNull(sUser)=False AND sUser<>"" Then
sComTxt = sComTxt & " /user " & sUser
End if
End if
objShellDb.Run sComTxt 'Launch the database
Please assist
tried this code & get the error below.
Thanks.
—————————
Windows Script Host
—————————
Script: I:\AS400\Checklist\Access_Checklist\ajith.vbs
Line: 77
Char: 5
Error: Path not found
Code: 800A004C
Source: Microsoft VBScript runtime error
—————————
OK
—————————
I’d start by using a MsgBox to display the variable values to see where the script is having an issue and work backwards from there.
MsgBox sVBSPath & “\” & sFrontEnd
MsgBox sPath & sFrontEnd
etc…
Hi,
I’m having issues with this script and was wondering if you could help. With troubleshooting, I’ve gotten as far as determining that I think the issue is because the Microsoft Access executable is in a folder called Office14 in the Program files (x86) folder. I had originally tried just adding an ElseIf with the folder name Office14, but the problem is sProgPath stores Program Files, not the x86 file folder. How do I modify the script to accommodate for the situations where Access is installed in the x86 Program files folder? I’m not sure how to change the way it calls the folder as I’m not sure how that part works. And it’d be awesome to check both folders in case it is still installed in the Program Files folder.
Any help would be much appreciated 🙂
Yes, this is an old script that I need to update. Basically, you need to change the entire ‘Determine the location of the MS Access executable’ section and replace it with code similar to what is found at http://www.devhut.net/2012/07/10/vba-determine-executable-path-for-given-application/. Then the code should adapt to any setup.
Thank you very much, that worked. I had another question. I can get this script to work if I have the script and the database in the same folder, and specify only the database name and not the path to the database for sFrontEnd. Is there a way to make this script work if I want the script to be saved in a different folder from the database? I don’t want users opening the front end directly, so I want it somewhere else from the script. Can this script be used that way? It fails if I specify the path to the database in the variable.
Kristen,
While it is possible to change the script to do as you are thinking, the way I’ve always worked was to setup my network folder, install the BE and networked FE and the launch script and then create a shortcut on each user’s desktop back to the launch script. Then all they do is double-click the shortcut and everything is taken care of for them. They don’t ever need to know the actual path to the database.
hi
can i Opening a protected Access database with a workgroup
like (SYSTEM.MDW)
thank
Simply update the sSec variable with the name of your mdw file.