Problems with RecordsetClone.RecordCount in Access 2007

Posted on 2009-07-07
Medium Priority
Last Modified: 2013-11-28
I have created a form to search the database that has several unbound fields in the form header that allow a user to enter criteria that gets dynamically filtered as they are typing.  And it works perfectly.....in Access 2003.  I have users that will be using both Access 2003 and 2007 so I figured that I would take it for a test run in 2007 and it works....for one letter.  I went thru and debugged the code to figure out why I was always getting "No Records Returned" after I entered a second letter....

For some reason this code works in 2003, but not 2007:
 If Me.RecordsetClone.RecordCount = 0 Then
        MsgBox "No records returned"

I can't just get rid of the code because of other access bugs dealing with unbound textboxes in the form header (I can show you the threads on EE if you like).  So what I need is a way to test the number of records in my form after a filter is applied to it that works everytime and not just the first time I run thru the code.

I have looked around and seen lots of problems with the RecordCount in 07, but they all seemed to happen when trying to use the information in a textbox.

Any ideas?

Access 07 is on a different computer with Vista where  the other computer is Access 03 and XP.  I also installed all the updates from Windows Update two days ago so Access 07 is up to date.

Oh the joys of Microsoft....

Thanks for any help in advance
Question by:jackrabbitman
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
  • 8
  • 4
  • 2
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 200 total points
ID: 24796250
For some reason I'm finding it hard to believe that

 If Me.RecordsetClone.RecordCount = 0 Then

does not work.  Are you saying it's not returning zero when it IS zero?  If you put a breakpoint on that line of code, what is the value of the RecordCount ?


Author Comment

ID: 24796412
Alright you win, Me.RecordsetClone.RecordCount = 0 is right....so now I have to go find out why it brings back 0 records with the same Filtering criteria as  I used in 2003
LVL 75
ID: 24796442
I would say you win :-)

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.


Author Comment

ID: 24796492
Okay....I just don't get it.  I have made a button that runs the same code (and "turns off" the onChange event that normally occurs) and if I enter in the same criteria that I'm trying to enter in with the dynamic filter and it works.....but once again only one time and it comes back with 0 records the second time again.....

Author Comment

ID: 24796523
P.S. Looks like access is winning right now
LVL 75
ID: 24796544
Unfortunately, I'm not running A2007 yet.  A few experts here are however.


Author Comment

ID: 24796586
Well thanks for your insight.....and I wish I didn't have to, but seeing as half the office is using 07 and the rest will eventually get there...something that works in 2003 and not 2007 isn't really an option.

Any help from someone with Office 07?
LVL 44

Assisted Solution

by:Leigh Purvis
Leigh Purvis earned 200 total points
ID: 24796817
Unless I'm missing something - you've not actually given us anything to work with as yet?
(Other than a description of your scenario).
Expression suppose in 2007 has been hacked back yes. So whereas you could freely build expression in previous versions referring to the RecordCount (and even to objects such as CurrentDb) that isn't available in 2007.
However in code your comparisons should be fine.
It seems that you're saying that the problem is now with your search.
We don't know much about that though - other than you're running code for a dynamic filter based on entered text. :-s

Author Comment

ID: 24797029
Haha....good point.  I'm sorry that I made the assumption that people I've worked with before on this same form would be helping me (such as MX) so....i'll post the code that shows the filter information and perhaps you could provide some insight....

Sorry the style isn't that great, but I kept running into NULL problems.

