PercentPosition Property
Applies To
Dynamic-Type Recordset object, Recordset object, Snapshot-Type Recordset object, Table-Type Recordset object.
Description
Sets or returns a value indicating the approximate location of the current record in the Recordset object based on a percentage of the records in the Recordset.
Settings and Return Values
The setting or return value is a Single that is a number between 0.0 and 100.00.
Remarks
To indicate or change the approximate position of the current record in a Recordset object, you can check or set the PercentPosition property. When working with a dynaset- or snapshot-type Recordset object opened directly from a base table, first populate the Recordset object by moving to the last record before you set or check the PercentPosition property. If you use the PercentPosition property before fully populating the Recordset object, the amount of movement is relative to the number of records accessed as indicated by the RecordCount property setting. You can move to the last record by using the MoveLast method.
Note Using the PercentPosition property to move the current record to a specific record in a Recordset object isn't recommended — the Bookmark property is better suited for this task.
Once you set the PercentPosition property to a value, the record at the approximate position corresponding to that value becomes current, and the PercentPosition property is reset to a value that reflects the approximate position of the current record. For example, if your Recordset object contains only five records, and you set its PercentPosition property value to 77, the value returned from the PercentPosition property may be 80, not 77.
The PercentPosition property applies to all types of Recordset objects except for forward-only-type Recordset objects or Recordset objects opened from pass-through queries against remote databases.
You can use the PercentPosition property with a scroll bar on a form or text box to indicate the location of the current record in a Recordset object.
See Also
Bookmark property, Index property, Move method, MoveFirst, MoveLast, MoveNext, MovePrevious methods.
Example
This example uses the PercentPosition property to show the position of the current record pointer relative to the beginning of the Recordset.
Sub PercentPositionX()
Dim dbsNorthwind As Database
Dim rstProducts As Recordset
Dim strFind As String
Dim strMessage As String
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
' PercentPosition only works with dynasets or snapshots.
Set rstProducts = dbsNorthwind.OpenRecordset( _
"SELECT ProductName FROM Products " & _
"ORDER BY ProductName", dbOpenSnapshot)
With rstProducts
' Populate the Recordset.
.MoveLast
.MoveFirst
Do While True
' Show current record information and ask user
' for input.
strMessage = "Product: " & !ProductName & vbCr & _
"The record pointer is " & _
Format(.PercentPosition, "##0.0") & _
"% from the " & vbCr & _
"beginning of the Recordset." & vbCr & _
"Please enter a character search string " & _
"for a product name."
strFind = Trim(InputBox(strMessage))
If strFind = "" Then Exit Do
' Try to find a record matching the search string.
.FindFirst "ProductName >= '" & strFind & "'"
If .NoMatch Then .MoveLast
Loop
.Close
End With
dbsNorthwind.Close
End Sub
Example (Microsoft Access)
The following example expresses the current position of a record in a recordset as a percent, then displays it in a message box:
Sub PercentOfRecords()
Dim dbs As Database, rst As Recordset
Dim strInput As String, strSQL As String
strSQL = "SELECT * FROM Products ORDER BY ProductName;"
' Return reference to current database.
Set dbs = CurrentDb
' Open dynaset-type Recordset object.
Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset)
' Move to end of recordset.
rst.MoveLast
' Return to start of recordset.
rst.MoveFirst
' Prompt user to input name of product.
strInput = InputBox("Please enter the full product name.")
' Find first occurrence.
rst.FindFirst "ProductName = '" & strInput & "'"
' Display current position as a percent.
MsgBox("The current position in the Recordset is " _
& rst.PercentPosition & "%.")
rst.Close
Set dbs = Nothing
End Sub
Example (Microsoft Excel)
This example selects and changes records in the Customer recordset in the Nwindex.mdb database, and then it copies the recordset to Sheet1. The status bar shows the percentage of records that have been changed.
To create the Nwindex.mdb database, run the Microsoft Excel example for the CreateDatabase method.
Dim db As Database, rs As Recordset, sQLText As String
Set db = Workspaces(0).OpenDatabase(Application.Path & "\NWINDEX.MDB")
sQLText = "SELECT [CON_TITLE], [CONTACT], [COMPANY] FROM Customer " _
& "WHERE [CON_TITLE] = 'Owner';"
Set rs = db.OpenRecordset(sQLText, dbOpenDynaset)
rs.MoveFirst
Sheets("Sheet1").Activate
Do While Not rs.EOF
rs.Edit
rs.Fields("CON_TITLE").Value = "Account Excecutive"
rs.Update
Application.StatusBar = rs.PercentPosition & "% of the" _
& " records have been replaced."
rs.MoveNext
Loop
Application.StatusBar = "Done"
rs.MoveFirst
numberOfRows = ActiveCell.CopyFromRecordset(rs)
MsgBox numberOfRows & " Records have been changed"
rs.Close
db.Close