Solved

Unbound sub form no record exists

Posted on 2013-05-20
4
433 Views
Last Modified: 2013-05-21
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
0
Comment
Question by:JArndt42
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:pdebaets
ID: 39182758
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
0
 
LVL 1

Author Comment

by:JArndt42
ID: 39182796
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?
0
 
LVL 12

Accepted Solution

by:
pdebaets earned 500 total points
ID: 39183227
A bound form will add the record automatically. If you have a recordsource for your form, then it is a bound form.
0
 
LVL 1

Author Closing Comment

by:JArndt42
ID: 39184753
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
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now