access object model. application object the application object contains all microsoft access objects...
Post on 20-Dec-2015
219 views
TRANSCRIPT
Access Object Model
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
Some Useful Application Object’s Properties and Methods
• Properties:– CurrentData– CurrentProject– DoCmd– Screen
• Methods:– CurrentDB– OpenCurrentDatabase– Run– RunCommand– Quit
CurrentData/CurrentProject
• How many tables in the current database?
• How many forms?
OpenCurrentDatabase Method
• You can use the OpenCurrentDatabase method to open an existing Access database as the current database.
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
Screen
• MsgBox (Screen.ActiveForm.Name)
• MsgBox (Screen.ActiveControl)• DoCmd.Close acForm, Screen.ActiveForm.Name
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.
Macro and DoCmd Demo
• DoCmd.OpenForm "Employee", acNormal
• DoCmd.GoToRecord , , acNewRec
• DoCmd.SendObject
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
DoCmd TransferDatabase/Spreadsheet
• DoCmd.TransferDatabase acImport, "Microsoft Access", "c:\salesDB.mdb", acTable, "customer", "customer", False
• DoCmd.TransferSpreadsheet acExport, 8, "faculty", "c:\f.xls", False, ""
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
Try For Each
Sub closeAllForms2()Dim myObj As FormMsgBox (Forms.Count)For Each myObj In Forms DoCmd.Close acForm, myObj.NameLoopEnd Sub
Open All Forms
Sub OpenAllForms()Dim myObj As ObjectFor Each myObj In CurrentProject.AllForms DoCmd.OpenForm myObj.Name, acNormalNextEnd Sub
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
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
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"))