Results 1 to 8 of 8
  1. #1
    crispy-bacon is offline Novice
    Windows XP Access 2007
    Join Date
    May 2011
    Posts
    13

    OpenRecordset vs. Query

    So I've run into a problem and I've tried everything I can think of and can't seem to find a solution. The problem I am having is the following:

    Dim rs As DAO.Recordset
    Set db = CurrentDb()


    Set rs = db.OpenRecordSet("...")

    So when I insert a query name (or SQL string) instead of "..." in the OpenRecordset command it does not return any records. When I run the query with the exact same criteria (same SQL statement) by clicking run or datasheet view it returns the appropriate records.

    There are no errors given by ms access it is always rs.EOF when I check if there are records. I have concatenated the criteria into the SQL string properly as far as I know as I have redone it 3 or 4 times.

    One more detail is that the strSQL that I am trying to use in the OpenRecordset command is selecting data from another query (not table).

    Any help is appreciated, I can post more details if required.
    Thanks!

  2. #2
    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,726
    I think you should show us all your vba code and the SQL string.

  3. #3
    crispy-bacon is offline Novice
    Windows XP Access 2007
    Join Date
    May 2011
    Posts
    13
    Will post the code first thing tomorrow, I am not currently on the machine with the access program

  4. #4
    Mordred is offline Novice
    Windows XP Access 2003
    Join Date
    Apr 2011
    Location
    Winnipeg, MB. Canada
    Posts
    8
    Quote Originally Posted by crispy-bacon View Post
    Will post the code first thing tomorrow, I am not currently on the machine with the access program
    You mean you don't take it home??

  5. #5
    crispy-bacon is offline Novice
    Windows XP Access 2007
    Join Date
    May 2011
    Posts
    13
    I've tried various things changing weekDate to referencing the control on the form with the right value with no luck.

    Code:
    Code:
    Private Sub equipConflict(weekDate As Date, weekNum As Integer)
    Dim db As Database
    Dim rs As DAO.Recordset
    Dim strSQL As String
    Dim Beam As String
    Dim lstName As String
     
    strSQL = "SELECT [qryEquipConflict].[AncillaryID], [qryEquipConflict].[contIndex], [qryEquipConflict].[Beam], [qryEquipConflict].[StartDate], [qryEquipConflict].[EndDate] " _
    & "FROM [qryEquipConflict] " _
    & "WHERE ([qryEquipConflict].[StartDate] Between " & weekDate & " And " & (weekDate + 6) & " );"
     
    Set db = CurrentDb()
    Set rs = db.OpenRecordset(strSQL)
     
    If Not rs.EOF Then rs.MoveFirst
     
    'Do While Not rs.EOF Never enters loop
     
    rs.Close
    Set rs = Nothing
    End Sub

  6. #6
    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,726
    You have a function with weeknum parameter, but you don't use it???

    You open a recordset but you don't loop through it??

    You may wan t to do some research on using Dates in access, and the need for the # delimiter.

    You may also do some investigation of debugging techniques-- msgbox, step through code; debug.print...

  7. #7
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You are sure the query "qryEquipConflict" has records??
    The two date parameters need to be delimited.

    Try:
    Code:
    & "WHERE ([qryEquipConflict].[StartDate] Between #"  & weekDate & "# And #" & (weekDate + 6) &  "#);"

  8. #8
    crispy-bacon is offline Novice
    Windows XP Access 2007
    Join Date
    May 2011
    Posts
    13
    @orange:
    -I removed irrelevant code from the lines of code I posted. The weekNum parameter is used
    -Line 17 is the Loop that uses the recordset which i also did not include

    Thank you both orange and ssanfu the delimeters were what was needed the strSQL now returns records.

    Silly mistake thanks!

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

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