Solved

Select statement vs RecordsetClone

Posted on 2010-08-24
13
319 Views
Last Modified: 2012-06-27
I attached some access code (I'm linked to SQL 2008) and I don't understand why it's working the way it is.
If I run the code the way it is using "Set rs = Me.RecordsetClone" it falls thru rs.findfirst and is painfully slow but it finds a matching record and gives the desired results.
If I use the commented out SQL statement (which is the SQL code I copied from my record source) then it falls thru the findfirst much quicker but it does not find a match.  What am I doing wrong.  

Set db = CurrentDb()
'       strKey1 = "SELECT dbo_tblCaseNotes.ClientFileNo, dbo_tblClients.LastName, dbo_tblClients.FirstName, dbo_tblClients.MiddleName, "
'              strKey1 = strKey1 & "dbo_tblCaseNotes.CaseNotesID, dbo_tblCaseNotes.CaseNotesDate, dbo_tblCaseNotes.SupportStaffLkUpID "
'              strKey1 = strKey1 & "FROM (dbo_tblClients INNER JOIN dbo_tblCaseNotes ON dbo_tblClients.ClientFileNo = dbo_tblCaseNotes.ClientFileNo) "
'              strKey1 = strKey1 & "LEFT JOIN dbo_tblLkUpSupportStaff ON dbo_tblCaseNotes.SupportStaffLkUpID = dbo_tblLkUpSupportStaff.SupportStaffLkUpID "
'              strKey1 = strKey1 & "ORDER BY dbo_tblCaseNotes.ClientFileNo, dbo_tblCaseNotes.CaseNotesDate DESC , dbo_tblCaseNotes.SupportStaffLkUpID; "
'              Set rs = db.OpenRecordset(strKey1, dbOpenSnapshot)

        Set rs = Me.RecordsetClone
       
        If bDelete = True Then
            rs.FindFirst "[ClientFileNo]& [CaseNotesId] = " & txtClientFileNo & txtCaseNotesID
            Exit Sub
        End If
        If bNewRevu = True Then
            Call AddInterventionRcd  'if adding new
            'if dblCaseNotesID has value then a new rcd was added and screen refreshed so use saved off dbl values
            If dblCaseNotesID = 0 Then
                rs.FindFirst "[ClientFileNo]& [CaseNotesId] = " & txtClientFileNo & cboCaseNotesDate.Value
            Else
                rs.FindFirst "[ClientFileNo]& [CaseNotesId] = " & dblClientFileNo & dblCaseNotesID
            End If
            bNewRevu = False


SQL code for the recordsource = 
SELECT dbo_tblCaseNotes.ClientFileNo, dbo_tblClients.LastName, dbo_tblClients.FirstName, dbo_tblClients.MiddleName, dbo_tblCaseNotes.CaseNotesID, dbo_tblCaseNotes.CaseNotesDate, dbo_tblCaseNotes.SupportStaffLkUpID
FROM (dbo_tblClients INNER JOIN dbo_tblCaseNotes ON dbo_tblClients.ClientFileNo = dbo_tblCaseNotes.ClientFileNo) LEFT JOIN dbo_tblLkUpSupportStaff ON dbo_tblCaseNotes.SupportStaffLkUpID = dbo_tblLkUpSupportStaff.SupportStaffLkUpID
ORDER BY dbo_tblCaseNotes.ClientFileNo, dbo_tblCaseNotes.CaseNotesDate DESC , dbo_tblCaseNotes.SupportStaffLkUpID;

Open in new window

0
Comment
Question by:BobRosas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 33515425
Who knows what kind of SQL exchanges go on behind a recordset and a backend table.  But, your logic is client based, meaning the search logic is running on the client. In some circumstances the data provider is smart enough to "trump" your logic and do a server-side search, but not always.  I expect that is the difference.  Also, the type of recordset may may a difference.

My suggestion is to force a server-side filter by putting your search arguments in the where clause of your SQL rather than using findfirst.  
0
 

Author Comment

by:BobRosas
ID: 33515748
Thank you for your help.  What you suggest sounds like a good idea.  I tried adding in the arguments and I get the error...
Too few parameters, Expected 2
Do those go in with the "Set rs" statement some where?
I could really use some help with the syntax.   I've attached what I have so far.
Thanks

 strKey1 = "SELECT dbo_tblCaseNotes.ClientFileNo, dbo_tblClients.LastName, dbo_tblClients.FirstName, dbo_tblClients.MiddleName, "
              strKey1 = strKey1 & "dbo_tblCaseNotes.CaseNotesID, dbo_tblCaseNotes.CaseNotesDate, dbo_tblCaseNotes.SupportStaffLkUpID "
              strKey1 = strKey1 & "FROM (dbo_tblClients INNER JOIN dbo_tblCaseNotes ON dbo_tblClients.ClientFileNo = dbo_tblCaseNotes.ClientFileNo) "
              strKey1 = strKey1 & "LEFT JOIN dbo_tblLkUpSupportStaff ON dbo_tblCaseNotes.SupportStaffLkUpID = dbo_tblLkUpSupportStaff.SupportStaffLkUpID "
              strKey1 = strKey1 & "WHERE (((dbo_tblCaseNotes.ClientFileNo)=[Forms]![frmInterventionTime]![txtClientFileNoTmp]) AND "
              strKey1 = strKey1 & "((dbo_tblCaseNotes.CaseNotesID)=[Forms]![frmInterventionTime]![txtCaseNotesIDTmp])) "
              strKey1 = strKey1 & "ORDER BY dbo_tblCaseNotes.ClientFileNo, dbo_tblCaseNotes.CaseNotesDate DESC , dbo_tblCaseNotes.SupportStaffLkUpID; "
              Set rs = db.OpenRecordset(strKey1, dbOpenSnapshot)

Open in new window

0
 

Author Comment

by:BobRosas
ID: 33516316
I've attached some code that I've tried.  I'm not even sure if I'm going the right direction.
Now I get the error that "you must use the dbSeeChanges...", I guess I'm not using it correctly because that is the line of code where it stops working.
strKey1 = "SELECT dbo_tblCaseNotes.ClientFileNo, dbo_tblClients.LastName, dbo_tblClients.FirstName, dbo_tblClients.MiddleName, "
              strKey1 = strKey1 & "dbo_tblCaseNotes.CaseNotesID, dbo_tblCaseNotes.CaseNotesDate, dbo_tblCaseNotes.SupportStaffLkUpID "
              strKey1 = strKey1 & "FROM (dbo_tblClients INNER JOIN dbo_tblCaseNotes ON dbo_tblClients.ClientFileNo = dbo_tblCaseNotes.ClientFileNo) "
              strKey1 = strKey1 & "LEFT JOIN dbo_tblLkUpSupportStaff ON dbo_tblCaseNotes.SupportStaffLkUpID = dbo_tblLkUpSupportStaff.SupportStaffLkUpID "
              strKey1 = strKey1 & "WHERE (dbo_tblCaseNotes.ClientFileNo = txtClientFileNoTmp And "
              strKey1 = strKey1 & "CaseNotesID = txtCaseNotesIDTmp)"
              strKey1 = strKey1 & "ORDER BY dbo_tblCaseNotes.ClientFileNo, dbo_tblCaseNotes.CaseNotesDate DESC , dbo_tblCaseNotes.SupportStaffLkUpID; "
              Set qdf = db.CreateQueryDef("", strKey1)
                qdf.Parameters("[txtClientFileNoTmp]") = txtClientFileNoTmp
                qdf.Parameters("[txtCaseNotesIDTmp]") = txtCaseNotesIDTmp
                Set rs = qdf.OpenRecordset(, dbSeeChanges)  "ERROR HERE

Open in new window

0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 42

Expert Comment

by:dqmq
ID: 33517936
Either method is OK once you work out the kinks. I'd tend to stick with the first method, but do the form substitutions in VBA, like this:

 strKey1 = "SELECT dbo_tblCaseNotes.ClientFileNo, dbo_tblClients.LastName, dbo_tblClients.FirstName, dbo_tblClients.MiddleName, "
              strKey1 = strKey1 & "dbo_tblCaseNotes.CaseNotesID, dbo_tblCaseNotes.CaseNotesDate, dbo_tblCaseNotes.SupportStaffLkUpID "
              strKey1 = strKey1 & "FROM (dbo_tblClients INNER JOIN dbo_tblCaseNotes ON dbo_tblClients.ClientFileNo = dbo_tblCaseNotes.ClientFileNo) "
              strKey1 = strKey1 & "LEFT JOIN dbo_tblLkUpSupportStaff ON dbo_tblCaseNotes.SupportStaffLkUpID = dbo_tblLkUpSupportStaff.SupportStaffLkUpID "

'>>>>note the changes here
              strKey1 = strKey1 & "WHERE (((dbo_tblCaseNotes.ClientFileNo)=" & [Forms]![frmInterventionTime]![txtClientFileNoTmp]) & " AND "
              strKey1 = strKey1 & "((dbo_tblCaseNotes.CaseNotesID)=" & [Forms]![frmInterventionTime]![txtCaseNotesIDTmp])) & " "
 
              strKey1 = strKey1 & "ORDER BY dbo_tblCaseNotes.ClientFileNo, dbo_tblCaseNotes.CaseNotesDate DESC , dbo_tblCaseNotes.SupportStaffLkUpID; "
