VBA/Excel/Access/Word/Access/CurrentProject

Материал из VB Эксперт
Перейти к: навигация, поиск

CurrentProject.Connection

   <source lang="vb">

Public Sub usingexe()

 Dim cmd As ADODB.rumand
 Dim strSQL As String
 Dim recs As Long
 
 Set cmd = New ADODB.rumand
 strSQL = "UPDATE tblCompany SET CompanyName = "G" WHERE CompanyName = "A""
 Set cmd.ActiveConnection = CurrentProject.Connection
 cmd.rumandText = strSQL
 cmd.rumandType = adCmdText
 cmd.Execute RecordsAffected:=recs, Options:=adExecuteNoRecords
 Debug.Print recs & " Updated"
 Set cmd = Nothing

End Sub

</source>
   
  


Get current project from Application object

   <source lang="vb">

Option Compare Database Option Explicit

Public Sub ShowObjects()

 Dim objAO As AccessObject
 Dim objCP As Object
 Set objCP = Application.CurrentProject
 For Each objAO In objCP.AllReports
  Debug.Print objAO.Name
 Next

End Sub

</source>
   
  


Iterates through the AllForms collection of the CurrentProject, printing the name of each form

   <source lang="vb">

Sub IterateAllForms()

   Dim vnt As Variant
   With CurrentProject
       For Each vnt In .AllForms
           Debug.Print vnt.Name
       Next vnt
   End With

End Sub

</source>
   
  


Iterate through all modules located in the database referenced by the CurrentProject object

   <source lang="vb">

Sub IterateAllModules()

   Dim vnt As Variant
   With CurrentProject
       For Each vnt In .AllModules
           Debug.Print vnt.Name
       Next vnt
   End With

End Sub

</source>
   
  


Loop through all forms

   <source lang="vb">

Sub TestAllForms()

   Dim objAccObj As AccessObject
   Dim objTest As Object
   
   Set objTest = Application.CurrentProject
   For Each objAccObj In objTest.AllForms
       Debug.Print objAccObj.Name
   Next objAccObj

End Sub

</source>
   
  


Run a command through current project

   <source lang="vb">

Sub runcmdobj()

 Dim cmd As ADODB.rumand
 Dim strSQL As String
 Set cmd = New ADODB.rumand
 strSQL = "SELECT * FROM Employees"
 "Resuse the current Access connection
 Set cmd.ActiveConnection = CurrentProject.Connection
 cmd.rumandText = strSQL
 cmd.Execute
 Set cmd = Nothing

End Sub

</source>
   
  


The AllMacros collection allows you to iterate through all macros stored in the current project.

   <source lang="vb">

Sub IterateAllMacros()

   Dim vnt As Variant
   With CurrentProject
       For Each vnt In .AllMacros
           Debug.Print vnt.Name
       Next vnt
   End With

End Sub

</source>
   
  


The AllReports collection allows you to loop through all reports in the current project.

   <source lang="vb">

Sub IterateAllReports()

   Dim vnt As Variant
   With CurrentProject
       For Each vnt In .AllReports
           Debug.Print vnt.Name
       Next vnt
   End With

End Sub

</source>
   
  


Use the CompactRepair method of the Application object to compact and repair the database

   <source lang="vb">

Sub CompactRepairDB()

   Dim strFilePath As String
   strFilePath = CurrentProject.Path
   Application.rupactRepair strFilePath & "\B.accdb", _
       strFilePath & "\S.accdb", True

End Sub

</source>
   
  


Use With statement with CurrentProject

   <source lang="vb">

Sub CurrentProjectObject()

   With CurrentProject
       Debug.Print .Name
       Debug.Print .Path
   End With

End Sub

</source>