Users Online

· Guests Online: 27

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

MS Access bootstrap - Create Access projects programmatically

MS Access bootstrap - Create Access projects programmatically

 

MS Access bootstrap - Create Access projects programmatically

accessBootstrap.vbs is a light weight VBScript scriptlet that can be used to programmatically bootstrap the creation of MS Access applications.

accessBootstrap.vbs

Currently, accessBootstrap.vbs exhibits three functions:
  • createDB: to create the access ( .accdb) file. The sub takes a string that contains the path plus filename of the resulting file.
  • insertModule: to insert a VBA module
  • addReference: to add a reference to a type lib (see here).
'
' Provide the functionality to create Access applications from
' the command line.
'
' The functions in this file should be called from a *.wsf file.
'

option explicit

dim accessFile
dim acc       ' as access.application

dim fso
set fso = createObject("Scripting.FileSystemObject")
  
dim vb_editor ' as vbe

dim vb_proj   ' as VBProject
dim vb_comps  ' as VBComponents

sub createDB(accessFile) ' {


    if fso.fileExists(accessFile) then
       wscript.echo accessFile & " already exists. Deleting it."
       fso.deleteFile(accessFile)
    end if
  
  
    set acc = createObject("Access.Application")
    acc.newCurrentDatabase accessFile, 0 ' 0: acNewDatabaseFormatUserDefault

    acc.visible     = true
    acc.userControl = true ' http://stackoverflow.com/q/36282024/180275

    set vb_editor = acc.vbe
    set vb_proj   = vb_editor.activeVBProject
    set vb_comps  = vb_proj.vbComponents

  '
  ' Add (type lib) reference to "Microsoft Visual Basic for Applications Extensibility 5.3"
  '
    call addReference("{0002E157-0000-0000-C000-000000000046}", 5, 3)


end sub ' }

sub insertModule(moduleFilePath, moduleName, moduleType) ' {
 '
 '  moduleType:
 '    1 = vbext_ct_StdModule
 '    2 = vbext_ct_ClassModule
 '
 '  Compare with https://renenyffenegger.ch/notes/development/languages/VBA/modules/Common/00_ModuleLoader
 '    

    if not fso.fileExists(moduleFilePath) then ' {
       wscript.echo moduleFilePath & " does not exist!"
       wscript.quit
    end if ' }

    dim mdl ' as VBComponent
    set mdl = vb_comps.add(1) ' 1 = vbext_ct_StdModule
   
    wscript.echo("adding scriptFile " & ModuleFilePath)
    mdl.codeModule.addFromFile (ModuleFilePath)
   
    mdl.name = moduleName
   
    acc.doCmd.close 5, mdl.name, 1 ' 5=acModule, 1=acSaveYes

end sub ' }

sub addReference(guid, major, minor) ' {
  '
  ' guid identfies a type lib. Thus, the guid should be found in the
  ' Registry under HKEY_CLASSES_ROOT\TypeLib\
  '
  ' Note: guid probably needs the opening and closing curly paranthesis.
  '
    call acc.VBE.activeVbProject.references.addFromGuid (guid, major, minor)
end sub ' }
Github respository MS-Access-bootstrap, path: /accessBootstrap.vbs

Test

The wsf file

The following wsf file uses accessBootstrap.vbs to insert a VBA module and then call a sub ( createApplication) in the inserted module.
In a real world case, createApplication would then create the Access application.
<!--

   Create access application from commandline with VBScript:

       cscript createAccess.wsf

-->
<job id="IsThisIdRequired">

   <script language="VBScript" src="../accessBootstrap.vbs" /> -- >

   <script language="VBScript">

      option explicit

      dim wshShell
      set wshShell = createObject("WScript.Shell")

      dim projectRootDir
          projectRootDir = wshShell.CurrentDirectory & "\" ' "p:\ath\to\project\root\"

      wscript.echo("projectRootDir = " & projectRootDir)

      call createDB(projectRootDir & "created.accdb")

    '
    ' Add (TypeLib) References to
    '          '          '
      call addReference("{420B2830-E718-11CF-893D-00A0C9054228}", 1, 0) ' Microsoft Scripting Runtime

    '
    ' Insert a module. The 3rd parameter (1) indicates a »normal« module (vbext_ct_StdModule)
    ' rather than a class module (vbext_ct_ClassModule)
    '
      call insertModule(projectRootDir & "init.bas"                              , "init"           , 1)

    '
    ' Call a function that is defined in the inserted init.bas:
    '
      acc.run("createApplication")


      wscript.echo("The end")

   </script>

</job>
Github respository MS-Access-bootstrap, path: /test/createAccess.wsf

The inserted VBA module

The (simple) inserted module with the createApplication sub that is called by the wsf file (viaaccessBootstrap.vbs)
option explicit

sub createApplication() ' {

    msgBox "Now, the application should be created"

end sub ' }
Github respository MS-Access-bootstrap, path: /test/init.bas

 

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.75 seconds
10,799,790 unique visits