Download presentation
Presentation is loading. Please wait.
Published byJeffry Carpenter Modified over 9 years ago
1
Recordsets Chapter 7
2
Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access & Web coders are bound to them Because VBA/VBScript is necessary to … –Open a recordset, –Locate a record –Update or add a record –Close, etc
3
Controls on Forms I: Recordsets The usual job of forms: showing data from tables/queries With recordsets, you must use VBA/VBScript to show data in controls You fill listboxes, textboxes, etc The form doesn't fill them for you This is not "bound forms"
4
Controls on Forms II: Recordsets Another job of forms: taking data from controls into tables/queries With recordsets, you must use VBA/VBScript to take form data from the controls and save it The listboxes, textboxes, etc are the source of tabled data But, the form doesn't update the table for you This is not "bound forms"
5
Controls on Forms: Summary Use standard forms for input and output Use recordset programming to… –Read data from tables/queries –Fill controls on forms with this data –Read data in controls on forms –Update the tables/queries with user changes to the form data But, do not use recordset programming instead of binding forms to tables/queries. Use it to augment forms.
6
Types of DAO Recordsets Table-type Dynaset-type Snapshot-type Forward-type
7
Table-type DAO Recordsets Based on one table or external ISAM Cannot use linked tables or external ODBC Records can be edited Fast record location: index and seek Recommended mainly for lookups Drawbacks: no queries or externals, whole records instead of pointers to records
8
Dynaset-type DAO Recordsets Based on multiple tables, queries or attached tables (dBase, Excel, Access, etc) Can be based on an SQL string Only pointers (bookmarks) not records Client-server enabled Underlying records can be edited Multiuser edits reflected
9
Snapshot-type DAO Recordsets Based on multiple tables, queries or attached tables (dBase, Excel, Access, etc) Can be based on an SQL string Entire records involved (max 500) Client-server enabled (qualified) Good for client analysis, reporting Underlying records cannot be edited
10
Forward-type DAO Recordsets Identical to Snapshots, except Can move only forward Rs.MoveNext Rs.MoveLast Best used for analysis and loading arrays
11
Table-type DAO Recordsets Seek to locate a single record, just one Fast, must be based on Index rs.Index = "Names" ' must be in table rs.Seek "=", strName If rs.NoMatch = True then ….
12
Dynaset-type DAO Recordsets (find numbers) Find to locate records, not just one record Not as fast as indexed seek, but can be sorted for speedier finds by SQL when built intID=4 strFind="pkPeopleID=" & intID rs.FindFirst strFind If rs.NoMatch = True then MsgBox "Failed to find " & intID Endif
13
Dynaset-type DAO Recordsets (find dates) Find to locate records, not just one record Not as fast as indexed seek, but can be sorted for speedier finds by SQL when built dteHire=#01/03/01# strFind="HireDate=" & "#" & dteHire & "#" rs.FindFirst strFind If rs.NoMatch = True then MsgBox "Failed to find " & dteHire Endif
14
Dynaset-type DAO Recordsets (find strings) Find to locate records, not just one record Not as fast as indexed seek, but can be sorted for speedier finds by SQL when built strName="Poynor" strFind="LastName=" & "'" & strName & "'" rs.FindFirst strFind If rs.NoMatch = True then MsgBox "Failed to find " & strName Endif
15
Beginnings and endings 1/3 ' our textbook opens recordsets like this Dim rs as DAO.Recordset ' any variable name is OK Dim db as Database ' any variable name is OK Set db = CurrentDb Set rs = db.OpenRecordset("tblEmployee",dbOpenDynaset) ' … records are processed as needed ' and then the table is closed safely rs.Close Set rs = Nothing
16
Beginnings and endings 2/3 ' most recordsets are opened something like this Dim rec as DAO.Recordset Set rec = _ Currentdb.OpenRecordset("tblPeople",dbOpenDynaset) ' ….processed as needed ….etc etc rec.Close Set rec = Nothing
17
Beginnings and endings 3/3 ' efficient recordsets are opened with SQL Dim recdset as DAO.Recordset Set recdset = Currentdb.OpenRecordset( _ "SELECT * From tblEmployee " & _ "WHERE HireDate < #01/01/90# " & _ "ORDER BY HireDate", _ dbOpenDynaset) ' ….processed as needed ….etc etc recdset.Close Set recdset = Nothing
18
Beginning of File (BOF) rs.MovePrevious If rs.BOF = True Then…. ' true if you tried to go before the first record Ending of File (EOF) rs.MoveNext If rs.EOF = True Then…. ' true if you tried to go after the last record
19
' here is how to store a recordset into an array Dim varArrayData as Variant rs.MoveFirst varArrayData = rs.GetRows(1000) 'up to 1000 rows rs.Close ' how many records and fields were stored? (Chap. 11) intRecordCount = UBound(varArrayData, 2) + 1 intFieldCount = UBound(varArrayData, 1) + 1 'stored as varArrayData(Fields, Records) (Chap. 11) Arrays created from Recordsets
20
rs.AddNew ‘ fields set to defaults ' maybe show defaults on form Me!txtField1 = rs!Field1 ' if using form for data input, save ' or maybe or maybe just fill in fields rs!Field1 = "Unknown" 'save changes (if any) to new record rs.Update Adding to Recordsets
21
rs.Delete ' that's all folks Deleting from Recordsets
22
Dim ws As Workspace Set ws = DBEngine.Workspaces(0) On Error GoTo NetworkError ws.BeginTrans 'open a transaction rs.Edit ' open edit buffer ' maybe use form inputs rs!Field1=Me!Field1 rs.Update ' write & close buffer ws.CommitTrans 'close transaction Exit Sub NetworkError: ws.Rollback ' undo the edits Safety in Editing/Updating Recordsets
23
Dim rs as DAO.Recordset Set rs = CurrentDb.OpenRecordset("tblStudent", dbOpenTable) rs.Index = "StudentID" rs.Seek "=", strID If rs.NoMatch Then MsgBox "Seek failed." Else rs.Edit ' open edit buffer rs!AnyField = txtNewData rs.Update ' write buffer to recordset End If rs.Close ' close to prevent problems Editing/Updating recordsets without transactions
24
Dim ws As Workspace Set ws = DBEngine.Workspaces(0) On Error GoTo NetworkError ws.BeginTrans 'open a transaction rs.AddNew ' fields set to defaults ' maybe show defaults on form Me!txtField1 = rs!Field1 ' example ws.CommitTrans 'close the transaction Exit Sub NetworkError: ws.Rollback ' undo the add Safety in Adding to Recordsets – transaction processing
25
Dim varBookmark As Variant varBookmark = rs.Bookmark ' remember position rs.MoveFirst ' start at first position Do While Not rs.EOF ' calculate something you want rs.MoveNext ' move to next position Loop rs.Bookmark = varBookmark ' restore position Keeping track of record position in recordsets
26
Dim rs As DAO.Recordset Set db = CurrentDb() ' or put CurrentDb in next line Set rs = db.OpenRecordset("tblPeople", dbOpenSnapshot) strName = InputBox("Enter a last name to locate:") rs.FindFirst "Last = " & "'" & strName & "'" If rs.NoMatch = False Then MsgBox "Located. " & rs!First & " " & rs!Last Else MsgBox "Name not found." End If rs.Close Example of Locating Records in Dynasets and Snapshots
27
Dim rs As DAO.Recordset Set rs = CurrentDb.OpenRecordset( _ "tblPeople", dbOpenTable) strName = InputBox("Enter a last name to locate:") rs.Index = "Names" ' must already be this index in table rs.Seek "=", strName If rs.NoMatch = False Then MsgBox "Located. " & rs!First & " " + rs!Last Else MsgBox "Name not found." End If rs.Close Example of Locating Records in Tables
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.