Code:
Private Sub butEdit_Click()
'check for data existence
If Not (Me.frmEntrySub.Form.Recordset.EOF And Me.frmEntrySub.Form.Recordset.BOF) Then
'Load existing entry into form
With Me.frmEntrySub
Me.txtAssembly = !Assembly
Me.txtComponent = !Component
Me.txtDescription = !Description
Me.numAssemblyQty = !AssemblyQty
Me.txtUOM = !UOM
Me.txtAlternateA = !AlternateA
Me.txtAlternateB = !AlternateB
Me.numQtyA = !QtyA
Me.numQtyB = !QtyB
Me.numQtyC = !QtyC
Me.dbPriceA = !UnitPriceA
Me.dbPriceB = !UnitPriceB
Me.dbPriceC = !UnitPriceC
'store ID in tag in case of change
Me.ID.Tag = !ID
'change Add button caption
Me.butAdd.Caption = "Update"
'disable Edit button
Me.butEdit.Enabled = False
End With
End If
End Sub
Private Sub butAdd_Click()
'Add entry to list
'Insert vs Update options
'If the part is not in the list
If Me.ID.Tag & "" = "" Then
'add the part to the list
'Add main part
CurrentDb.Execute "INSERT INTO EntryFormTable(Assembly, Component, Description, AssemblyQty, UOM, QtyA, QtyB, QtyC, UnitPriceA, UnitPriceB, UnitPriceC) " & _
" VALUES ('" & Me.txtAssembly & "','" & Me.txtComponent & "','" & Me.txtDescription & "','" & Me.numAssemblyQty & "','" & Me.txtUOM & "','" & Me.numQtyA & "','" & _
Me.numQtyB & "','" & Me.numQtyC & "','" & Me.dbPriceA & "','" & Me.dbPriceB & "','" & Me.dbPriceC & "')"
'If there is an AlternateA, Add AlternateA
If Not (Me.txtAlternateA & "" = "") Then
CurrentDb.Execute "INSERT INTO EntryFormTable(Component, Description, UOM) " & _
" VALUES ('" & Me.txtAlternateA & "','" & Me.txtAltADescription & " \ALT" & "','" & Me.txtAltAUOM & "')"
End If
'If there is an AlternateB, Add AlternateB
If Not (Me.txtAlternateB & "" = "") Then
CurrentDb.Execute "INSERT INTO EntryFormTable(Component, Description, UOM) " & _
" VALUES ('" & Me.txtAlternateB & "','" & Me.txtAltBDescription & " \ALT" & "','" & Me.txtAltBUOM & "')"
End If
'update an already existing part
Else
CurrentDb.Execute "UPDATE EntryFormTable " & _
" SET Assembly='" & Me.txtAssembly & "'" & _
", Component='" & Me.txtComponent & "'" & _
", Description='" & Me.txtDescription & "'" & _
", AssemblyQty='" & Me.numAssemblyQty & "'" & _
", UOM='" & Me.txtUOM & "'" & _
", QtyA='" & Me.numQtyA & "'" & _
", QtyB='" & Me.numQtyB & "'" & _
", QtyC='" & Me.numQtyC & "'" & _
", PriceA='" & Me.dbPriceA & "'" & _
", PriceB='" & Me.dbPriceB & "'" & _
", PriceC='" & Me.dbPriceC & "'" & _
" WHERE ID='" & Me.frmEntrySub!ID & "'"
CurrentDb.Execute "UPDATE EntryFormTable " & _
" SET AlternateA='" & Me.txtAlternateA & "'" & _
", Description='" & Me.txtAltADescription & "'" & _
", UOM='" & Me.txtAltAUOM & "'" & _
" WHERE ID='" & Me.frmEntrySub!ID & "'"
CurrentDb.Execute "UPDATE EntryFormTable " & _
" SET AlternateB='" & Me.txtAlternateB & "'" & _
", Description='" & Me.txtAltBDescription & "'" & _
", UOM='" & Me.txtAltBUOM & "'" & _
" WHERE ID='" & Me.frmEntrySub!ID & "'"
End If
'Clear form after add/update
butClear_Click
'Refresh form
frmEntrySub.Form.Requery
End Sub