Beruflich Dokumente
Kultur Dokumente
Table of Contents
Introduction .................................................................................................................................. 2 The Basics ...................................................................................................................................... 3 Application Object ..................................................................................................................... 3 Objects Enumeration..................................................................................................................... 3 Tables, Views, Forms, Reports, Modules and References ........................................................ 3 Enumerate all tables.............................................................................................................. 4 Enumerate all Views .............................................................................................................. 4 Enumerate all Forms ............................................................................................................. 5 Enumerate all Reports ........................................................................................................... 5 Enumerate All Modules ......................................................................................................... 5 Enumerate all References ..................................................................................................... 6 Add Reference from File........................................................................................................ 6 Delete Reference ................................................................................................................... 6 Objects in Depth ............................................................................................................................ 7 Tables ........................................................................................................................................ 7 Create, Edit, Set relationships and Delete Tables ................................................................. 7 Views ....................................................................................................................................... 11 Create, execute and Delete ................................................................................................. 11 Forms & Reports...................................................................................................................... 13 Create, Edit, Open and Delete............................................................................................. 13 Modules & Class Modules ....................................................................................................... 19 Enumerate all Modules ....................................................................................................... 20 Create Module..................................................................................................................... 20 Create Class ......................................................................................................................... 21 Edit Module/Class ............................................................................................................... 22 Delete module ..................................................................................................................... 23 Useful Functions: ................................................................................................................. 23 Document References ............................................................................................................. 27
1|P a g e
Introduction
This document is intended to be a quick guide to automation; you wont find detailed explanations of any kind, or any reference any object model. This is not a cookbook either; you wont find code to address any specific solution. What you will find here are the basics, the foundation to let you start right away working on any of your automation projects stored in the shelves due to lack of documentation. If you ever asked any of this questions: How to automate MS Access? Where can I start to search for access automation documentation? How to create a form, report, module, class with automation? This document will help you. One of reasons to make this document is to save you from the tedious, boring and frustrating waste of time to find just the bits needed to perform a given task. The second reason is to give you another approach to automate Access. If you have never worked with the Microsoft Visual Basic For Applications Extensibility 5.3 library there is no better place to start to work than Automation. I hope this document achieve those objectives and help you build better applications.
2|P a g e
The Basics
The Application object is the top-level object in the Microsoft Access object model. It provides properties and methods you can use to create and work with other Access objects. It also provides several built-in functions you can use to work with the objects in your database. In essence, the Application object serves as the gateway to all other Access objects.
Objects Enumeration
All these objects belong to collections. To properly enumerate them you need to set a reference to its collection.
To Enumerate Tables and Views, most of the times examples are with the DAO object, here Ill do it with ADOX (Microsoft ADO Ext x.x for DDL and Security) Before start, for all the examples well be using the following two Procedures and local variables. The cp Variable is your gateway to automation.
w w w .a cc es
ExitHere: Exit Sub Handler: MsgBox Err.Number & VBA.vbCrLf & Err.Description Resume ExitHere End Sub
Private Sub CloseDb() On Error Resume Next If Not (cp Is Nothing) Then cp.CloseCurrentDatabase cp.Application.Quit Set cp = Nothing End If End Sub
se
Private cp As New Access.Application Private Const mstrDbPath As String = "C:\Northwind.accdb Private mintTotalLines As Integer Private Const mstrCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=C:\Northwind.accdb;"
xt en
de d. co m
3|P a g e
Application Object
For Each tbl In catDB.Tables If tbl.Type <> "VIEW" And tbl.Type <> "SYSTEM TABLE" Then Debug.Print tbl.Name & vbTab & tbl.Type End If Next Set catDB = Nothing End Sub
w w w .a cc es
Set catDB = Nothing End Sub
For Each qry In catDB.Tables If qry.Type = "VIEW" Then Debug.Print qry.Name & vbTab & qry.Type End If Next
se
catDB.ActiveConnection = mstrCon
xt en
4|P a g e
Enumerate all Views Because Views are virtual Tables the procedure is exactly the same.
de d. co m
ExitHere: Call CloseDb Exit Sub ErrHandler: MsgBox Err.Number & VBA.vbCrLf & Err.Description Resume ExitHere End Sub
w w w .a cc es
Enumerate All Modules
Private Sub GetAllModules() On Error GoTo ErrHandler Dim Mdl As Object Call Connect
For Each mdl In cp.CurrentProject.AllModules Debug.Print "Module/Class Name " & mdl.Name Next
ExitHere: Call CloseDb Exit Sub ErrHandler: MsgBox Err.Number & VBA.vbCrLf & Err.Description Resume ExitHere End Sub
se
For Each rpt In cp.CurrentProject.AllReports Debug.Print rpt.Name Next Call CloseDb End Sub
xt en
5|P a g e
de d. co m
Private Sub GetAlRefereces() Dim ref As Reference Call Connect For Each ref In cp.References Debug.Print ref.Name Next Call CloseDb End Sub
w w w .a cc es
ExitHere: Set ref = Nothing Exit Sub
Delete Reference
Private Sub DeleteReference() On Error GoTo ErrHandler Dim ref As Reference Call Connect Set ref = cp.References("C:\Program Files\Common Files\System\ado\msadox.dll") References.Remove ref
ErrHandler MsgBox "Exception id " & Err.Number & VBA.vbCrLf & Err.Description, vbCritical Resume ExitHere End Sub
se
ErrHandler MsgBox "Exception id " & Err.Number & VBA.vbCrLf & Err.Description, vbCritical Resume ExitHere
xt en
Private Sub ReferenceFromFile() On Error GoTo ErrHandler Dim ref As Reference Call Connect cp.References.AddFromFile ("C:\Program Files\Common Files\System\ado\msadox.dll") 'Microsoft ADO.Ext 6.0 for DLL and Security
de d. co m
6|P a g e
Objects in Depth
Create, Edit, Set relationships and Delete Tables The following snippets show how to create a new table, set special properties to different columns, creates an index as primary, modify a column, delete a column and create a relationship between two tables. Create Table
Private Dim Dim Dim Sub cat tbl idx CreateAccessTable() As New ADOX.Catalog As ADOX.Table As ADOX.Index
cat.ActiveConnection = mstrCon Set tbl = New ADOX.Table Set idx = New ADOX.Index tbl.Name = "tblCustomers"
w w w .a cc es
Continue.
With .Item("PKCustomerID") Set .ParentCatalog = cat .Properties("Autoincrement") = True 'AutoNumber. .Properties("Seed") = CLng(10) 'Set a seed .Properties("Increment") = CLng(10) 'Increment by 10 .Properties("Description") = "Primary Key." End With
'Set this column as PrimaryKey With idx .Name = "PrimaryKey" .PrimaryKey = True .Columns.Append ("PKCustomerID") tbl.Indexes.Append idx End With
se
'Append the columns. With tbl.Columns .Append "PKCustomerID", adInteger 'Long Integer .Append "LastName", adVarWChar, 20 'Size 20) .Append "FirstName", adVarWChar, 20 'Size 20) .Append "RegistrationDate", adDate 'Date/Time .Append "MonthlyFee", adCurrency 'Currency .Append "Notes", adLongVarWChar 'Memo .Append "CustomerURL", adLongVarWChar 'Hyperlink .Append "Inactive", adBoolean 'True/False
xt en
de d. co m
7|P a g e
Tables
'Set a validation rule. With .Item("RegistrationDate") Set .ParentCatalog = cat .Properties("Jet OLEDB:Column Validation Rule") = _ "Is Null Or >Date()" .Properties("Jet OLEDB:Column Validation Text") = _ "Registration date cannot be future." End With
'Hyperlink field. With .Item("CustomerURL") Set .ParentCatalog = cat .Properties("Jet OLEDB:Hyperlink") = True 'Hyperlink. End With End With cat.Tables.Append tbl Debug.Print "Table " & tbl.Name; " created on remote db" Set tbl = Nothing Set cat = Nothing Set idx = Nothing End Sub
w w w .a cc es
Created by Estuardo Sierra info@accessextended.com URL: http://www.accessextended.com
se
xt en
de d. co m
8|P a g e
'Required field. With .Item("LastName") Set .ParentCatalog = cat .Properties("Nullable") = False 'Required. .Properties("Jet OLEDB:Allow Zero Length") = False End With
' Open a Catalog on the database in which to create the link. catDB.ActiveConnection = mstrCon
Set tblLink = New ADOX.Table With tblLink ' Name the new Table and set its ParentCatalog property to the ' open Catalog to allow access to the Properties collection. .Name = strLinkTblAs Set .ParentCatalog = catDB ' Set the properties to create the link. .Properties("Jet OLEDB:Create Link") = True .Properties("Jet OLEDB:Link Datasource") = strDBLinkTo .Properties("Jet OLEDB:Remote Table Name") = strLinkTbl End With ' Append the table to the Tables collection. catDB.Tables.Append tblLink Set catDB = Nothing End Sub
w w w .a cc es
Set cat = Nothing End Sub
Edit Table
Private Sub EditTableColum() 'WARNING: You cannot change the Defined Size of a field in this way. Dim cat As New ADOX.Catalog 'Rename Column With cat .ActiveConnection = mstrCon .Tables("tblCustomers").Columns("Inactive").Name = "IsActive" ' To delete a colum '.Tables("tblCustomers").Columns.Delete "IsActive" End With
se
xt en
de d. co m
9|P a g e
Private Sub CreateLinkedTable(strDBLinkFrom As String, _ strDBLinkTo As String, _ strLinkTbl As String, _ strLinkTblAs As String)
Set FK = Nothing Set tbl = Nothing Set cat = Nothing End Sub
Delete Table
w w w .a cc es
se
xt en
'Create as foreign key With FK .Type = adKeyForeign .Name = "tblAdoxContractortblAdoxBooking" .RelatedTable = "tblAdoxContractor" .Columns.Append "PKCustomerID" .Columns("FKCustomerID").RelatedColumn = "PKCustomerID" .DeleteRule = adRICascade 'Cascade Update. Possible Values: adRINone adRICascade adRISetNull adRISetDefault End With tbl.Keys.Append FK
de d. co m
10 | P a g e
Create Relationship
Create, execute and Delete Views as virtual tables share many properties with tables; the main difference is the use of SQL, which makes a lot easier to work with them. ADOX distinguish two types of Views. The No parameterized, known as View, and the parameterized known as Procedure Create View
Private Dim Dim Dim Sub CreateView() cat As New ADOX.Catalog cmd As New ADODB.Command strSql As String
cat.ActiveConnection = mstrCon
w w w .a cc es
Private Sub ExecuteView() Set cmd = Nothing Set cat = Nothing End Sub
Execute View
Dim cat As New ADOX.Catalog Dim cmd As ADODB.Command Dim intCount As Integer
se
xt en
11 | P a g e
'Build the SQL Text. strSql = "SELECT * FROM tblCustomers;" cmd.CommandText = strSql cat.Views.Append "qryListCustomers", cmd
de d. co m
Views
Sub CreateProcedure() cat As New ADOX.Catalog cmd As New ADODB.Command strSql As String
cat.ActiveConnection = mstrCon
'Build the parameterized procedure. strSql = "PARAMETERS CustID Long; SELECT * FROM tblCustomers WHERE PKCustomerID=CustId;" cmd.CommandText = strSql cat.Procedures.Append "qryCustomerInfo", cmd Set cmd = Nothing Set cat = Nothing End Sub
Execute Procedure
Private Dim Dim Dim
w w w .a cc es
Set cmd = Nothing Set cat = Nothing End Sub
cmd.Parameters("CustID") = 10 'Set parameter cmd.Execute intCount Debug.Print intCount & " record(s) Found"
Delete View/Procedure
Private Sub DeleteView() Dim cat As New ADOX.Catalog Dim cmd As ADODB.Command
cat.ActiveConnection = CurrentProject.Connection cat.Procedures.Delete "qryCustomerInfo" cat.Views.Delete "qryListCustomers" Set cat = Nothing End Sub
se
xt en
de d. co m
12 | P a g e
Create Procedure
Create a form/report This is kind of undocumented procedure. Almost every example show how to do something with an existing object, but no how to create one. The following code shows how to create a form and set its main properties. This form will have 3 controls; two labels and one image, plus two events (Load Event and Timer Event).
Private Sub CreateForm() On Error GoTo Handler Dim strOldName As String Dim mdl As Access.Module Dim f As Access.Form Call Connect
w w w .a cc es
Note that the variable strOldName is to store the automatic name given to the New form by access (usually something like form1, form2 etc.), just after the form is actually saved with this name is possible to change it.
se
With cp This the core procedure .DoCmd.RunCommand acCmdNewObjectBlankForm strOldName = .Forms(.Forms.Count - 1).Name .DoCmd.Save Access.AcObjectType.acForm, strOldName .DoCmd.Close Access.AcObjectType.acForm, strOldName, Access.AcCloseSave.acSaveNo .DoCmd.Rename "frmMyForm", Access.AcObjectType.acForm, strOldName End Sub
xt en
de d. co m
13 | P a g e
Open the form hidden in design view and get its module
cp.DoCmd.OpenForm "frmMyForm", Access.AcFormView.acDesign, WindowMode:=Access.AcWindowMode.acHidden Set f = cp.Forms("frmMyForm") Call ToolTipFormProperties(f)Set forms Properties
Set mdl = f.Module mdl.InsertText AddCodeBehind()Set forms code behind .DoCmd.Save Access.AcObjectType.acForm, "frmMyForm" End With ExitHere: Call CloseDb Exit Sub Handler: MsgBox Err.Number & VBA.vbCrLf & Err.Description Resume ExitHere
w w w .a cc es
Created by Estuardo Sierra info@accessextended.com URL: http://www.accessextended.com
se
xt en
de d. co m
14 | P a g e
w w w .a cc es
Continue.
frm.Visible = False With frm .DefaultView = 0 'Single Form .ViewsAllowed = 1 ' Form .ScrollBars = 0 ' neither .RecordSelectors = False .NavigationButtons = False .DividingLines = False .AutoResize = True .AutoCenter = False .BorderStyle = 0 ' None .ControlBox = False .MinMaxButtons = 0 ' None .CloseButton = False .Cycle = 1 ' Current record .GridX = 5 .GridY = 5 .PopUp = True .Picture = "...\Resources\ToolTip\Themes\green.jpg" .PictureType = 1 ' 0=Embedded 1=Linked .PictureSizeMode = 1 '0=Clip '1=Stretch 3=Zoom .InsideWidth = 3255 .InsideHeight = 2490 .Width = 3260 .Section(0).Height = 2506 'Detail Section .TimerInterval = 3000 .OnLoad = "[Event Procedure]" .OnTimer = "[Event Procedure]" On Error Resume Next .Form.Section(1).Visible = False 'Header Section .Form.Section(2).Visible = False 'Footer Section End With
se
xt en
de d. co m
15 | P a g e
Sub ToolTipFormProperties(frm As Access.Form) lbl As Access.Label img As Access.Image lbl2 As Access.Label strStartUpPath As String
With lbl .Name = "lblTipTitle" .Caption = "Title Here" .BackStyle = 0 End With
img = cp.CreateControl(frm.Name, Access.AcControlType.acImage, Access.AcSection.acDetail, Left:=60, Top:=75, Width:=810, Height:=735) img.SizeMode = 1 '1=Stretch img.Name = "imgToolTip" img.PictureType = 1 ' 1=Linked
The code shows how to create a control set some properties and place it on the form. The last part shows how to add the code-behind to the forms module.
w w w .a cc es
Created by Estuardo Sierra info@accessextended.com URL: http://www.accessextended.com
se
xt en
lbl2 = cp.CreateControl(frm.Name, Access.AcControlType.acLabel, Access.AcSection.acDetail, Left:=88, Top:=881, Width:=3047, Height:=1507) With lbl2 .Name = "lblTipMessage" .Caption = "Message Here" .BackStyle = 0 End With cp.DoCmd.Save acForm, "frmMyForm"
de d. co m
16 | P a g e
This function returns the formatted text for the code behind
w w w .a cc es
Forms and reports share the same model. Before create a form, make sure that any public object such as function, sub, class, enum or type to be called from code behind must exist before saving the form, otherwise the automation process will break. The code behind the Load event sets several properties to the controls. Those values come from a class. That class should exist before the code behind automation.
se
sb = sb & "Private Sub Form_Load()" sb = sb & "" & VBA.vbCrLf sb = sb & " With MytoolTip" & VBA.vbCrLf sb = sb & " Me.lblTipTitle.Caption = .Title" & VBA.vbCrLf sb = sb & " Me.lblTipMessage.Caption = .Msg" & VBA.vbCrLf sb = sb & " Me.imgToolTip.Picture = .ImagePath" & VBA.vbCrLf sb = sb & " If VBA.Len(.SetTheme & vba.vbCrLf < 2 Then .ThemeName = Green" & VBA.vbCrLf sb = sb & " Me.Picture = .SetTheme" & VBA.vbCrLf sb = sb & " Me.Move .X, .Y" & VBA.vbCrLf sb = sb & " Me.lblTipMessage.FontSize = .SetFontSize" & VBA.vbCrLf sb = sb & " End With" & VBA.vbCrLf sb = sb & "" & VBA.vbCrLf sb = sb & "End Sub" & VBA.vbCrLf sb = sb & "" & VBA.vbCrLf sb = sb & "" & VBA.vbCrLf sb = sb & "Private Sub Form_Timer()" & VBA.vbCrLf sb = sb & " MytoolTip.Clear" & VBA.vbCrLf sb = sb & " If pfrmMyForm Is Nothing Then" & VBA.vbCrLf sb = sb & " Access.DoCmd.Close acForm, Me.Name" & VBA.vbCrLf sb = sb & " Else" & VBA.vbCrLf sb = sb & " Set pfrmMyForm = Nothing" & VBA.vbCrLf sb = sb & " End If" & VBA.vbCrLf sb = sb & "End Sub" & VBA.vbCrLf sb = sb & "" & VBA.vbCrLf
xt en
de d. co m
17 | P a g e
Delete
On Error Resume Next 'Just to make sure the form is closed before deletion cp.DoCmd.Close Access.AcObjectType.acForm, "frmMyForm" cp.DoCmd.DeleteObject Access.AcObjectType.acForm, "frmMyForm"
w w w .a cc es
Created by Estuardo Sierra info@accessextended.com URL: http://www.accessextended.com
se
xt en
18 | P a g e
de d. co m
Lines Name
w w w .a cc es
Parent ProcBodyLine ProcCountLines ProcOfLine ProcStartLine Type
Source: http://msdn.microsoft.com/en-us/library/office/ff823029.aspx
se
CountOfLines
Description The CountOfDeclarationLines property returns a Long value indicating the number of lines of code in the Declarations section in a standard module or class module. Read-only Long. The CountOfLines property returns a Long value indicating the number of lines of code in a standard module or class module. Read-only Long. The Lines property returns a string containing the contents of a specified line or lines in a standard module or a class module. Read-only String. You can use the Name property to specify or determine the string expression that identifies the name of an object. Read/write String. Returns the parent object for the specified object. Read-only. The ProcBodyLine property returns the number of the line at which the body of a specified procedure begins in a standard module or a class module. Read-only Long. The ProcCountLines property returns the number of lines in a specified procedure in a standard module or a class module. Read-only Long. The ProcOfLine property returns the name of the procedure that contains a specified line in a standard module or a class module. Readonly string. The ProcStartLine property returns avalue identifying the line at which a specified procedure begins in a standard module or a class module. Read-only Long. Indicates whether a module is a standard module or a class module. Read-only AcModuleType.
xt en
de d. co m
19 | P a g e
Private Sub GetAllModules() On Error GoTo Handler Dim CodeDoc As VBComponent Call Connect
For Each CodeDoc In cp.VBE.ActiveVBProject.VBComponents Debug.Print "Object " & CodeDoc.Name Next VBComp ExitHere: Call CloseDb Exit Sub Handler: MsgBox Err.Number & VBA.vbCrLf & Err.Description Resume ExitHere End Sub
Create Module
Private Sub CreateModule() On Error GoTo Handler Dim mdl As New VBComponent
w w w .a cc es
Call Connect Set mdl = cp.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule) ' Or the wizhook way 'Set mdl =cp.WizHook.DbcVbProject.VBComponents.Add(vbext_ct_StdModule) With mdl .Name = "modAutomated" cp.DoCmd.Save acModule, mdl.Name End With
ExitHere: Call CloseDb Exit Sub Handler: MsgBox Err.Number & VBA.vbCrLf & Err.Description Resume ExitHere End Sub
se
xt en
de d. co m
20 | P a g e
Enumerate all Modules To really enumerate all modules, the AllModules is not enough, like the command says it will get all modules not the code behind modules such as those from forms and reports. A solution would be to read from each one of those collections but that would be too lengthy
Create Class
Call Connect Set mdl = cp.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_ClassModule) ' Or the wizhook way 'Set mdl =cp.WizHook.DbcVbProject.VBComponents.Add(vbext_ct_ClassModule) With mdl .Name = "clsAutomated" cp.DoCmd.Save acModule, mdl.Name Debug.Print .VBE.SelectedVBComponent.Saved End With ExitHere: Call CloseDb Exit Sub Handler: MsgBox Err.Number & VBA.vbCrLf & Err.Description Resume ExitHere End Sub
Notice that the only difference between create a module and a class module are the constants: vbext_ct_StdModule and vbext_ct_ClassModule. If you try the straight forward way with the wizhook, those constants wont appear unless you have referenced the Microsoft Visual Basic For Applications Extensibility 5.3 library
w w w .a cc es
Created by Estuardo Sierra info@accessextended.com URL: http://www.accessextended.com
se
xt en
de d. co m
21 | P a g e
Private Sub CreateClassModule() On Error GoTo Handler Dim mdl As New VBComponent
Edit Module/Class
'Code from MSDN http://msdn.microsoft.com/enus/library/office/ff195471.aspx DoCmd.OpenModule strModuleName ' Return reference to Module object. Set mdl = Modules(strModuleName)
w w w .a cc es
Exit_FindAndReplace: Exit Function Error_FindAndReplace:
' Search for string. If mdl.Find(strSearchText, lngSLine, lngSCol, lngELine, lngECol) Then ' Store text of line containing string. strLine = mdl.Lines(lngSLine, Abs(lngELine - lngSLine) + 1) ' Determine length of line. intChr = Len(strLine) ' Determine number of characters preceding search text. intBefore = lngSCol - 1 ' Determine number of characters following search text. intAfter = intChr - CInt(lngECol - 1) ' Store characters to left of search text. strLeft = Left$(strLine, intBefore) ' Store characters to right of search text. strRight = Right$(strLine, intAfter) ' Construct string with replacement text. strNewLine = strLeft & strNewText & strRight ' Replace original line. mdl.ReplaceLine lngSLine, strNewLine FindAndReplace = True Else MsgBox "Text not found." FindAndReplace = False End If
MsgBox Err & ": " & Err.Description FindAndReplace = False Resume Exit_FindAndReplace End Function
se
xt en
de d. co m
22 | P a g e
Private Function EditModule(strModuleName As String, _ strSearchText As String, _ strNewText As String) As Boolean Dim mdl As Module Dim lngSLine As Long Dim lngSCol As Long Dim lngELine As Long Dim lngECol As Long Dim strLine As String Dim strNewLine As String Dim intChr As Integer Dim intBefore As Integer Dim intAfter As Integer Dim strLeft As String Dim strRight As String
Delete module
Useful Functions: Before start with this section you may want to run all your automation tasks in the background do not forget to start your connection without this line (see page 3): .VBE.MainWindow.Visible = False
Count module lines a better approach. Skips blank lines and comments
Public Function TotalCodeLines(CodeDoc As VBIDE.VBComponent) As Long Dim i As Long Dim strLeft As String Dim intCodeLineCount As Integer
Then
w w w .a cc es
With CodeDoc.CodeModule For i = 1 To .CountOfLines strLeft = .Lines(i, 1) If Not VBA.Trim(strLeft) = vbNullString Or Not VBA.Left(VBA.Trim(strLeft), 1) = "'" Then ' To skip blank lines and comments intCodeLineCount intCodeLineCount + 1 End If Next i End With TotalCodeLines = LineCount End Function
se
xt en
If CodeDoc.Collection.Parent.Protection = vbext_pp_locked
de d. co m
23 | P a g e
Call Connect Set mdl = cp.VBE.ActiveVBProject.VBComponents("PurchaseOrders") Set CodeMod = mdl.CodeModule With CodeMod intCurrentLine = .CountOfDeclarationLines + 1 Do Until intCurrentLine >= .CountOfLines strProcName = .ProcOfLine(intCurrentLine, ProcKind) Debug.Print strProcName intCurrentLine = .ProcStartLine(strProcName, ProcKind) + .ProcCountLines(strProcName, ProcKind) + 1 Loop End With ExitHere: Call CloseDb Exit Sub Handler: MsgBox Err.Number & VBA.vbCrLf & Err.Description Resume ExitHere End Sub
w w w .a cc es
se
xt en
de d. co m
24 | P a g e
Private Sub GetProcedures(mdl As VBIDE.VBComponent) On Error GoTo Handler Dim CodeMod As VBIDE.CodeModule Dim intCurrentLine As Long Dim strProcName As String Dim ProcKind As VBIDE.vbext_ProcKind
Call Connect If cp.VBE.ActiveVBProject.Protection = vbext_pp_locked Then TotalProjectLines = -1 Exit Function End If For Each mdl In cp.VBE.ActiveVBProject.VBComponents intLneCount = intLneCount + mdl.CodeModule.CountOfLines Next mdl TotalProjectLines = intLneCount
Delete Procedure
w w w .a cc es
Private Sub DeleteProcedure(strProcName As String, mdl As VBIDE.VBComponent) Dim CodeMod As VBIDE.CodeModule Dim intBLine As Integer Dim intELine As Integer Set CodeMod = mdl.CodeModule
With CodeMod intBLine = .ProcStartLine(strProcName, vbext_pk_Proc) intELine = .ProcCountLines(strProcName, vbext_pk_Proc) .DeleteLines StartLine:=intBLine, Count:=intELine End With Call CloseDb End Sub
se
xt en
ExitHere: Call CloseDb Exit Function Handler: MsgBox Err.Number & VBA.vbCrLf & Err.Description Resume ExitHere End Function
de d. co m
25 | P a g e
Private Function TotalProjectLines() As Integer On Error GoTo Handler Dim mdl As VBIDE.VBComponent Dim intLneCount As Integer
For Each mdl In cp.VBE.ActiveVBProject.VBComponents If mdl.Type = vbext_ct_Document Or mdl.Type = vbext_ct_ClassModule Or mdl.Type = vbext_ct_ClassModule Then Set CodeMod = mdl.CodeModule With CodeMod .DeleteLines 1, .CountOfLines End With Else cp.VBE.ActiveVBProject.VBComponents.Remove mdl End If Next End Sub
Function DeleteWholeLine(strModuleName, strText As String) _ As Boolean Dim mdl As Module, lngNumLines As Long Dim lngSLine As Long, lngSCol As Long Dim lngELine As Long, lngECol As Long Dim strTemp As String
w w w .a cc es
Else Exit_DeleteWholeLine: Exit Function
Then
lngNumLines = Abs(lngELine - lngSLine) + 1 strTemp = LTrim$(mdl.Lines(lngSLine, lngNumLines)) strTemp = RTrim$(strTemp) If strTemp = strText Then mdl.DeleteLines lngSLine, lngNumLines Else MsgBox "Line contains text in addition to '" _ & strText & "'." End If
MsgBox "Text '" & strText & "' not found." End If DeleteWholeLine = True
Error_DeleteWholeLine: MsgBox Err & " :" & Err.Description DeleteWholeLine = False Resume Exit_DeleteWholeLine End Function
se
xt en
de d. co m
26 | P a g e
w w w .a cc es
Created by Estuardo Sierra info@accessextended.com URL: http://www.accessextended.com
se
xt en
27 | P a g e
Subject Application Object Module Object Working with Microsoft Access Objects Add, Remove, Check References Using Microsoft Access as an Automation Server Use ADOX to Manipulate AutoNumber Fields AcModuleType Enumeration How To Automate Microsoft Access From Visual Basic .NET
de d. co m
Document References