Presentation is loading. Please wait.

Presentation is loading. Please wait.

Private Sub Close_Click() On Error GoTo Err_Close_Click DoCmd.Close Exit_Close_Click: Exit Sub Err_Close_Click: MsgBox Err.Description Resume Exit_Close_Click.

Similar presentations


Presentation on theme: "Private Sub Close_Click() On Error GoTo Err_Close_Click DoCmd.Close Exit_Close_Click: Exit Sub Err_Close_Click: MsgBox Err.Description Resume Exit_Close_Click."— Presentation transcript:

1 Private Sub Close_Click() On Error GoTo Err_Close_Click DoCmd.Close Exit_Close_Click: Exit Sub Err_Close_Click: MsgBox Err.Description Resume Exit_Close_Click End Sub

2 Private Sub Add_Click() On Error GoTo Err_Add_Click DoCmd.GoToRecord,, acNewRec Exit_Add_Click: Exit Sub Err_Add_Click: MsgBox Err.Description Resume Exit_Add_Click End Sub

3 Private Sub Save_Click() On Error GoTo Err_Save_Click DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord,, acMenuVer70 Exit_Save_Click: Exit Sub Err_Save_Click: MsgBox Err.Description Resume Exit_Save_Click End Sub

4 Recordsets Recordset object –The set of records in a table or returned by a query or SQL statement Record Source property –Assigns specific Recordset object to form Current Record –The record pointed to by the record selector (form may display one or more records depending on view) RecordsetClone –A copy of the recordset object

5 Bookmarks Track records in a recordset Are created for each record in a form’s recordset when we open the form Value of form’s bookmark property is the value of the bookmark of the current record Two operations –Store – assign value of bookmark to string variable -Set Value -assign previously stored value to form’s bookmark property

6 Private Sub Combo28_AfterUpdate() ' Find the record that matches the control. Dim rs As Object Set rs = Me.Recordset.Clone rs.FindFirst "[MemberID] = " & str(Me![Combo28]) Me.Bookmark = rs.Bookmark End Sub Subroutine name Comment Variable declaration Assign value to variable Call Recordset FindFirst method Set form’s Bookmark property to RecodrsetClone Bookmark property Code for Find Members Combo Box

7 Code in action 1Jenny 2Deborah 3Janet 4Ian 1Jenny 2Deborah 3Janet 4Ian Ian Find Member Create clone FindFirst Set bookmark Actions Form RecordsetRecordsetClone

8 Procedures Units of VBA code to perform an operation or calculate a value Two types –Sub No return value –Function Returns value (e.g. result of calculation)

9 Modules Procedures are stored in Modules Two types of Module –Class Module Associated with form or report –Standard Module General purpose Run from anywhere within database Stored in Module object

10 Message Box Code Private Sub Form_Open(Cancel As Integer) MsgBox "Hello, welcome to Access", vbOKOnly, "Welcome Message" End Sub

11 Public Function Confirm(strMessage As String) ' Display an important message to the user MsgBox strMessage, vbOKCancel End Function Confirm Function

12 Public Function NewRec(strFormName As String) ' This function is used in the Click event of command buttons to open forms and create a new record ' Using a function is often more efficient than repeating the same code in multiple event procedures ' Open specified form DoCmd.OpenForm strFormName,,,, acFormAdd DoCmd.Maximize End Function

13 Private Sub cmdCountMembers_Click() Dim dbs As Database, rst As Recordset 'Return reference to current database Set dbs = CurrentDb ' Open table-type Recordset object Set rst = dbs.OpenRecordset("tblMembers") MsgBox "No of Records = " & rst.RecordCount Set dbs = Nothing End Sub

14 Public Function Confirm2(strMessage As String) ' Display an important message to the user Dim choice As Integer choice = MsgBox(strMessage, vbOKCancel) If choice = vbOK Then DoCmd.Quit End Function


Download ppt "Private Sub Close_Click() On Error GoTo Err_Close_Click DoCmd.Close Exit_Close_Click: Exit Sub Err_Close_Click: MsgBox Err.Description Resume Exit_Close_Click."

Similar presentations


Ads by Google