27
Recordsets Chapter 7

Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

Embed Size (px)

Citation preview

Page 1: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

Recordsets

Chapter 7

Page 2: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 3: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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"

Page 4: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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"

Page 5: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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.

Page 6: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

Types of DAO Recordsets

• Table-type

• Dynaset-type

• Snapshot-type

• Forward-type

Page 7: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 8: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 9: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 10: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

Forward-type DAO Recordsets

• Identical to Snapshots, except

• Can move only forward• Rs.MoveNext• Rs.MoveLast

• Best used for analysis and loading arrays

Page 11: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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 ….

Page 12: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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 builtintID=4

strFind="pkPeopleID=" & intID

rs.FindFirst strFindIf rs.NoMatch = True then

MsgBox "Failed to find " & intID

Endif

Page 13: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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 builtdteHire=#01/03/01#

strFind="HireDate=" & "#" & dteHire & "#"

rs.FindFirst strFindIf rs.NoMatch = True then

MsgBox "Failed to find " & dteHire

Endif

Page 14: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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 builtstrName="Poynor"strFind="LastName=" & "'" & strName & "'"rs.FindFirst strFindIf rs.NoMatch = True then MsgBox "Failed to find " & strName Endif

Page 15: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 16: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 17: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 18: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 19: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

' 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

Page 20: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 21: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

rs.Delete ' that's all folks

Deleting from Recordsets

Page 22: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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 SubNetworkError: ws.Rollback ' undo the edits

Safety in Editing/Updating Recordsets

Page 23: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 24: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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 SubNetworkError: ws.Rollback ' undo the add

Safety in Adding to Recordsets – transaction processing

Page 25: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 26: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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

Page 27: Recordsets Chapter 7. Recordset Objects Similar to Tables and Queries: data But forms cannot be bound to them Fields cannot be bound to them But Access

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