230 likes | 318 Views
Introduction to Procedures. Modules. A module is a file that holds code or pieces of code in a Visual Basic application. you can open the object in Design View and click the View Code button. Creating a Module. In Microsoft Access, on the Ribbon, click Create. In the Other section,
E N D
Introduction to Procedures • Modules • A module is a file that holds code or pieces of code in a Visual Basic application • you can open the object in Design View and click the View Code button harshana-fernando.blogspot.com
Creating a Module • In Microsoft Access, on the Ribbon, click Create. In the Other section, • click the arrow under the Macro button and click Module In Microsoft Visual Basic On the main menu, click Insert -> Module On the Standard toolbar, click the arrow of the Insert Module button and click Module harshana-fernando.blogspot.com
Sub Procedures A procedure is an assignment you ask Microsoft Visual Basic to perform besides, or to complete, the normal flow of the program. A procedure is created to work in conjunction with the controls' events of a database. Structurally, a procedure appears similar to an event. Sub ProcedureName() End Sub Sub CreateName() Dim strFullName As String strFullName = "Jacques Fame Ndongo" End Sub harshana-fernando.blogspot.com
Inserting a Procedure On the main menu, click Insert -> Procedure On the Standard toolbar, click the arrow of the Insert button and click Procedure harshana-fernando.blogspot.com
Calling a Procedure Private Sub Detail_Click() ChangeColor End Sub Private Sub cmdSqCalculate_Click() SquareSolution End Sub Private Sub cmdRCalculate_Click() SolveRectangle End Sub harshana-fernando.blogspot.com
Functions • A function is a procedure that takes care of an assignment and returns a result. Function GetFullName() End Function Function GetFullName() As String End Function Function GetFullName() As String Dim strFirstName, strLastName As String strFirstName = txtFirstName strLastName = txtLastName GetFullName = strFirstName & " " & strLastName End Function harshana-fernando.blogspot.com
Calling a Function Function GetFullName() As String Dim strFirstName, strLastName As String strFirstName = txtFirstName strLastName = txtLastName GetFullName = strFirstName & " " & strLastName End Function Private Sub Detail_DblClick(Cancel As Integer) txtFullName = GetFullName End Sub harshana-fernando.blogspot.com
Introduction to Built-In Objects Microsoft Access heavily relies on built-in objects and already created collections. These collections are made in entities we call classes. Besides the collections and objects, there are also many enumerations. • To display the Object Browser: • On the main menu, click View -> Object Browser • On the Standard toolbar, click the Object Browser button harshana-fernando.blogspot.com
The Current Project Private Sub Detail_Click() Dim CurrentDatabase As Object Set CurrentDatabase = Application.CurrentProject txtFilename = CurrentDatabase.FullName End Sub harshana-fernando.blogspot.com
The DoCmd Object harshana-fernando.blogspot.com
Microsoft Access Object Library and VBA on the main menu of Microsoft Visual Basic, you can click Tools -> References... harshana-fernando.blogspot.com
When you start a database in Microsoft Access, you are said to have started a session. If one database is not enough for what you are currently doing, you can open another existing database or you can create a new one. harshana-fernando.blogspot.com
Microsoft Office 2007 ships with a library that can be used to perform all types of operations on a database and used throughout all applications of the Microsoft Office family. This library is called Microsoft Office Access Database Engine Object. Like every library, it has a version. In Microsoft Office 2007, it is the Microsoft Office 12.0 Access Database Engine Object Library. harshana-fernando.blogspot.com
Using DAO If you decide to use the DAO library, you must remove the Microsoft Office 12.0 Access Database Engine Object Library because both libraries cannot coexist in the same database harshana-fernando.blogspot.com
Microsoft ActiveX Data Objects- ADO harshana-fernando.blogspot.com
Connection to a Database Private Sub cmdConnector_Click() Dim conConnector As ADODB.Connection Set conConnector = New ADODB.Connection Set conConnector = Nothing End Sub After using the Connection variable, to release the resources it using, assign Nothing to it. harshana-fernando.blogspot.com
Opening a Connection Connection.Open ConnectionString, UserID, Password, Options The most common provider we will use is the Microsoft JET database engine. It is represented as Microsoft.JET.OLEDB.4.0 (case insensitive). You can use it to specify the provider as follows Private Sub cmdConnector_Click() Dim conConnector As ADODB.Connection Set conConnector = New ADODB.Connection conConnector.Open "Provider=Microsoft.Jet.OLEDB.4.0" Set conConnector = Nothing End Sub harshana-fernando.blogspot.com
Private Sub cmdConnector_Click() Dim conConnector As ADODB.Connection Set conConnector = New ADODB.Connection conConnector.Open "Provider='Microsoft.Jet.OLEDB.4.0';" & _ "Data Source=C:\Exercises\Example.accdb" Set conConnector = Nothing End Sub harshana-fernando.blogspot.com
Connection to the Current Database Private Sub cmdCurrentConnection_Click() Dim conCurrent As ADODB.Connection Set conCurrent = Application.CurrentProject.Connection Set conCurrent = Nothing End Sub When this code executes, it identifies the connection to the current database and stores it in a declared variable named conCurrent harshana-fernando.blogspot.com
Closing a Connection Private Sub cmdDataSource_Click() Dim conConnector As ADODB.Connection Set conConnector = New ADODB.Connection conConnector.Open "DSN=Exercise;UID=;PWD=;" conConnector.Close Set conConnector = Nothing End Sub When using a connection, it consumes resources that other applications may need. Therefore, after using it, you should close it and free the resources it was using so they can be made available to the other parts of the computer harshana-fernando.blogspot.com
Table Creation With the Microsoft Access Object Library Private Sub cmdCreateTable_Click() Dim curDatabase As Object Dim tblStudents As Object ‘ Get a reference to the current database Set curDatabase = CurrentDb ' Create a new table named Students Set tblStudents = curDatabase.CreateTableDef("Students") . . . ' Add the Students table to the current database curDatabase.TableDefs.Append tblStudents End Sub After creating the table, you must add it to the current database. To support this, the CurrentDb object is equipped with the TableDefs property. TableDefs is in fact a collection. The TableDefs collection is equipped with the Append() method that is used to add a new table to the current database. harshana-fernando.blogspot.com
Table Creation With DAO Private Sub cmdCreateTable_Click() Dim dbDeja As DAO.Database Dim tblEmployees As DAO.TableDef ' Open the database Set dbDeja = DBEngine.OpenDatabase("C:\My Documents\Exercise.accdb") ' Create a new TableDef object. Set tblEmployees = dbDeja.CreateTableDef("Employees") . . . ' Add the new table to the database. dbDeja.TableDefs.Append tblEmployees dbDeja.Close End Sub harshana-fernando.blogspot.com