Method
Locates a record and copies it to a buffer.
Category
Records
Syntax
Function FindRec(ByVal hTable As Long, ByVal hRec As Long, ByVal
SearchMethod As String) As Boolean
hTable
The handle to the table.
hRec
As input: The record from which the search will begin.
As output: The record that is found. Any FlowFields associated with the record are set to zero; use CalcFields to update these fields.
SearchMethod
A string with one or more of these operators:
= |
finds the record equal to hRec. |
< |
finds a record less than hRec. |
> |
finds a record greater than hRec. |
- |
finds the first record in the table. |
+ |
finds the last record in the table. |
empty string |
means the same as =. |
An operator can only occur once. The operators + and - must be used alone.
If SearchMethod contains any of the operators =, > or < values must be assigned to all fields of the current key and the primary key in hRec before making this call.
Example
' Case "FindRec", AllocRec and NextRec
' Open Table 15 (G/Ledger)
CF1.OpenTable hTable, 15
hRecord = CF1.AllocRec(hTable)
' get last record
tmpVar = CF1.FindRec(hTable, hRecord, "+")
If VarType(tmpVar) = vbBoolean Then
logWr "FindRec OK"
Else
logWr "FindRec failed. It didn't return a boolean"
End If
tmpLastRecord = CF1.GetFieldData(hTable, hRecord, 1)
' loop through records
CF1.FindRec hTable, hRecord, "-"
tmpLong2 = 1 'used to count no of records
tmpLong = CF1.GetFieldData(hTable, hRecord, 1)
Do While tmpLong <> tmpLastRecord
tmpVar = CF1.NextRec(hTable, hRecord, 1)
If VarType(tmpVar) <> vbInteger Then
logWr "NextRec failed. It didn't return an Integer"
End If
tmpLong = CF1.GetFieldData(hTable, hRecord, 1)
tmpLong2 = tmpLong2 + 1
Loop
logWr "Stepped through " & Trim$(Str$(tmpLong2)) & " records. Data from the last one: " + Str$(tmpLastRecord)
CF1.FreeRec hRecord
Comments
FindRec retrieves the first record that meets the criteria set by
SearchMethod and the scope of any filters associated with the table
handle (set by SetFilter or
SetRange). The order in which
records are scanned is determined by the current key of the table
handle (set by SetCurrentKey).
The search starts from the values in the current key fields in hRec. If the current key is not the primary key, there is a chance that several records will have the same values in their current key fields. If this is the case, the values in the primary key fields of hRec are also used in the search.
If the record is found, the function returns TRUE. If the record is not found in the table, two things can happen:
The function returns FALSE, and, unless the error has been allowed, an error message will be displayed if HideError is FALSE.
If this result has been allowed by the AllowRecordNotFound function, it will not be considered an error. The function, however, still returns FALSE; and you should test this return value in your code.
For more information about how errors are handled, see Error Handling.