And to point out what I said before...the code works with the button that I put in that ignores the onChange event for each text box and runs this same code.
Sub searchData()
    'Declare all variables
    Dim condition As Boolean
    Dim criteria As String
    Dim length As Integer
    Dim oldCriteria As String
    Dim varCname As String
    Dim varFname As String
    Dim varLname As String
    Dim count As Integer
    count = 0
    'Fill variables if they are not null in case of Error
    If Me.txtCname <> "" Then
        varCname = Me.txtCname
        varCname = ""
    End If
    If Me.txtFname <> "" Then
        varFname = Me.txtFname
        varFname = ""
    End If
    If Me.txtLname <> "" Then
        varLname = Me.txtLname
        varLname = ""
    End If   
    If Me.Filter <> "" Then
        oldCriteria = Me.Filter
        oldCriteria = ""
    End If
    criteria = ""
    condition = False
    With Me
        'Company Name Criteria
        .txtCname2 = .[txtCname].Text
        If .txtCname2 <> "" Then
            length = Len(.txtCname2)
            criteria = "(((Left(CompanyName, " & length & ")) = [Forms]![Search].txtCname)) "
            condition = True
        End If
        'First Name Criteria
        .txtFname2 = .[txtFname].Text
        If .txtFname2 <> "" And criteria = "" Then
            length = Len(.txtFname2)
            criteria = "(((Left(Fname, " & length & ")) = [Forms]![Search].txtFname)) "
            condition = True
        ElseIf .txtFname2 <> "" Then
            length = Len(.txtFname2)
            criteria = criteria & "AND (((Left(Fname, " & length & ")) = [Forms]![Search]![txtFname])) "
        End If
        'Last Name Criteria
        .txtLname2 = .[txtLname].Text
        If .txtLname2 <> "" And criteria = "" Then
            length = Len(.txtLname2)
            criteria = "(((Left(Lname, " & length & ")) = [Forms]![Search].[txtLname])) "
            condition = True
        ElseIf .txtLname2 <> "" Then
            length = Len(.txtLname2)
            criteria = criteria & "AND (((Left(Lname, " & length & ")) = [Forms]![Search].[txtLname])) "
        End If
    End With
    'Apply Criteria to the filter
    If condition Then
        Forms!Search.Filter = criteria
        Forms!Search.FilterOn = True
        Forms!Search.Filter = ""
        Forms!Search.FilterOn = False
    End If
    If Me.RecordsetClone.RecordCount = 0 Then
        MsgBox "No records returned"
        'For whatever reason the filter doesn't like to = "" on the first time and if this
        'doesn't happen it can mess up the code, so this loop is set up to fix that problem
            Me.Filter = ""
            count = count + 1
        Loop Until Me.Filter = "" Or count = 10
        Me.FilterOn = False
        'Assign propper value to txtCname
        If varCname = "" Then
            Me.txtCname = ""
            Me.txtCname = varCname
        End If
        'Assign propper value to txtFname
        If varFname = "" Then
            Me.txtFname = ""
            Me.txtFname = varFname
        End If
        'Assign propper value to txtLname
        If varLname = "" Then
            Me.txtLname = ""
            Me.txtLname = varLname
        End If
        If oldCriteria <> "" Then
            Me.Filter = oldCriteria
            Me.FilterOn = True
        End If
    End If
End Sub

Open in new window


Author Comment

ID: 24797430
I've done some more testing in 07 now and I guess I was wrong again...it seems to be in my code that something isn't getting updated or something so...it will work more than once now so I can't say for sure what it is....

Accepted Solution

jackrabbitman earned 0 total points
ID: 24798650
Well for whatever reason it didn't like the code, I don't know, but I used what Access uses for its "Begins With" filter and did

criteria =  "([Table].CompanyName Like " & Chr(34) & txtCname2 & "*" & Chr(34) & ")"

and that seems to work no problem so now I'll just have to go and make all those changes.
LVL 75
ID: 24798667
strange for sure ...


Author Comment

ID: 24798720
And it works in 2003 as well so guess thats definatly what i'll be going with.
LVL 44

Expert Comment

by:Leigh Purvis
ID: 24799128
Well, not wanting to halt any automatic procedure thus giving the mods more to do - but this is the first chance I've had to come back and look at this - so I might as well risk it and comment anyway (things sure do happen in a rush at EE)...
The code section offered of itself is more informative.
For the solution, you're saying you used:
criteria =  "([Table].CompanyName Like " & Chr(34) & txtCname2 & "*" & Chr(34) & ")"
instead of:
criteria = "(((Left(CompanyName, " & length & ")) = [Forms]![Search].txtCname)) "
(Using "txtCname2" just for some testing reason perhaps? And the difference being in the criteria syntax - not that you've included the TableName prefix?)
Ultimately, the replacement syntax is definitely preferable.
Wrapping fields in function calls always adds overhead - even if there isn't an index that could have been made use of.
(If there is then it's even worse).
The latter requires that the form reference be correctly resolved as part of the expression.
As opposed to concatenating in the value as you're now doing:
criteria = "Left(CompanyName, " & length & ") = " & Chr(34) & [Forms]![Search].txtCname " & Chr(34) & ")"
or forcing evaluation:
criteria = "(((Left(CompanyName, " & length & ")) = Eval(""[Forms]![Search].txtCname""))) "
as such parameters can struggle with evaluation even in earlier versions of Access.
However, neither's a great option.
The absolutely appropriate syntax is to use the Like operator as you now are.
Equally applicable should be:
criteria =  "(CompanyName Like '" & txtCname2 & "*')"

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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