0
 

Author Comment

by:BobRosas
ID: 33522404
I'd rather do it the way you suggested.  However I can't figure out why lines 4 & 5 won't compile.
See attached.  Would you help me with that?

strKey1.JPG
0
 
LVL 42

Expert Comment

by:dqmq
ID: 33522558
What is the error message?
0
 
LVL 42

Expert Comment

by:dqmq
ID: 33522693
Never mind, I see the problem:
The reference to the form field must stand by itself as a distinct term between the ampersands. The closing parens that follow it are wrong.  The need to be moved inside the quote marks that immediately follow.

wrong:
...[txtFileNoTmp]) & " AND"

right:
...[txtFileNoTmp] & ") AND"
0
 

Author Comment

by:BobRosas
ID: 33523182
Thank you!  Now it compiles but I still get an error.  Now that I have the string shouldn't it work in the findfirst?  I get the error "Syntax error in expression" on the rs.FindFirst line.  Here is how it looks so far...
Set db = CurrentDb()
Set rs = Me.RecordsetClone
strKey1 = "SELECT dbo_tblCaseNotes.ClientFileNo, dbo_tblClients.LastName, dbo_tblClients.FirstName, dbo_tblClients.MiddleName, "
              strKey1 = strKey1 & "dbo_tblCaseNotes.CaseNotesID, dbo_tblCaseNotes.CaseNotesDate, dbo_tblCaseNotes.SupportStaffLkUpID "
              strKey1 = strKey1 & "FROM (dbo_tblClients INNER JOIN dbo_tblCaseNotes ON dbo_tblClients.ClientFileNo = dbo_tblCaseNotes.ClientFileNo) "
              strKey1 = strKey1 & "LEFT JOIN dbo_tblLkUpSupportStaff ON dbo_tblCaseNotes.SupportStaffLkUpID = dbo_tblLkUpSupportStaff.SupportStaffLkUpID "
              strKey1 = strKey1 & "WHERE (((dbo_tblCaseNotes.ClientFileNo)=" & [Forms]![frmInterventionTime]![txtClientFileNoTmp] & ") AND "
              strKey1 = strKey1 & "((dbo_tblCaseNotes.CaseNotesID)=" & [Forms]![frmInterventionTime]![txtCaseNotesIDTmp] & " )"
              strKey1 = strKey1 & "ORDER BY dbo_tblCaseNotes.ClientFileNo, dbo_tblCaseNotes.CaseNotesDate DESC , dbo_tblCaseNotes.SupportStaffLkUpID; "
