Solved

Unbound sub form no record exists

Posted on 2013-05-20
4
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 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