Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 33
  1. #16
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940

    Code:
    If (strCurClassID >= 2 And strCurClassID <= 5) Then
        rsEMAs.Filter = "ClassID = " & strCurClassID
    End If
    Set rsEMAsFltrd = rsEMAs.OpenRecordset
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  2. #17
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    What about something like:

    Code:
    If (strCurClassID >= 2 And strCurClassID <= 5) Then
    
          strSql = "Select * from sometable where ClassID = " & strCurClassID
    
    Else
    
          strSql = "Select * from sometable
    
    End If
    
        Set db = CurrentDb()
        Set rs = db.OpenRecordset(strSql)
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #18
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    This might work if one could specify the domain with query rather than a table.
    Code:
    If (strCurClassID >= 2 And strCurClassID <= 5) Then
    
          strSql = "Select * from SomeQuery where ClassID = " & strCurClassID
    Else
          strSql = "Select * from SomeQuery 
    
    
    End If
    
        Set db = CurrentDb()
        Set rs = db.OpenRecordset(strSql)

  4. #19
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by GraeagleBill View Post
    This might work if one could specify the domain with query rather than a table.
    Code:
    If (strCurClassID >= 2 And strCurClassID <= 5) Then
    
          strSql = "Select * from SomeQuery where ClassID = " & strCurClassID
    Else
          strSql = "Select * from SomeQuery 
    
    
    End If
    
        Set db = CurrentDb()
        Set rs = db.OpenRecordset(strSql)
    What is to there to stop you?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #20
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Just to reinforce what micron and WGM said - the source can be a table or a stored query.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #21
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Quote Originally Posted by Minty View Post
    Just to reinforce what micron and WGM said - the source can be a table or a stored query.
    You could also probably finagle a querydef too, but my whole point was I think you were over complicating the procedure to begin with.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #22
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    I tried the following even before the OP:

    Code:
    strSQL = "SELECT * FROM QName WHERE ClassID = " & strCurClassID
    
    
    Set rsEMAs = DBEngine(0)(0).OpenRecordset(strSQL)
    AND
    Code:
    strSQL = "SELECT * FROM " & QName & " WHERE ClassID = " & strCurClassID
    
    
    Set rsEMAs = DBEngine(0)(0).OpenRecordset(strSQL)
    AND
    Code:
    strSQL = "SELECT * FROM QEMA-ALL WHERE ClassID = " & strCurClassID
    
    
    Set rsEMAs = DBEngine(0)(0).OpenRecordset(strSQL)
    I followed the string value of strSQL in debug and it seemed to be okay, but after Access complained about the FROM clause, I switched to a different approach that ultimately led to the OP.

  8. #23
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Well the first attempt is never going to work , but I cannot see anything wrong with the other two.?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #24
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    So, I returned to the strSQL SELECT approach and finally found that Access simply didn't like the way the 18-year old query was named. Since I've long since stopped using any kind of "-", "_" or anything else in naming objects and variables, it just didn't occur to me right off that the way the "old" query was named was the source of the whole approach. All I had to do was rename the query.

    Code:
    strSQL = "SELECT * FROM QEMAll WHERE ClassID = " & strCurClassID
    
    
    Set rsEMAs = DBEngine(0)(0).OpenRecordset(strSQL)
    I'm still puzzled why the approach posted in #16 didn't work, but all I got returned in the rs was a count of "1".

    Thanks guys for putting up with this "Old Geezer", I truly appreciate your efforts.
    Bill

  10. #25
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Well I have never used - or _, but - does not work for me. I get that From error message. However _ works fine for me.?

    I know a hypen is frowned upon in DB development, and an underscore used instead. Perhaps that is why.?

    Oh well, something new learnt today.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #26
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    If you think about it Access is probably going to evaluate QEMA-ALL as

    QEMA Minus ALL

    Which may well cause an issue or two...
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  12. #27
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Unless you movelast, you will always get that, unless the recordset is empty?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  13. #28
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Quote Originally Posted by Minty View Post
    If you think about it Access is probably going to evaluate QEMA-ALL as

    QEMA Minus ALL

    Which may well cause an issue or two...
    Experienced that a long time ago. Must be [QEMA-ALL] not QEMA-ALL. QEMA_ALL ok too - probably better, right up there with Camel Case ( QEMAall or similar).
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #29
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Now that all the total conditions have been addressed, here's what the code looks like:
    Code:
    Dim I As Integer
    Dim strSQL As String
    Dim strAppend As String
    
    
    strSQL = "SELECT * FROM QEMAll"                           'Start by assuming everyone
    strAppend = ""
        
    If QName = "QSelected" Then _
        strAppend = " WHERE SelAro <> "" """                  'Individuals selected (blanks are explicit)
    
    
    If (strCurClassID >= 2 And strCurClassID <= 5) Then       'Filter class? strCurClassID (form global)
        If strAppend = "" Then
            strAppend = " WHERE ClassID = " & strCurClassID   'Yes, the whole class
        Else
            strAppend = " WHERE ((SelAro <> "" "") AND (ClassID = " & strCurClassID & "))"   'Selects within class
        End If
    End If
    
    
    If strAppend <> "" Then strSQL = strSQL & strAppend       'Some filtering, compound or otherwise
        
    Set rsEMAs = DBEngine(0)(0).OpenRecordset(strSQL)         'rsEMAs Recordset global to form module
    
    
    intEMACount = rsEMAs.RecordCount

  15. #30
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Still not going to get correct record count?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 2 of 3 FirstFirst 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Filtering - Table or recordset
    By diegomarino in forum Access
    Replies: 2
    Last Post: 09-10-2020, 11:02 AM
  2. MS Access Filtering Query Recordset
    By Mick99 in forum Access
    Replies: 5
    Last Post: 06-14-2017, 03:25 PM
  3. Replies: 6
    Last Post: 12-03-2013, 11:14 PM
  4. Recordset Filtering
    By George in forum Access
    Replies: 9
    Last Post: 05-27-2012, 10:10 AM
  5. create table filtering a recordset
    By JJCHCK in forum Programming
    Replies: 5
    Last Post: 09-27-2011, 01: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