Download presentation
Presentation is loading. Please wait.
1
Access Object Model
3
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
4
Some Useful Application Object’s Properties and Methods Properties: –CurrentData –CurrentProject –DoCmd –Screen Methods: –CurrentDB –OpenCurrentDatabase –Run –RunCommand –Quit
5
CurrentData/CurrentProject How many tables in the current database? How many forms?
6
OpenCurrentDatabase Method You can use the OpenCurrentDatabase method to open an existing Access database as the current database.
7
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
8
Screen MsgBox (Screen.ActiveForm.Name) MsgBox (Screen.ActiveControl) DoCmd.Close acForm, Screen.ActiveForm.Name
9
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.
10
Macro and DoCmd Demo DoCmd.OpenForm "Employee", acNormal DoCmd.GoToRecord,, acNewRec DoCmd.SendObject
11
SendObject Example Sub GPALetter() On Error GoTo macroLetter_Err Dim 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
12
DoCmd TransferDatabase/Spreadsheet DoCmd.TransferDatabase acImport, "Microsoft Access", "c:\salesDB.mdb", acTable, "customer", "customer", False DoCmd.TransferSpreadsheet acExport, 8, "faculty", "c:\f.xls", False, ""
13
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
14
Try For Each Sub closeAllForms2() Dim myObj As Form MsgBox (Forms.Count) For Each myObj In Forms DoCmd.Close acForm, myObj.Name Loop End Sub
15
Open All Forms Sub OpenAllForms() Dim myObj As Object For Each myObj In CurrentProject.AllForms DoCmd.OpenForm myObj.Name, acNormal Next End Sub
16
Form Object Example Dim intView As Integer If 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 If Else MsgBox ("Not open") End If
17
Dim ctrl As Control For 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 If Next
18
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"))
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.