script to compile database

lundi 4 mai 2015

I have been googling on how to auto compile database using code.

I found the following 2 scripts and neither work.
As per my third script I set up a script that would error upon compiling, yet neither of these scripts work.
Yet when I manually compile the database it finds the error.

Code:

Sub test2()
Dim strProjectPath As String, strProjectName As String
strProjectName = Application.CurrentProject.Name
strProjectPath = Application.CurrentProject.Path
SysCmd 602, strProjectPath, strProjectName 'compact DB

SysCmd 504, 16483 ' compile
End Sub

Next

Code:

Sub CompileAll()
  ' Comments  : Use menu commands to compile and optionally save modules
  ' Parameters: fSave - True to compile and save, False to only compile
  ' Returns  : nothing
  '
  Dim dbsCurrent As Database
  Dim conTmp As Container
  Dim docTmp As Document
  Dim fFound As Boolean
  Dim intOpenedType As Integer
  Dim strOpenedName As String
  Dim strName As String

  Set dbsCurrent = CurrentDb()
  fFound = False

  ' Need to open a module, so let's find one
  Set conTmp = dbsCurrent.Containers("Modules")
  If conTmp.Documents.Count > 0 Then
    DoCmd.OpenModule conTmp.Documents(0).Name
    intOpenedType = acModule
    strOpenedName = conTmp.Documents(0).Name
    fFound = True
  Else

    ' No modules, try for a form
    Set conTmp = dbsCurrent.Containers("Forms")

    ' Need to iterate to ignore lightweight forms
    For Each docTmp In conTmp.Documents
      strName = docTmp.Name
      DoCmd.OpenForm strName, acDesign, , , , acHidden
      If Forms(strName).HasModule Then
        fFound = True
        intOpenedType = acForm
        strOpenedName = strName
        Exit For
      End If
    Next docTmp

    ' No form class modules, try for a report
    If Not fFound Then
      Set conTmp = dbsCurrent.Containers("Reports")

      ' Need to iterate to ignore lightweight reports
      For Each docTmp In conTmp.Documents
        strName = docTmp.Name
        DoCmd.OpenReport strName, acDesign
        If Reports(strName).HasModule Then
          fFound = True
          intOpenedType = acReport
          strOpenedName = strName
          Exit For
        End If
      Next docTmp
    End If
  End If

  ' If the database contains at least one module, it
  ' should now be open, so lets to a Compile All{Save All}
  If fFound Then
    If fSave Then
      DoCmd.RunCommand acCmdCompileAndSaveAllModules
      MsgBox "All modules have been compiled and saved."
    Else
      DoCmd.RunCommand acCmdCompileAllModules
      MsgBox "All modules have been compiled."
    End If

    DoCmd.Close intOpenedType, strOpenedName
  Else
    Beep
    MsgBox "This project has no modules to compile."
  End If

End Sub

script to cause compiler to fail

Code:

Sub test()
lalalala
End Sub

script to compile database

0 commentaires:

Enregistrer un commentaire

Labels