rs.FindFirst strKey1
0
 
LVL 42

Expert Comment

by:dqmq
ID: 33524451
You don't want strKey1 on the findfirst.

The whole point is to apply the filter cirteria on the server so you don't need to do it on the client.  As coded, the recordset will only contain records that already match your criteria, so just do "rs.Findfirst" to get the first one.
0
 

Author Comment

by:BobRosas
ID: 33524633
Thank you for your help.  I've increased your points becaused I really appreciate all you have done.  But I still can't get it to work.  If I use the string I posted above and then use
rs.FindFirst "[ClientFileNo]=" & dblClientFileNo & " And [CaseNotesID] = " & dblCaseNotesID
it runs but it also runs without the string.  I not setting the recordset to the string anywhere and I don't know how.  I thought it would become part of the findfirst code.
.
0
 
LVL 42

Accepted Solution

by:
dqmq earned 250 total points
ID: 33525804
>If I use the string I posted above and then use
rs.FindFirst "[ClientFileNo]=" & dblClientFileNo & " And [CaseNotesID] = " & dblCaseNotesID
it runs but it also runs without the string.

Because you have a where clause in the select statement that feeds the recordset, the server only sends back the rows that match the conditions of the where clause, i.e. the fields referenced on your form.  .Findfirst navigates through the recordset, appying the same conditions again, which is unnecessary.  

