Here is the scenario. I have a main form with an unbound subform. I have a list box that the user chooses and I then have the following code.
Me.sfrmChild.SourceObject = "frmCarrierEquipment"
strSQL = "Select tblCarrierEquipment.* from tblCarrierEquipment Where Code ='" & CarrierCode & "'"
urce = strSQL
Me.sfrmChild.Width = 13500
Me.sfrmChild.Height = 3340
lblHeader.Caption = "Equipment Types"
intSelect = 3
Here is where the problem lies. If there is no record in the tblCarrierEquipment then of course it does not have anything to populate. So on the subform I have the following.
Private Sub UpdateEquipment()
Dim rsNewRecord As dao.Recordset
Dim ctl As Control
Dim strField As String
Dim rsUpdate As dao.Recordset
If IsNull(DLookup("Code", "tblCarrierEquipment", "code = '" & Forms!frmCarrierLookup!CarrierCode & "'")) Then
Set rsNewRecord = CurrentDb.OpenRecordset("tblCarrierEquipment")
.Fields("Code") = Forms!frmCarrierLookup!CarrierCode
Me.txtLastUpdateBy = Forms!gate!FName
Me.txtLastUpdateDate = Date
Set rsUpdate = CurrentDb.OpenRecordset("Select * from tblCarrierEquipment where code = '" & Forms!frmCarrierLookup!CarrierCode & "'")
For Each ctl In Me.Controls
If TypeOf ctl Is CheckBox Then
If ctl.Value = -1 Then
strField = ctl.NAME
.Fields(strField) = -1
I call this from the Form_dirty of the subform so as soon as the user clicks on one of the check boxes it fires this. What this is doing is creating two records. One with the carriercode and nothing else and another one with the fields chosen but not the carrier code. Any suggestions on how to do this? I did at first have some code that would create the new record if there was none when the user selected it in the lstbox. That worked. However if they just viewed it and did not make any changes I do not want a record created.