I have a form where a user reports issues occurring with raw materials.
If the commodity they are using and(or) defect/non-conformance they are experiencing isn't already included in the combo boxes (have some base commodities and defects set up, but thinking long term use right now) they can click a button to go to another form to enter in a new defect and associated commodity. They click 'add' and access will add the defect and commodity into the db. It works well, except when I try and add a defect to an already existing commodity.
I have two separate tables germane to this: tblCommodity with 'commodity' as the PK, and tblNonConformances with fields 'defect' & 'commodity', and an auto# PK. 1-Many rship on the field 'commodity'. Now when they add a defect to an existing commodity I get an error since I am attempting to add a duplicate PK. This is my current code behind the add button:
Is there a way I can get the entry to add to tblCommodity only if the commodity entry does not already exist? (but still add the 'defect' and 'commodity' to the tblNonConformances)Code:Sub btnAdd_Click() On Error GoTo Err_proc0 Dim B As Object Dim V As Object Set V = CurrentDb.OpenRecordset("tblCommodity") V.AddNew V![Commodity_PK] = Me.txtCommodity.Value V.Update Set V = Nothing Set B = CurrentDb.OpenRecordset("tblNonConformances") B.AddNew B![NonConformance] = Me.txtNonConformance.Value B![Commodity] = Me.txtCommodity.Value B.Update Set B = Nothing Forms!frmIssueEntry!cboCommodity.Requery Forms!frmIssueEntry!cboDefect.Requery DoCmd.Close Exit Sub Err_proc0: MsgBox Err.Description Exit Sub End Sub
Let me know if I can help explain this better.


Add Entry to a Table Unless Already Exists, VBA
Reply With Quote


