Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unbound sub form no record exists

Posted on 2013-05-20
4
Medium Priority
?
448 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

670 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