Link to home
Start Free TrialLog in
Avatar of JArndt42
JArndt42Flag for United States of America

asked on

Unbound sub form no record exists

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 & "'"
        Me.sfrmChild.Form.RecordSource = 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")
    With rsNewRecord
        .AddNew
        .Fields("Code") = Forms!frmCarrierLookup!CarrierCode
        .Update
    End With
End If
Me.txtLastUpdateBy = Forms!gate!FName
Me.txtLastUpdateDate = Date
Set rsUpdate = CurrentDb.OpenRecordset("Select * from tblCarrierEquipment where code = '" & Forms!frmCarrierLookup!CarrierCode & "'")
    With rsUpdate
        For Each ctl In Me.Controls
            If TypeOf ctl Is CheckBox Then
                If ctl.Value = -1 Then
                   strField = ctl.NAME
                   .Edit
                   .Fields(strField) = -1
                   .Update
                End If
            End If
        Next
    End With
End If

End Sub

Open in new window


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.
Carrier-Master.bmp
Avatar of pdebaets
pdebaets
Flag of United States of America image

What is the problem with showing a blank (new) record on frmCarrierEquipment?

Do you have the form frmCarrierEquipment AllowAdditions property set to Yes? If so, then a blank record will appear, ready for data input,  when there are no records in tblCarrierEquipment
Avatar of JArndt42

ASKER

I thought that as well. A problem I was finding is where do I put the code to insert the record? ON Close, Lost Focus?
ASKER CERTIFIED SOLUTION
Avatar of pdebaets
pdebaets
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I thought about that too. so I did this.

        Me.sfrmChild.SourceObject = "frmCarrierEquipment"
        strSQL = "Select tblCarrierEquipment.* from tblCarrierEquipment Where Code ='" & CarrierCode & "'"
        Me.sfrmChild.Form.RecordSource = strSQL
        Me.sfrmChild.LinkChildFields = "Code"
        Me.sfrmChild.LinkMasterFields = "CarrierCode"
        Me.sfrmChild.Width = 13500
        Me.sfrmChild.Height = 3340
        lblHeader.Caption = "Equipment Types"
        intSelect = 3

Of course it works. LOL