Results 1 to 9 of 9
  1. #1
    jbuckner72 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    12

    Code instead of queries all over the place?


    Generic question about capabilities of MS Access. I am currently working on a mini project to take a dataset and do a bunch of grouping, various distinct data sets, filtering and all kinds of stuff. I am going about my standard method of writing queries, then queries on top of those queries, etc. All in all I think this will result in 15-20 queries all mashed together with a union query at the end, then a manual export to tab delimited. With MS Access, is there a better way to skin this cat? Is it possible to create a module of code that allows you to do this all at once. Where the code contains the query information, but stores as variables that are referenced later in code, etc?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    Some people opinion that if you have to use Union queries, then your DB structure is incorrect?
    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

  3. #3
    jbuckner72 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    12
    I don't necessarily disagree with that statement. I am probably not using the best tool for the job necessarily. Mostly using MS Access to transform data from external systems into something different. Different format, different fields, include calculations, etc. A more appropriate tool would be something like Python, Perl, even Powershell. I am just too lazy to learn those and I have decent skills with SQL so that is where I go usually.

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    jbuckner,

    It seems you are asking HOW to do something in Access. It would facilitate things if you told us WHAT you want to achieve in simple terms or even a couple of examples. It's difficult -near impossible - to advise on How when the What has not been understood.
    I'm sure you'll get focused responses when the requirement is clear.

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I'd say the short answer to the question is yes, it can all be done in code.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    I agree with micron. As a friend of mine use to say --It's SMOP. Simple matter of programming.

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Here's a sub that will accept an action SQL string, create a query (without a name) and execute it. It might accommodate your needs. I realize you could just execute the query string directly, but this is cooler.
    Example use:

    call MakeQuery("Update tblMembers Set lastname ='Smith' Where lastname = 'Jones'")

    Code:
    Public Sub MakeQuery(strPassedSQL As String)
        Dim db As DAO.Database: Set db = CurrentDb
        Dim qthisQuery As DAO.QueryDef
        Set qthisQuery = db.CreateQueryDef("", strPassedSQL)
        qthisQuery.Execute dbFailOnError
        Set qthisQuery = Nothing: Set db = Nothing
    End Sub
    Last edited by davegri; 05-16-2021 at 06:01 PM. Reason: ¯\_(ツ)_/¯

  8. #8
    jbuckner72 is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2019
    Posts
    12
    Thanks all. Sounds like the general capabilities exist enough for me to go exploring.

    For the note on providing specific details, I intentionally wanted to ask a generic question, but in general, I find myself following this scenario below quite often. In order to get from that source table to the desired output, it takes me 3-4 queries depending on a few factors. That is fine. It is just that I write those queries in a code editor that makes it easy to read, then copy/paste into Access. Maintaining all that in one set of code would be far more efficient from a maintenance perspective.

    Table: Customer_Visits
    Name City Cust_ID
    John Doe New York 1
    Jane Doe Atlanta 2
    John Smith New York 3
    John Doe New York 1
    Daffy Duck Atlanta 4

    Desired Output
    City Visits Unique Customers
    New York 3 2
    Atlanta 2 2

  9. #9
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    It is just that I write those queries in a code editor that makes it easy to read, then copy/paste into Access. Maintaining all that in one set of code would be far more efficient from a maintenance perspective.
    You could keep your SQL strings in a table, then have a form for maintenance. The form could also have a button for running the selected query.

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

Similar Threads

  1. How to get a report with code macro's queries
    By CarlotaVina in forum Access
    Replies: 1
    Last Post: 09-23-2017, 03:43 PM
  2. [code]about queries[/code]
    By dayavathi in forum Access
    Replies: 3
    Last Post: 11-11-2015, 10:13 AM
  3. Help me understand queries in my code
    By Datech in forum Queries
    Replies: 0
    Last Post: 06-15-2012, 08:32 PM
  4. Replies: 21
    Last Post: 02-14-2011, 02:51 PM
  5. Replies: 3
    Last Post: 01-02-2011, 07:17 PM

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