Results 1 to 4 of 4
  1. #1
    ssalem is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Feb 2013
    Posts
    39

    Error Trapping a Empty Recordset for a Parameter Query

    I am ready to tear my hair out. Something simple seems to be such a pain. I have done alot of research with different ideas but I just keep coming up with problems.

    I have a simple query based on a form for the user to put in the start and end dates they want to retreive totals for. I want to error check if an empty recordset is returned.

    Not sure what I am doing wrong. This is erroring out on the parameters. I had tried me.OpenRecordset.reccount = 0 but that did not work. When I hovered over it in VB code it displayed my TxtEndDate as the openrecordset.

    Dim dbs As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim rst As DAO.Recordset
    Set dbs = CurrentDb

    'Get the parameter query
    Set qfd = dbs.QueryDefs("Query Total by Month")

    ' set query parameters
    qdf.Parameters("TxtStartDate") = Me.TxtStartDate ' ("TxtStartDate") = Parameter in Query and me.TxtStartDate is the parameter entered on Form
    qdf.Parameters("TxtEndDate") = Me.TxtEndDate ' ("TxtEndDate") = Parameter in Query and Me.TxtEndDate is the parameter entered on Form


    ' Open a Recordset based on the parameter query
    Set rst = qdf.OpenRecordset()

    Me.Requery

    ' Check to see if query returns an empty record set and reset the form

    If rst.RecordCount = 0 Then
    MsgBox "No records Found for Parameters", vbExclamation
    Me.TxtStartDate = "01/01/2010" ' I have a blank record to reset all totals on form to blank and makes the form empty with 1 record in the query to start
    Me.TxtEndDate = "01/01/2010"
    Me.Requery

    Me.TxtStartDate = ""
    Me.TxtEndDate = ""
    Me.TxtMonth = ""
    Me.TxtYear = ""
    Exit Sub


    End If

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    You want to open a query with parameters that reference form controls for input?

    If only purpose of recordset is to verify that records will return for the input criteria, can maybe just use a DLookup or DCount domain aggregate.

    If DCount("ID","tablename","TxtStartDate=#" & Me.TxtStartDate & "# AND TxtEndDate=#" & Me.TxtEndDate & "#") = 0 Then
    ...
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    Are you getting a 0 count even when there are records in the record set?

    The standard approach ( I've been told it isn't necessary for tables) that is often used is

    Code:
    ...
    If rst.Bof =rst.Eof then MsgBox "There are no records in the recordset",vbOkOnly  ' reccount is 0
    
    rst.MoveLast  'needed to get correct count
    rst.MoveFirst 'needed to get to start of recordset
    Debug.print rst.RecordCount
    ....
    Oooops: I see June7 has posted while I was typing. I agree with her --depends on what you are actually doing. The DCount will work, as will the snippet I've shown.

  4. #4
    ssalem is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Feb 2013
    Posts
    39
    Thank you thank you........One of my problems worked out!!! Thank god I still have hair on my head...lol. I used the Dcount() and that did exactly what I wanted it to. I just replaced the query name where the tablename was and a couple other little tweaks within the flow of the sub and now it works!!! HARAYYYYY!!!!!!


    Thanks Again June 7

Please reply to this thread with any new information or opinions.

Similar Threads

  1. recordset open error on SQL query
    By Siuxia in forum Programming
    Replies: 5
    Last Post: 05-30-2012, 08:02 AM
  2. Append query error trapping
    By tpcervelo in forum Programming
    Replies: 4
    Last Post: 12-22-2011, 10:57 AM
  3. Proper Error Trapping
    By SemiAuto40 in forum Programming
    Replies: 6
    Last Post: 08-11-2011, 10:22 AM
  4. Form and Subform error trapping
    By usmcgrunt in forum Forms
    Replies: 8
    Last Post: 09-12-2010, 11:54 AM
  5. Access 2003 returns empty recordset
    By Leelers in forum Queries
    Replies: 0
    Last Post: 03-20-2009, 11:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums