Code:
Option Compare Database
Option Explicit
Private Sub clearform_Click()
Me.cupcakeselect1 = Null
Me.cupcakeselect2 = Null
Me.cupcakeselect3 = Null
Me.cupcakeselect4 = Null
Me.cupcakeselect5 = Null
Me.cupcakeselect6 = Null
Me.cupcakeselect7 = Null
Me.cupcakeselect8 = Null
Me.cupcakeselect9 = Null
Me.cupcakeselect10 = Null
End Sub
Private Sub runquery_Click()
Dim sSQL As String
sSQL = "delete * FROM pivottablesource"
CurrentDb.Execute sSQL, dbFailOnError
If Not IsNull(cupcakeselect1) Then
Call subUpdate(1)
End If
If Not IsNull(cupcakeselect2) Then
Call subUpdate(2)
End If
If Not IsNull(cupcakeselect3) Then
Call subUpdate(3)
End If
If Not IsNull(cupcakeselect4) Then
Call subUpdate(4)
End If
If Not IsNull(cupcakeselect5) Then
Call subUpdate(5)
End If
If Not IsNull(cupcakeselect6) Then
Call subUpdate(6)
End If
If Not IsNull(cupcakeselect7) Then
Call subUpdate(7)
End If
If Not IsNull(cupcakeselect8) Then
Call subUpdate(8)
End If
If Not IsNull(cupcakeselect9) Then
Call subUpdate(9)
End If
If Not IsNull(cupcakeselect10) Then
Call subUpdate(10)
End If
End Sub
Sub subUpdate(arg As Long)
Dim sSQL As String
Select Case arg
Case 1
sSQL = "INSERT INTO PivotTableSource ([Cupcake Name], [# Cupcake Made], [Ingredient Name], Qty, Unit, Component) " _
& "SELECT Cupcake.[cupcake name], Cupcake.[# Cupcake Made], Ingredient.[Ingredient Name], ([Cupcake_Ingredient].[Qty]* " & multiply1 & ") as Qty, " _
& "Measurements.Unit, Component.Component " _
& "FROM Measurements INNER JOIN (Ingredient INNER JOIN (Cupcake INNER JOIN (Component INNER JOIN " _
& "Cupcake_Ingredient ON Component.[Component ID] = Cupcake_Ingredient.[Component ID]) ON Cupcake.[Cupcake ID] = Cupcake_Ingredient.[Cupcake ID]) " _
& "ON Ingredient.[Ingredient ID] = Cupcake_Ingredient.[Ingredient ID]) ON Measurements.[Unit ID] = Cupcake_Ingredient.[Units ID] " _
& "WHERE Cupcake.[cupcake name]= '" & [cupcakeselect1] & "';"
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
Case 2
sSQL = "INSERT INTO PivotTableSource ([Cupcake Name], [# Cupcake Made], [Ingredient Name], Qty, Unit, Component) " _
& "SELECT Cupcake.[cupcake name], Cupcake.[# Cupcake Made], Ingredient.[Ingredient Name], ([Cupcake_Ingredient].[Qty]* " & multiply2 & ") as Qty, " _
& "Measurements.Unit, Component.Component " _
& "FROM Measurements INNER JOIN (Ingredient INNER JOIN (Cupcake INNER JOIN (Component INNER JOIN " _
& "Cupcake_Ingredient ON Component.[Component ID] = Cupcake_Ingredient.[Component ID]) ON Cupcake.[Cupcake ID] = Cupcake_Ingredient.[Cupcake ID]) " _
& "ON Ingredient.[Ingredient ID] = Cupcake_Ingredient.[Ingredient ID]) ON Measurements.[Unit ID] = Cupcake_Ingredient.[Units ID] " _
& "WHERE Cupcake.[cupcake name]= '" & [cupcakeselect2] & "';"
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
Case 3
sSQL = "INSERT INTO PivotTableSource ([Cupcake Name], [# Cupcake Made], [Ingredient Name], Qty, Unit, Component) " _
& "SELECT Cupcake.[cupcake name], Cupcake.[# Cupcake Made], Ingredient.[Ingredient Name], ([Cupcake_Ingredient].[Qty]* " & multiply3 & ") as Qty, " _
& "Measurements.Unit, Component.Component " _
& "FROM Measurements INNER JOIN (Ingredient INNER JOIN (Cupcake INNER JOIN (Component INNER JOIN " _
& "Cupcake_Ingredient ON Component.[Component ID] = Cupcake_Ingredient.[Component ID]) ON Cupcake.[Cupcake ID] = Cupcake_Ingredient.[Cupcake ID]) " _
& "ON Ingredient.[Ingredient ID] = Cupcake_Ingredient.[Ingredient ID]) ON Measurements.[Unit ID] = Cupcake_Ingredient.[Units ID] " _
& "WHERE Cupcake.[cupcake name]= '" & [cupcakeselect3] & "';"
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
Case 4
sSQL = "INSERT INTO PivotTableSource ([Cupcake Name], [# Cupcake Made], [Ingredient Name], Qty, Unit, Component) " _
& "SELECT Cupcake.[cupcake name], Cupcake.[# Cupcake Made], Ingredient.[Ingredient Name], ([Cupcake_Ingredient].[Qty]* " & multiply4 & ") as Qty, " _
& "Measurements.Unit, Component.Component " _
& "FROM Measurements INNER JOIN (Ingredient INNER JOIN (Cupcake INNER JOIN (Component INNER JOIN " _
& "Cupcake_Ingredient ON Component.[Component ID] = Cupcake_Ingredient.[Component ID]) ON Cupcake.[Cupcake ID] = Cupcake_Ingredient.[Cupcake ID]) " _
& "ON Ingredient.[Ingredient ID] = Cupcake_Ingredient.[Ingredient ID]) ON Measurements.[Unit ID] = Cupcake_Ingredient.[Units ID] " _
& "WHERE Cupcake.[cupcake name]= '" & [cupcakeselect4] & "';"
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
Case 5
sSQL = "INSERT INTO PivotTableSource ([Cupcake Name], [# Cupcake Made], [Ingredient Name], Qty, Unit, Component) " _
& "SELECT Cupcake.[cupcake name], Cupcake.[# Cupcake Made], Ingredient.[Ingredient Name], ([Cupcake_Ingredient].[Qty]* " & multiply5 & ") as Qty, " _
& "Measurements.Unit, Component.Component " _
& "FROM Measurements INNER JOIN (Ingredient INNER JOIN (Cupcake INNER JOIN (Component INNER JOIN " _
& "Cupcake_Ingredient ON Component.[Component ID] = Cupcake_Ingredient.[Component ID]) ON Cupcake.[Cupcake ID] = Cupcake_Ingredient.[Cupcake ID]) " _
& "ON Ingredient.[Ingredient ID] = Cupcake_Ingredient.[Ingredient ID]) ON Measurements.[Unit ID] = Cupcake_Ingredient.[Units ID] " _
& "WHERE Cupcake.[cupcake name]= '" & [cupcakeselect5] & "';"
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
Case 6
sSQL = "INSERT INTO PivotTableSource ([Cupcake Name], [# Cupcake Made], [Ingredient Name], Qty, Unit, Component) " _
& "SELECT Cupcake.[cupcake name], Cupcake.[# Cupcake Made], Ingredient.[Ingredient Name], ([Cupcake_Ingredient].[Qty]* " & multiply6 & ") as Qty, " _
& "Measurements.Unit, Component.Component " _
& "FROM Measurements INNER JOIN (Ingredient INNER JOIN (Cupcake INNER JOIN (Component INNER JOIN " _
& "Cupcake_Ingredient ON Component.[Component ID] = Cupcake_Ingredient.[Component ID]) ON Cupcake.[Cupcake ID] = Cupcake_Ingredient.[Cupcake ID]) " _
& "ON Ingredient.[Ingredient ID] = Cupcake_Ingredient.[Ingredient ID]) ON Measurements.[Unit ID] = Cupcake_Ingredient.[Units ID] " _
& "WHERE Cupcake.[cupcake name]= '" & [cupcakeselect6] & "';"
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
Case 7
sSQL = "INSERT INTO PivotTableSource ([Cupcake Name], [# Cupcake Made], [Ingredient Name], Qty, Unit, Component) " _
& "SELECT Cupcake.[cupcake name], Cupcake.[# Cupcake Made], Ingredient.[Ingredient Name], ([Cupcake_Ingredient].[Qty]* " & multiply7 & ") as Qty, " _
& "Measurements.Unit, Component.Component " _
& "FROM Measurements INNER JOIN (Ingredient INNER JOIN (Cupcake INNER JOIN (Component INNER JOIN " _
& "Cupcake_Ingredient ON Component.[Component ID] = Cupcake_Ingredient.[Component ID]) ON Cupcake.[Cupcake ID] = Cupcake_Ingredient.[Cupcake ID]) " _
& "ON Ingredient.[Ingredient ID] = Cupcake_Ingredient.[Ingredient ID]) ON Measurements.[Unit ID] = Cupcake_Ingredient.[Units ID] " _
& "WHERE Cupcake.[cupcake name]= '" & [cupcakeselect7] & "';"
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
Case 8
sSQL = "INSERT INTO PivotTableSource ([Cupcake Name], [# Cupcake Made], [Ingredient Name], Qty, Unit, Component) " _
& "SELECT Cupcake.[cupcake name], Cupcake.[# Cupcake Made], Ingredient.[Ingredient Name], ([Cupcake_Ingredient].[Qty]* " & multiply8 & ") as Qty, " _
& "Measurements.Unit, Component.Component " _
& "FROM Measurements INNER JOIN (Ingredient INNER JOIN (Cupcake INNER JOIN (Component INNER JOIN " _
& "Cupcake_Ingredient ON Component.[Component ID] = Cupcake_Ingredient.[Component ID]) ON Cupcake.[Cupcake ID] = Cupcake_Ingredient.[Cupcake ID]) " _
& "ON Ingredient.[Ingredient ID] = Cupcake_Ingredient.[Ingredient ID]) ON Measurements.[Unit ID] = Cupcake_Ingredient.[Units ID] " _
& "WHERE Cupcake.[cupcake name]= '" & [cupcakeselect8] & "';"
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
Case 9
sSQL = "INSERT INTO PivotTableSource ([Cupcake Name], [# Cupcake Made], [Ingredient Name], Qty, Unit, Component) " _
& "SELECT Cupcake.[cupcake name], Cupcake.[# Cupcake Made], Ingredient.[Ingredient Name], ([Cupcake_Ingredient].[Qty]* " & multiply9 & ") as Qty, " _
& "Measurements.Unit, Component.Component " _
& "FROM Measurements INNER JOIN (Ingredient INNER JOIN (Cupcake INNER JOIN (Component INNER JOIN " _
& "Cupcake_Ingredient ON Component.[Component ID] = Cupcake_Ingredient.[Component ID]) ON Cupcake.[Cupcake ID] = Cupcake_Ingredient.[Cupcake ID]) " _
& "ON Ingredient.[Ingredient ID] = Cupcake_Ingredient.[Ingredient ID]) ON Measurements.[Unit ID] = Cupcake_Ingredient.[Units ID] " _
& "WHERE Cupcake.[cupcake name]= '" & [cupcakeselect9] & "';"
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
Case 10
sSQL = "INSERT INTO PivotTableSource ([Cupcake Name], [# Cupcake Made], [Ingredient Name], Qty, Unit, Component) " _
& "SELECT Cupcake.[cupcake name], Cupcake.[# Cupcake Made], Ingredient.[Ingredient Name], ([Cupcake_Ingredient].[Qty]* " & multiply10 & ") as Qty, " _
& "Measurements.Unit, Component.Component " _
& "FROM Measurements INNER JOIN (Ingredient INNER JOIN (Cupcake INNER JOIN (Component INNER JOIN " _
& "Cupcake_Ingredient ON Component.[Component ID] = Cupcake_Ingredient.[Component ID]) ON Cupcake.[Cupcake ID] = Cupcake_Ingredient.[Cupcake ID]) " _
& "ON Ingredient.[Ingredient ID] = Cupcake_Ingredient.[Ingredient ID]) ON Measurements.[Unit ID] = Cupcake_Ingredient.[Units ID] " _
& "WHERE Cupcake.[cupcake name]= '" & [cupcakeselect10] & "';"
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
End Select
End Sub