Question : speed up findfirst with query definition

I have Access 2007 as front end and SQL Server 2008 as a backend.  I was using...
Set rs = Me.RecordsetClone
rs.findfirst etc
Since it was really slow I changed to a query definition with parameters.
Shouldn't that be faster?   Because in my case it's not.
Can you please look at my attached code and tell me what I did wrong?  It actually seems to be working but it's still very slow.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
Private Sub cboEvalDate_AfterUpdate()           ' Lookup by date
        Dim db As DAO.Database
        Dim qd As DAO.QueryDef
        Dim rs As DAO.Recordset
        Set db = CurrentDb()
        Set qd = db.QueryDefs!qryEvaluation
        qd.Parameters!ClientFileNo = txtClientFileNo
        qd.Parameters!WorkstationID = txtWorkstationID
        qd.Parameters!EvalDate = cboEvalDate.Text
        Set rs = qd.OpenRecordset
               If bNewRevu = True Then  'this if 021108 4:00
            Call AddEvalRcd 'if adding 1st date/eval to a workstation
            bNewRevu = False
            cboName.Value = Null
            cboWorkstation.Value = Null
            cboEvalDate.Value = Null
            DoCmd.GoToControl "fsubEvalService"
            Exit Sub
        Else
            'very slow here on lookup 
            rs.FindFirst "[ClientFileNo]& [WorkstationID]& [EvalID] = " & txtClientFileNo & txtWorkstationID & cboEvalDate.Value
         End If

Answer : speed up findfirst with query definition

So what we're doing here is finding the record in the current recordset by using the RecordsetClone object.  Once the record is found we set the form bookmark to be the same as the recordset bookmark so the form will display the desired record.  There are a few things to consider such as what if there are unsaved changes on the form when the user selects the combo box.  You can add a save record command to that part of the sub or you can test for it and do something else.  Also, what happens if a match is not found?  This may or may not be a possibility based upon your form design.  Look at Allen Browne's example for insite http://allenbrowne.com/ser-03.html

Private Sub cboEvalDate_AfterUpdate()           ' Lookup by date
        Dim db As DAO.Database
        Dim qd As DAO.QueryDef
        Dim rs As DAO.Recordset, rsFind As DAO.Recordset
        Set db = CurrentDb()
        Set qd = db.QueryDefs!qryEvaluation
        qd.Parameters!ClientFileNo = txtClientFileNo
        qd.Parameters!WorkstationID = txtWorkstationID
        qd.Parameters!EvalDate = cboEvalDate.Text
        Set rs = qd.OpenRecordset
        If bNewRevu = True Then  'this if 021108 4:00
            Call AddEvalRcd 'if adding 1st date/eval to a workstation
            bNewRevu = False
            cboName.Value = Null
            cboWorkstation.Value = Null
            cboEvalDate.Value = Null
            DoCmd.GoToControl "fsubEvalService"
            Exit Sub
        Else
            'allow user to select from dropdown (using EvalID)
            'very slow here on lookup 7/12/10
            'rs.MoveFirst

            Set rsFind = Me.RecordsetClone
                'assumes ClientFileNo and WorkstationID are text fields.  Remove surrounding quotes (Chr(34)) if either are numeric
            rsFind.FindFirst "EvalDate = #" & Me.cboEvalDate & "# And ClientFileNo = " & Chr(34) & Me.txtClientFileNo & Chr(34) & " And WorkstationID = " & Chr(34) & Me.txtWorkstationID & Chr(34)
                'set the form book mark to the recordset bookmark
            Me.Boolmark = rsFind.Bookmark
            Set rsFind = Nothing

         End If

OM Gang
Random Solutions  
 
programming4us programming4us