Access query design help (1 Viewer)

mitchk

New member
Local time
Today, 18:40
Joined
Apr 11, 2024
Messages
2
Hi Guys,

New here hoping for somebody to help me understand how to create this query.

I have a database of customer records which span years 2000 to current and are trying to create a query to show the customer (row) by the amount of units received each year (column).

Problem is I want the query to return 0 for the years that they didn't deal with us or stopped dealing with us :)

how would I do this ? closest I have so far is adding an IN Clause to the SQL like below but I don't know how to make it dynamic so it adds a new column each year?

TRANSFORM Count(tbl_GoodsInAndStockData.JobNumber) AS CountOfJobNumber
SELECT tbl_GoodsInAndStockData.CustomerName
FROM tbl_GoodsInAndStockData
GROUP BY tbl_GoodsInAndStockData.CustomerName
PIVOT Year([DateIn])
IN (2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024);
 

GPGeorge

Grover Park George
Local time
Today, 10:40
Joined
Nov 25, 2004
Messages
1,878
To make it dynamic, you could try using a talley table of years. A talley table is a one field table of the values you want to include. In this case that would be years from 2000 to 2024. In future, all you need to do is add the next year, e.g. 2025.

I don't have your table and sample data to validate on, so this is a suggestion to try.

Include the year talley table in the query with a left outer join to the [DateIn] field in the existing table, so that one record is returned for each year, whether it has a corresponding record or not. Pivot on that field.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:40
Joined
Oct 29, 2018
Messages
21,479
Hi. Welcome to AWF!

You can either have a separate table of all the years that you update manually or use a query to list all the years from your original table assuming there are no gaps and then use it to join with your customer table.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:40
Joined
May 7, 2009
Messages
19,248
column count can be of max 255 only.
you need VBA to update your Crosstab query with the latest Year to add.

make to copies of this query (name the queries, qryOrig and OriginalNameOfYourQuery):
Code:
TRANSFORM Count(tbl_GoodsInAndStockData.JobNumber) AS CountOfJobNumber
SELECT tbl_GoodsInAndStockData.CustomerName
FROM tbl_GoodsInAndStockData
GROUP BY tbl_GoodsInAndStockData.CustomerName
PIVOT Year([DateIn])

next create a sub/function on Module to update the years on your query:

Code:
Public Sub subUpdateMyQuery()

    Dim db As DAO.Database
    Dim qd As DAO.QueryDef
    Dim sql As String
    Dim sYr As String
    Dim i As Integer, j As Integer, k As Integer
    
    j = 2000
    k = Year(Date)
    
    'adjust the beginning year
    If k - j > 254 Then
        j = k - 254
    End If
    
    For i = j To k
        sYr = sYr & "," & i
    Next
    
    sYr = Mid$(sYr, 2)
    
    
    Set db = CurrentDb
    sql = Replace$(db.QueryDefs("qryOrig").sql, ";", "")
    Set qd = db.QueryDefs("OriginalNameOfYourQuery")
    qd.sql = sql & " IN (" & sYr & ");"
    Set qd = Nothing
    
End Sub
 
Last edited:

nector

Member
Local time
Today, 20:40
Joined
Jan 21, 2020
Messages
368
Just filter a query either <> 0 in the query grid, I always do it that way
 

mitchk

New member
Local time
Today, 18:40
Joined
Apr 11, 2024
Messages
2
Morning Guys,

thankyou so much for your help - I've been able to use both the talley table and vba solutions for my query
 

Users who are viewing this thread

Top Bottom