access object model. application object the application object contains all microsoft access objects...

18
Access Object Model

Post on 20-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

Access Object Model

Page 2: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object
Page 3: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

Application Object

• The Application object contains all Microsoft Access Objects and collections. You can use the Application object to apply methods or property settings to the entire Microsoft Access application. – For example, you can use the SetOption method of

the Application object to set database options from Visual Basic. The following example shows how you can set the Status Bar check box under Show on the View tab of the Options dialog box.

– Application.SetOption "Show Status Bar", True

Page 4: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

Some Useful Application Object’s Properties and Methods

• Properties:– CurrentData– CurrentProject– DoCmd– Screen

• Methods:– CurrentDB– OpenCurrentDatabase– Run– RunCommand– Quit

Page 5: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

CurrentData/CurrentProject

• How many tables in the current database?

• How many forms?

Page 6: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

OpenCurrentDatabase Method

• You can use the OpenCurrentDatabase method to open an existing Access database as the current database.

Page 7: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

OpenCurrentDatabase Example

• Dim appAccess As Access.Application• Set appAccess = CreateObject("Access.Application")• appAccess.OpenCurrentDatabase ("c:\salesDB.mdb")• MsgBox (appAccess.CurrentData.AllTables.Count)• appAccess.DoCmd.OpenForm appAccess.CurrentProject.AllForms(2).FullName

• MsgBox ("Press a key to continue ...")

• Set appAccess = Nothing

Page 8: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

Screen

• MsgBox (Screen.ActiveForm.Name)

• MsgBox (Screen.ActiveControl)• DoCmd.Close acForm, Screen.ActiveForm.Name

Page 9: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

DoCmd

• You can use the methods of the DoCmd object to run Microsoft Access actions from Visual Basic. An action performs tasks such as closing windows, opening forms, and setting the value of controls.

Page 10: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

Macro and DoCmd Demo

• DoCmd.OpenForm "Employee", acNormal

• DoCmd.GoToRecord , , acNewRec

• DoCmd.SendObject

Page 11: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

SendObject Example

Sub GPALetter()On Error GoTo macroLetter_ErrDim msgBody As String If (Forms!student!GPA < 2 And MsgBox("Send letter: ", 1) = 1) Then msgBody = "Dear " & Forms!student!SName & ": " & vbCrLf msgBody = msgBody & "Your GPA is: " & CStr(Forms!student!GPA) DoCmd.SendObject , , , Forms!student!Email, , , "testEmail", msgBody, True 'DoCmd.SendObject acForm, "StudentLetter", "RichTextFormat(*.rtf)", "", "", "", "Low GPA", "", False, "" End If

Page 12: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

DoCmd TransferDatabase/Spreadsheet

• DoCmd.TransferDatabase acImport, "Microsoft Access", "c:\salesDB.mdb", acTable, "customer", "customer", False

• DoCmd.TransferSpreadsheet acExport, 8, "faculty", "c:\f.xls", False, ""

Page 13: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

Close All Open Forms

• Sub closeAllForms()• Dim myObj As Form• Dim i As Integer• MsgBox (Forms.Count)• Do While Forms.Count > 0• DoCmd.Close acForm, Forms(0).Name• Loop• End Sub

Page 14: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

Try For Each

Sub closeAllForms2()Dim myObj As FormMsgBox (Forms.Count)For Each myObj In Forms DoCmd.Close acForm, myObj.NameLoopEnd Sub

Page 15: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

Open All Forms

Sub OpenAllForms()Dim myObj As ObjectFor Each myObj In CurrentProject.AllForms DoCmd.OpenForm myObj.Name, acNormalNextEnd Sub

Page 16: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

Form Object Example

Dim intView As IntegerIf CurrentProject.AllForms("faculty").IsLoaded Then intView = CurrentProject.AllForms("faculty").CurrentView If intView = 0 Then MsgBox ("Design view") ElseIf intView = 1 Then MsgBox ("Form view") Else MsgBox ("Datasheet view") End IfElse MsgBox ("Not open")End If

Page 17: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

Dim ctrl As ControlFor Each ctrl In Screen.ActiveForm.Controls If ctrl.ControlType = acLabel Then ctrl.ForeColor = vbRed End If If ctrl.ControlType = acTextBox Then ctrl.BackColor = vbBlue End IfNext

Page 18: Access Object Model. Application Object The Application object contains all Microsoft Access Objects and collections. You can use the Application object

CurrentDB

• Dim db As Database

• Set db = CurrentDb

• db.Execute "update student set gpa=3.0 where sid='s30'"

• Dim rs As Recordset

• Set rs = db.OpenRecordset("select count(sid)as scount from student")

• MsgBox (rs("scount"))