Results 1 to 6 of 6
  1. #1
    Hesco is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    4

    SELECT Query using VBA to filter

    Question: I am trying to write a piece of VBA Code that will actuate when I open my report titled ‘Workers’. The Code is supposed to select three employees randomly and then filter (exclude) one of the employees based on their ID number.

    I was able to create a query, switch to SQL view and use that statement as the model for the code below. I understand that I can make a query in Access but I am trying to reduce the query clutter in my database while increasing my skills and knowledge of VBA/Code. I had success making a DELETE query and think that it must be possible to make something like this but I have a limited understanding.

    I have read in the forum that you cannot use a SELECT query in VBA but there has to be some work around for the desired results.

    Private Sub Report_Open(Cancel As Integer)
    Dim strQuery As String
    strQuery = "SELECT TOP 3 Workers.ID, Workers.FullName, Rnd([ID]) AS Expr1 FROM Workers WHERE (((Workers.ID)<>9)) ORDER BY Rnd([ID]);"
    DoCmd.RunSQL strQuery


    End Sub

    Any help with this would be great. Please see attached file.
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Can't 'run' a SELECT query, only action queries like DELETE, UPDATE, INSERT can be run.

    Open a SELECT query in VBA as a recordset.

    Or use the constructed SELECT statement to set the RecordSource property of form or report.
    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
    Hesco is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    4
    How do I use the constructed SELECT statement to set the RecordSource property? I like doing research, if you can point me in a direction I will be happy to follow. :-)

  4. #4
    Hesco is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    4
    DUDE!!!! June7!!! I get it now!!! I just did a keyword search on your reply!!!!! I found this link and it filled in the blanks!!! Its not VBA but it works for me!!

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Yes, using a query as the RecordSource of form or report is basic Access functionality, no need for VBA although VBA can be used to change the RecordSource property. However, I find that there is seldom a reason to do that.
    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.

  6. #6
    Hesco is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    4
    Thanks! This forum is GREAT!

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

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2013, 03:36 PM
  2. Replies: 2
    Last Post: 10-25-2012, 02:53 AM
  3. Form ComboBox Filter - Select ALL records
    By jhrBanker in forum Forms
    Replies: 2
    Last Post: 08-02-2012, 08:57 AM
  4. Replies: 1
    Last Post: 10-08-2011, 11:15 PM
  5. Replies: 1
    Last Post: 03-01-2009, 09:53 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