>I not setting the recordset to the string anywhere and I don't know how.  
Are you not doing this:

Set rs = db.OpenRecordset(strKey1, dbOpenSnapshot)


Perhaps it's time to re-post your latest code and clarify what is not working



0
 

Author Comment

by:BobRosas
ID: 33525876
No I did not have
Set rs = db.OpenRecordset(strKey1, dbOpenSnapshot)
I added it and now I get "Run-time error '3075':  Syntax error (missing operator) in query expression...."
on that line.
I've attached my current code.
Set db = CurrentDb()
strKey1 = "SELECT dbo_tblCaseNotes.ClientFileNo, dbo_tblClients.LastName, dbo_tblClients.FirstName, dbo_tblClients.MiddleName, "
              strKey1 = strKey1 & "dbo_tblCaseNotes.CaseNotesID, dbo_tblCaseNotes.CaseNotesDate, dbo_tblCaseNotes.SupportStaffLkUpID "
              strKey1 = strKey1 & "FROM (dbo_tblClients INNER JOIN dbo_tblCaseNotes ON dbo_tblClients.ClientFileNo = dbo_tblCaseNotes.ClientFileNo) "
              strKey1 = strKey1 & "LEFT JOIN dbo_tblLkUpSupportStaff ON dbo_tblCaseNotes.SupportStaffLkUpID = dbo_tblLkUpSupportStaff.SupportStaffLkUpID "
              strKey1 = strKey1 & "WHERE (((dbo_tblCaseNotes.ClientFileNo)=" & dblClientFileNo & ") AND "
              strKey1 = strKey1 & "((dbo_tblCaseNotes.CaseNotesID)=" & dblCaseNotesID & " )"
              strKey1 = strKey1 & "ORDER BY dbo_tblCaseNotes.ClientFileNo, dbo_tblCaseNotes.CaseNotesDate DESC , dbo_tblCaseNotes.SupportStaffLkUpID; "

              Set rs = db.OpenRecordset(strKey1, dbOpenSnapshot)  'syntax error in query expression
              rs.FindFirst "[ClientFileNo]=" & dblClientFileNo & " And [CaseNotesID] = " & dblCaseNotesID

Open in new window

0
 

Author Closing Comment

by:BobRosas
ID: 33667489
I know that becauise of your help I'm now much closer to the solution so I will award points and post a related question.  Thanks again for all your help.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need return values from a stored procedure 8 46
SQL works but want to get the XML node data separately 11 31
Database Owner 3 21
denied execute as 13 33
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question