Recordset Position Question

thechazm

VBA, VB.net, C#, Java
Local time
Today, 16:51
Joined
Mar 7, 2011
Messages
515
I have a recordset being populated with the same dataset as the form has.

What I need to understand is what is the best approach to set the starting position of the recordset to where the form is currently at.

Example's that I have tried:
i = long
fForm = Form
rs = dao.recordset

Code:
rs.bookmark = fForm.Bookmark ' This does not seem to work as it just errors.

Code:
i = fForm.Recordset.AbsolutePosition
 
rs.absoluteposition + i ' Will not let me add the variable i to it

Code:
i = fForm.Recordset.AbsolutePosition
 
rs.move i ' This tells me it's invalid

Anyway the reason why I am asking this is because I am adding on to my SearchAll function which searches all the fields in a form using one text box and is dynamic. It works just fine finding the first entry but I want it to be able to act as a findnext type of function so if you keep pressing the search button it will start at where you are currently at on the form and search for the next match and display that.

Thanks,

TheChazm
 
Perhaps you can define a recordset and use the recordset.bookmark option to select the starting postition.
 
Why not just use the RecordsetClone property?
 
I got it to work. I had to adjust my code. My recordset was the problem because I was looking at the data already filtered the way I originally had it setup. The reason why I don't want to use recordsetclone is because of the search functionality that I needed. Anyway so here is the chopped up version that I just got working but I'll fix it up later. Thanks for the inputs


Code:
Option Compare Database
Private NoForm As Boolean
''''''''''''''''''''''''''''''''''''''''''''''''
'Code Written By: Charles Branham
'Date: 02/13/2012
'Version: 1.0
''''''''''''''''''''''''''''''''''''''''''''''''
'Copyright:
'The code was entirely written by me, Charles Branham.
'Unless otherwise stated.
'All procedures are my work using Ideas gathered from resources available
' ... ie: Microsoft knowledge base.  As the distributor and primary
'author of this Module, I welcome you to use it in your application
'however, you MUST NOT gain financially from the use of the module or the code
'within it, in whole or in part! .. If you use my code in your application, then
'please give credit where credit is due.  If you want to use my code in a commercial
'application (ie: something someone spends money on), please contact me first...
[EMAIL="'charles.branham@navy.mil"]'charles.branham@navy.mil[/EMAIL], or at [EMAIL="thechazm@live.com"]thechazm@live.com[/EMAIL].
'Thanks for your honesty and cooperation ... Charles Branham
''''''''''''''''''''''''''''''''''''''''''''''''
'Application
'Version Information
'--------------------
'1.0    02/13/2012   Initial Release
Public Function SearchAll(strFormName As String, strCriteria As String)
Dim RFields, R, i As Long, ii As Long, bFirstPass As Boolean
Dim strSQL, strTableName As String, strSQLWHERE
Dim db As Database, rs As DAO.Recordset, iRecID As Long, fForm As Form
NoForm = False
bFirstPass = True
RFields = ReturnFields(strFormName)
If NoForm = True Then Exit Function
i = InStr(1, Forms(strFormName).RecordSource, "from")
If i = 0 Then
    '// This is a direct Table
    strTableName = "[" & Forms(strFormName).RecordSource & "]"
    
Else
    '// This is a query feeding form. Extract out the table name
    strTableName = Mid(Forms(strFormName).RecordSource, i + 5, Len(Forms(strFormName).RecordSource) - i) ' Strips out beginning SQL before table name
    
    If Mid(strTableName, 1, 1) = "[" Then ' Checks if table name used brackets
        i = InStr(1, strTableName, "]")
        strTableName = Mid(strTableName, 1, i) ' Grabs table name
    Else
        i = InStr(1, strTableName, "WHERE") ' Finds the WHERE Clause
        If i = 0 Then
            strTableName = "[" & Mid(strTableName, 1, Len(strTableName) - 1) & "]" ' If no where is found then it builds the table name.
        Else
            strTableName = "[" & Mid(strTableName, 1, i - 2) & "]" ' If where was found it strips of the end grabbing the table name.
        End If
        
        i = InStr(1, strTableName, "ORDER BY")
        If i <> 0 Then
            strTableName = Mid(strTableName, 1, i - 2) & "]"
        End If
        
    End If
    
End If
'strSQL = "Select * from " & strTableName & " WHERE ["
strSQL = "Select * from " & strTableName
For Each R In RFields
    If R = "" Then Exit For
    If bFirstPass = True Then
        'strSQL = strSQL & R & "] In(Select [" & R & "] from " & strTableName & " WHERE "
'        strSQL = strSQL & R & "] In(Select [" & R & "] from " & strTableName & " "
        bFirstPass = False
    Else
        strSQLWHERE = strSQLWHERE & "[" & R & "] LIKE '*" & strCriteria & "*' Or "
    End If
Next R
'// Add ) to end of where string
'strSQLWHERE = Mid(strSQLWHERE, 1, Len(strSQLWHERE) - 4) & ")"
strSQLWHERE = Mid(strSQLWHERE, 1, Len(strSQLWHERE) - 4)
'Debug.Print strSQL & strSQLWHERE
Set db = CurrentDb
'Set rs = db.OpenRecordset(strSQL & strSQLWHERE, dbOpenSnapshot)
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
Set fForm = Forms(strFormName)
i = fForm.Recordset.AbsolutePosition
rs.MoveLast
rs.MoveFirst
rs.Move i
rs.FindNext strSQLWHERE
If rs.NoMatch = True Then
    MsgBox "No records match your search. Please try again.", vbInformation, "Not Found"
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    Exit Function
Else
    iRecID = rs.Fields(0).Value
    rs.Close
    Set rs = Forms(strFormName).RecordsetClone
    rs.FindFirst rs.Fields(0).Name & " = " & iRecID
    If Not rs.EOF Then Forms(strFormName).Bookmark = rs.Bookmark ' Sets form to value!
End If
rs.Close
Set rs = Nothing
Set db = Nothing
End Function
Public Function ReturnFields(strFormName As String) As Variant ' Toggles through the field names that can be searched in the table
Dim R As Variant, i As Long
Dim rs As DAO.Recordset, db As Database
On Error GoTo ErrHandler
Set db = CurrentDb
If CurrentProject.AllForms(strFormName).IsLoaded = False Then
    MsgBox "The form has to be loaded for this function to work!"
    Exit Function
End If
Set rs = db.OpenRecordset(Forms(strFormName).RecordSource, dbOpenDynaset)
ReDim R(0 To 255)
Do While i <> rs.Fields.Count
    R(i) = rs.Fields(i).Name
    i = i + 1
Loop
rs.Close
Set rs = Nothing
ReturnFields = R
ErrHandler:
    If Err.Number = 2450 Then
        MsgBox "The form name you supplied is not valid! Please check your spelling.", vbCritical, "Oops"
        NoForm = True
        Set rs = Nothing
        Exit Function
    End If
    Resume Next
        
End Function
 

Users who are viewing this thread

Back
Top Bottom