Solved

Unbound sub form no record exists

Posted on 2013-05-20
4
442 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

752 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