Results 1 to 11 of 11
  1. #1
    bbrazeau is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    101

    OpenRecordset multiple criteria

    Hi, Is there a way to use multiple criteria when setting up a record set. The code below gets a recordset that meets criteria of MyPress_ID =[Schedule].Press_ID (MyPress_ID is a variable) I would like to have it further narrow the recordset by only including records after a certain date which I also have as a variable (MyStartDate). Could someone show me the correct syntax for this? Thank you.



    strSQL = "SELECT * FROM [Schedule] WHERE ( " & MyPress_ID & " =[Schedule].Press_ID );"
    Set MyRecordSet = MyDB.OpenRecordset(strSQL, dbOpenDynaset)

  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,743
    This is a great set of tutorials. Several SQL and vba topics with full examples. Enjoy.
    http://www.fontstuff.com/access/acctut14.htm

  3. #3
    bbrazeau is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    101
    Hi orange link does not seem to work?

  4. #4
    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,743
    Try this, then choose the SQL/vba tutorials. Not sure why the last one isn't working. It could be down for maintenance??
    http://www.fontstuff.com/access/index.htm


    I just tried them both again and they work????
    http://www.fontstuff.com/access/acctut14.htm

  5. #5
    bbrazeau is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    101
    Hi Orange, Tried several times and link is not working for me. below is the syntax I'm trying to use. Do you see an error?

    strSQL = "SELECT * FROM [Schedule] WHERE ( " & MyPress_ID & " =[Schedule].Press_ID AND " & MyStartDate & " > [Schedule].[Start_Date] );"

  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,743
    You can try this:

    strSQL = "SELECT * FROM [Schedule] WHERE Press_ID = " & MyPress_ID & " AND Start_Date < #" & MyStartDate & "#"

    I'm not having any trouble with links to Martin's material.
    Last edited by orange; 11-02-2011 at 05:30 PM.

  7. #7
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Yes, I see several:

    It Should be

    strSQL = "SELECT * FROM [Schedule] WHERE [Schedule].Press_ID=" & MyPress_ID & " AND [Schedule].[Start_Date] < #" & MyStartDate & "#"

    Your logic was backwards. The FIELD is first and then the value is checked, not the other way around. Also, you don't need the semi-colon at the end and you have to use Octothorpes (#) for Date Fields.

  8. #8
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Seems like I posted the same answer as orange albeit late.

  9. #9
    bbrazeau is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2011
    Posts
    101

    OpenRecordset multiple criteria

    Thanks Guys, I got mine to work late in the day. Here's what I ended up with:
    strSQL = "SELECT * FROM [Schedule] WHERE ( " & MyPress_ID & " =[Schedule].Press_ID AND [Schedule].Start_Date >= #" & MyStartDate & "# );"

    Orange, I tried the links again this morning and their working fine. Must have been down for maint. or something.

  10. #10
    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,743
    Glad you got it working. The tutorials on Martin's site are very good. Suggest you give them a try -- they'll make a good reference for any sql /vba activity.

  11. #11
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Just an FYI for you. If you don't have a bunch of JOINS, your Where Clause doesn't need the parentheses. It makes things simpler as you could have just used:

    strSQL = "SELECT * FROM [Schedule] WHERE " & MyPress_ID & " =[Schedule].Press_ID AND [Schedule].Start_Date >= #" & MyStartDate & "#"

    (you don't need the semi-colon at the end either)

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

Similar Threads

  1. Iif statement with multiple criteria
    By coach32 in forum Queries
    Replies: 1
    Last Post: 09-07-2011, 01:28 AM
  2. DLookup in strSQL for OpenRecordset
    By szucker1 in forum Programming
    Replies: 2
    Last Post: 07-30-2011, 06:00 PM
  3. list box with multiple criteria
    By white_flag in forum Access
    Replies: 6
    Last Post: 07-25-2011, 11:25 AM
  4. Replies: 1
    Last Post: 07-13-2011, 11:00 AM
  5. OpenRecordset vs. Query
    By crispy-bacon in forum Programming
    Replies: 7
    Last Post: 07-04-2011, 09:52 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