Microsoft Access: SQL Programming Code Examples
Option Compare Database
'
Purpose: To pass table name 'tblStockingList' to the function. If 'tblStockingList'
doesn't exist, it creates the table and columns
cn.Close
'a Purpose: Automating multiple tables and column creation instantaneously. Sub CreateMultipleTables(Table As String, F1 As String,
F2 As String, F3 As String, F4 As String) & "[" & F2 & "] Text (20), " & "[" & F3 & "] DateTime, " & "[" & F4 & "] Integer)" cn.Execute strSQL cn.Close Set cn = Nothing End Sub
Sub create_multple_tables() ' Purpose: To add and delete fields using SQL Sub AddDeleteFields()
Set cn = CurrentProject.Connection ' Purpose: To create a View with an Inner Join query ' This example create an Inner Join query with another table.
It pivots 3 rows heading: 'PartNumber' from main table, CustomerName' Sub CreateView() Dim cn As ADODB.Connection Dim strSQL As String Set cn = CurrentProject.Connection strSql = "CREATE VIEW qry_CustomerSales AS " strSql = strSql & "SELECT m.PartNumber, c.CustomerName, " strSql = strSql & "Format([m.QtyOnHand]*[c.ListPrice],""$#,##0.00"") AS Q4_realizableAmount " strSql = strSql & "FROM MaterialList AS m INNER JOIN CustomerList AS c ON " strSql = strSql & "m.PartNumber = c.PartNumber " strSql = strSql & "WHERE (([m.QtyOnHand]>1));" cn.Execute strSQL cn.Close Set cn = Nothing End Sub ' Purpose: Using Inner Join to create a nested query between 3 tables, populating queried data to a created table name. ' The main query will only get the data retrieved base on 3 conditions,
that's - if the delivery dates from the 2nd subselect was in Sub NestedQuery() strSql = "SELECT MaterialList.PartNumber, DatePart('yyyy',[DeliveryDate]) AS YrDel, " & _ "Format(CLng(([DeliveredQty]*[ListPrice])),""$#,##0.00"") AS [Sales$], " & _ "Format(Nz(([QtyOnHand]),0)*Nz(([StandardCost]),0),""Currency"") AS [Inventory$], " & _ "CustomerCode.CustomerID, CustomerList.CustomerName " & _ "INTO tbl_StockOnHandDollars " & _ "FROM CustomerCode INNER JOIN (MaterialList INNER JOIN CustomerList " & _ "ON (MaterialList.PartNumber = CustomerList.PartNumber) AND (MaterialList.ID = CustomerList.ID)) " & _ "ON CustomerCode.CustomerName = CustomerList.CustomerName " & _ "WHERE (((DatePart('yyyy',[DeliveryDate]))=2006) " & _ "AND ((Format(CLng(([DeliveredQty]*[ListPrice])),""$#,##0.00"")) >1000 " & _ "OR (Format(CLng(([DeliveredQty]*[ListPrice])),""$#,##0.00"")) Is Null) " & _ "AND ((Format(Nz(([QtyOnHand]),0)*Nz(([StandardCost]),0),""Currency""))>300)) " & _ "ORDER BY MaterialList.PartNumber DESC;" DoCmd.RunSQL strSql MsgBox "query has updated table 'tbl_StockOnHandDollars'" End Sub ' Purpose: To create a Crosstab Query with an Outer Left Join query.
' This query creates a View table with
two row heading fields - 'PartNumber' from
main table, and 'CustomerNames' from the other. SELECT m.PartNumber, c.CustomerName FROM MaterialList AS m LEFT JOIN CustomerList AS c ON (m.ID = c.ID) AND (m.PartNumber = c.PartNumber) WHERE (((DatePart('yyyy',[DeliveryDate]))>2003)) GROUP BY m.PartNumber, c.CustomerName ORDER BY m.PartNumber DESC PIVOT DatePart('yyyy',[DeliveryDate]);
' Purpose: to update records through a subquery.
' After you had run the SQL query, check on the SafetyStock field in the main table, you will find that a part for Sony had been updated in its safety stock level from 15 to 17. Download this database. Sub subquery_update() DoCmd.RunSQL ("UPDATE MaterialList SET [SafetyStock] = ROUND(([SafetyStock]*1.15),0) " & _ "WHERE PartNumber IN (SELECT PartNumber FROM CustomerList " & _ "WHERE CustomerList.CustomerName = 'Sony' AND CustomerList.DeliveryDate >#12/15/2005#);") Msgbox "the safety stock level has the quantities already updated." End Sub ' Purpose: using a subquery to remove duplicate records within a table. 'Duplicates' here mean those part numbers and standard costs both have the same values repeating more than once. Same null values repeating are also duplicates. For the duplicates, the records that have the lowest primary key value (field ID) will be retained. You need to provide table aliases in order to join a table to itself.Download this database. Sub s ubquery_deleteDuplicates()DoCmd.RunSQL (" DELETE FROM Table1 WHERE ID <>(SELECT Min(ID) AS MinOFiD " & _"FROM Table1 AS dupTable1 WHERE (dupTable1.PartNumber = Table1.PartNumber) " & _ "OR (dupTable1.PartNumber Is Null AND Table1.PartNumber Is Null) " & _ "AND (dupTable1.StandardCost = Table1.StandardCost) " & _ "OR (dupTable1.StandardCost Is Null AND Table1.StandardCost Is Null));") MsgBox "done! less than 24 records now." End Sub
|
|