The following is ANSI-92 Query Mode SQL syntax (http://
office.microsoft.com/en-gb/access/HP030704831033.aspx):
[snipped]
I am having serious trouble getting this to work in Access 2003. I
cant figure out where to begin.
Try running this VBA. It should create a new mdb file in your Temp
folder, with all the tables and data:
Sub Salesmen()
' Kill Environ$("temp") & "\DropMe.mdb"
Dim cat
Set cat = CreateObject("ADOX.Catalog")
With cat
..Create _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & _
Environ$("temp") & "\DropMe.mdb"
With .ActiveConnection
..Execute _
"CREATE TABLE Calendar ( dt DATETIME NOT" & _
" NULL CONSTRAINT pk__Calendar PRIMARY KEY," & _
" Y INTEGER, M INTEGER);"
..Execute _
"INSERT INTO Calendar (dt) VALUES" & _
" (#1990-01-01 00:00:00#);"
Dim sql
sql = _
"INSERT INTO Calendar (dt) SELECT CDATE(Units.nbr" & _
" + Tens.nbr + Hundreds.nbr + Thousands.nbr" & _
" + TenThousands.nbr) AS dt FROM (SELECT" & _
" nbr FROM (SELECT 0 AS nbr FROM Calendar" & _
" UNION ALL SELECT 1 FROM Calendar UNION" & _
" ALL SELECT 2 FROM Calendar UNION ALL SELECT" & _
" 3 FROM Calendar UNION ALL SELECT 4 FROM" & _
" Calendar UNION ALL SELECT 5 FROM Calendar" & _
" UNION ALL SELECT 6 FROM Calendar UNION" & _
" ALL SELECT 7 FROM Calendar UNION ALL SELECT" & _
" 8 FROM Calendar UNION ALL SELECT 9 FROM" & _
" Calendar) AS Digits) AS Units, (SELECT" & _
" nbr * 10 AS nbr FROM (SELECT 0 AS nbr" & _
" FROM Calendar UNION ALL SELECT 1 FROM Calendar" & _
" UNION ALL SELECT 2 FROM Calendar UNION" & _
" ALL SELECT 3 FROM Calendar UNION ALL SELECT" & _
" 4 FROM Calendar UNION ALL SELECT 5 FROM" & _
" Calendar UNION ALL SELECT 6 FROM Calendar" & _
" UNION ALL SELECT 7 FROM Calendar UNION" & _
" ALL SELECT 8 FROM Calendar UNION ALL SELECT" & _
" 9 FROM Calendar) AS Digits) AS Tens," & _
" (SELECT nbr * 100 AS nbr FROM (SELECT" & _
" 0 AS nbr FROM Calendar UNION ALL SELECT"
sql = sql & _
" 1 FROM Calendar UNION ALL SELECT 2 FROM" & _
" Calendar UNION ALL SELECT 3 FROM Calendar" & _
" UNION ALL SELECT 4 FROM Calendar UNION" & _
" ALL SELECT 5 FROM Calendar UNION ALL SELECT" & _
" 6 FROM Calendar UNION ALL SELECT 7 FROM" & _
" Calendar UNION ALL SELECT 8 FROM Calendar" & _
" UNION ALL SELECT 9 FROM Calendar) AS Digits" & _
") AS Hundreds, (SELECT nbr * 1000 AS nbr" & _
" FROM (SELECT 0 AS nbr FROM Calendar UNION" & _
" ALL SELECT 1 FROM Calendar UNION ALL SELECT" & _
" 2 FROM Calendar UNION ALL SELECT 3 FROM" & _
" Calendar UNION ALL SELECT 4 FROM Calendar" & _
" UNION ALL SELECT 5 FROM Calendar UNION" & _
" ALL SELECT 6 FROM Calendar UNION ALL SELECT" & _
" 7 FROM Calendar UNION ALL SELECT 8 FROM" & _
" Calendar UNION ALL SELECT 9 FROM Calendar" & _
") AS Digits) AS Thousands, (SELECT nbr" & _
" * 10000 AS nbr FROM (SELECT 0 AS nbr FROM" & _
" Calendar UNION ALL SELECT 1 FROM Calendar" & _
" UNION ALL SELECT 2 FROM Calendar UNION" & _
" ALL SELECT 3 FROM Calendar UNION ALL SELECT" & _
" 4 FROM Calendar UNION ALL SELECT 5 FROM" & _
" Calendar UNION ALL SELECT 6 FROM Calendar" & _
" UNION ALL SELECT 7 FROM Calendar UNION"
sql = sql & _
" ALL SELECT 8 FROM Calendar UNION ALL SELECT" & _
" 9 FROM Calendar) AS Digits) AS TenThousands" & _
" WHERE Units.nbr + Tens.nbr + Hundreds.nbr" & _
" + Thousands.nbr + TenThousands.nbr BETWEEN" & _
" CLNG(DATESERIAL(1990, 1, 2)) AND" & _
" CLNG(DATESERIAL(2020, 12, 31));"
..Execute sql
sql = _
"UPDATE Calendar SET Y = DATEPART('YYYY', dt)," & _
" M = DATEPART('M', dt);"
..Execute sql
sql = _
"CREATE TABLE SalesTransactionLog" & vbCr & "(" & vbCr & "" & _
" salesman_number INTEGER NOT" & _
" NULL," & vbCr & " effective_date DATETIME" & _
" NOT NULL," & vbCr & " CHECK" & vbCr & " (" & vbCr & " " & _
" DATEPART('H', effective_date)" & _
" = 0" & vbCr & " AND DATEPART('N', effective_date)" & _
" = 0" & vbCr & " AND DATEPART('S', effective_date)" & _
" = 0" & vbCr & " )," & vbCr & " shares_amount INTEGER" & _
" NOT NULL," & vbCr & " CHECK" & vbCr & " (" & vbCr & " " & _
" shares_amount > 0" & vbCr & " )" & vbCr & ")" & vbCr & ";"
..Execute sql
sql = _
"INSERT INTO SalesTransactionLog" & _
" (salesman_number, effective_date," & vbCr & "shares_amount)" & vbCr
& "" & _
" VALUES (1, #2007-01-01 00:00:00#," & _
" 11)" & vbCr & ";"
..Execute sql
sql = _
"INSERT INTO SalesTransactionLog" & _
" (salesman_number, effective_date," & vbCr & "shares_amount)" & vbCr
& "" & _
" VALUES (1, #2007-01-21 00:00:00#," & _
" 22)" & vbCr & ";"
..Execute sql
sql = _
"INSERT INTO SalesTransactionLog" & _
" (salesman_number, effective_date," & vbCr & "shares_amount)" & vbCr
& "" & _
" VALUES (1, #2007-03-03 00:00:00#," & _
" 33)" & vbCr & ";"
..Execute sql
sql = _
"INSERT INTO SalesTransactionLog" & _
" (salesman_number, effective_date," & vbCr & "shares_amount)" & vbCr
& "" & _
" VALUES (1, #2007-03-23 00:00:00#," & _
" 44)" & vbCr & ";"
..Execute sql
sql = _
"INSERT INTO SalesTransactionLog" & _
" (salesman_number, effective_date," & vbCr & "shares_amount)" & vbCr
& "" & _
" VALUES (1, #2007-05-05 00:00:00#," & _
" 55)" & vbCr & ";"
..Execute sql
sql = _
"INSERT INTO SalesTransactionLog" & _
" (salesman_number, effective_date," & vbCr & "shares_amount)" & vbCr
& "" & _
" VALUES (1, #2007-05-25 00:00:00#," & _
" 66)" & vbCr & ";"
..Execute sql
sql = _
"INSERT INTO SalesTransactionLog" & _
" (salesman_number, effective_date," & vbCr & "shares_amount)" & vbCr
& "" & _
" VALUES (2, #2007-03-01 00:00:00#," & _
" 1)" & vbCr & ";"
..Execute sql
sql = _
"INSERT INTO SalesTransactionLog" & _
" (salesman_number, effective_date," & vbCr & "shares_amount)" & vbCr
& "" & _
" VALUES (2, #2007-03-31 00:00:00#," & _
" 2)" & vbCr & ";"
..Execute sql
sql = _
"INSERT INTO SalesTransactionLog" & _
" (salesman_number, effective_date," & vbCr & "shares_amount)" & vbCr
& "" & _
" VALUES (2, #2007-04-01 00:00:00#," & _
" 3)" & vbCr & ";"
..Execute sql
sql = _
"INSERT INTO SalesTransactionLog" & _
" (salesman_number, effective_date," & vbCr & "shares_amount)" & vbCr
& "" & _
" VALUES (2, #2007-04-30 00:00:00#," & _
" 4)" & vbCr & ";"
..Execute sql
sql = _
"CREATE VIEW SalesByMonth (" & vbCr & " " & _
" Y, M, salesman_number, shares_amount_this_month" & vbCr & ")" & vbCr
& "AS" & vbCr & "SELECT" & _
" A1.Y, A1.M, A1.salesman_number," & _
" M1.shares_amount_this_month" & vbCr & "FROM" & vbCr & "(" & vbCr &
"" & _
" SELECT S1.salesman_number," & _
" C1.Y, C1.M" & vbCr & " FROM SalesTransactionLog" & _
" AS S1, Calendar AS C1" & vbCr & " WHERE" & _
" C1.dt BETWEEN" & vbCr & " (" & vbCr & " SELECT" & _
" MIN(S2.effective_date)" & vbCr & " " & _
" FROM SalesTransactionLog AS S2" & vbCr & "" & _
" ) AND" & vbCr & " (" & vbCr & " SELECT
MAX(S2.effective_date)" & vbCr & "" & _
" FROM SalesTransactionLog" & _
" AS S2" & vbCr & " )" & vbCr & " GROUP BY S1.salesman_number," &
_
" C1.Y, C1.M" & vbCr & ") AS A1" & vbCr & "LEFT JOIN" & vbCr & "(" &
vbCr & "" & _
" SELECT S1.salesman_number," & _
" C1.Y, C1.M," & vbCr & " IIF(SUM(S1.shares_amount)" & _
" IS NULL, 0, SUM(S1.shares_amount))" & _
" AS" & vbCr & "shares_amount_this_month" & vbCr & "" & _
" FROM SalesTransactionLog AS" & _
" S1" & vbCr & " INNER JOIN Calendar AS"
sql = sql & _
" C1" & vbCr & " ON S1.effective_date =" & _
" C1.dt" & vbCr & " GROUP BY S1.salesman_number," & _
" C1.Y, C1.M" & vbCr & ") AS M1" & vbCr & "ON A1.salesman_number" & _
" = M1.salesman_number" & vbCr & "AND A1.Y" & _
" = M1.Y" & vbCr & "AND A1.M = M1.M" & vbCr & "GROUP" & _
" BY A1.Y, A1.M, A1.salesman_number," & _
" M1.shares_amount_this_month;"
..Execute sql
sql = _
"SELECT DISTINCT ' Y', ' M', ' salesman_number','
shares_amount_this_month'" & _
" FROM SalesByMonth UNION ALL" & _
" SELECT T2.Y, T2.M, T2.salesman_number," & vbCr &
"SUM(T1.shares_amount_this_month)" & _
" AS" & vbCr & "shares_amount_cumulative_to_this_month" & vbCr &
"FROM" & _
" SalesByMonth AS T1" & vbCr & "INNER JOIN" & _
" SalesByMonth AS T2" & vbCr & "ON T1.salesman_number" & _
" = T2.salesman_number" & vbCr & "AND T1.Y" & _
" <= T2.Y" & vbCr & "AND T1.M <= T2.M" & vbCr & "GROUP" & _
" BY T2.Y, T2.M, T2.salesman_number" & vbCr & "ORDER" & _
" BY 3, 1, 2;"
Dim rs
Set rs = .Execute(sql)
MsgBox rs.GetString
End With
Set .ActiveConnection = Nothing
End With
End Sub