Hi Marshall, so sorry for the delay. We got slowed by year-end closing and
then I couldn't find the thread. Here's my code from the listbox and sql
statement:
Private Sub cboCustomerName_AfterUpdate()
Dim sql As String 'Fill listbox with Buildings
that apply to Customer when Customer is entered or updated
sql = "SELECT tblCustomer.CustomerName, tblClientBuildings.JobAddress1,
tblClientBuildings.JobAddress2, tblClientBuildings.JobAddress3,
tblClientBuildings.JobAddress4, tblClientBuildings.BuildingNo,
tblClientBuildings.ContactName1, tblClientBuildings.Phone1,
tblClientBuildings.Ext1, tblClientBuildings.Cell1, tblClientBuildings.Email1,
tblClientBuildings.ContactName2, tblClientBuildings.Phone2,
tblClientBuildings.Ext2, tblClientBuildings.Cell2, tblClientBuildings.Email2,
tblClientBuildings.ContactName3,tblClientBuildings.Phone3,
tblClientBuildings.Ext3, tblClientBuildings.Cell3, tblClientBuildings.Email3,
tblClientBuildings.SpecInstNotes, tblClientBuildings.RoofPlanLoc,
tblClientBuildings.PrintAll, tblClientBuildings.Reg FROM tblCustomer INNER
JOIN tblClientBuildings ON tblCustomer.CustomerName =
tblClientBuildings.CustomerName WHERE tblClientBuildings.CustomerName =
Forms!frmWorkOrders2!cboCustomerName ORDER BY tblCustomer.CustomerName,
tblClientBuildings.JobAddress1, tblClientBuildings.JobAddress2;"
Me!lstBuildings.RowSource = sql
End Sub
Private Sub lstBuildings_Click() 'if listbox is
clicked then move all data to subform
If Not IsNull(Me.lstBuildings) Then
Forms.frmWorkOrders2.sfrmClientBuildings.Form.txtBuildingNo =
Me.lstBuildings.Column(5) 'Put Building No. in subform
Me.txtBuildNo = Me.lstBuildings.Column(5)
'Put Building No. in main form
Forms.frmWorkOrders2.sfrmClientBuildings.Form.txtJobAddress1 =
Me.lstBuildings.Column(1) 'Put Job Address1 in subform
Forms.frmWorkOrders2.sfrmClientBuildings.Form.txtRegion =
Me.lstBuildings.Column(24) 'Put Region in subform
If Not IsNull(Me.lstBuildings.Column(2)) Then
'Put rest of data in subform, check for null field
first
Forms.frmWorkOrders2.sfrmClientBuildings.Form.txtJobAddress2 =
Me.lstBuildings.Column(2)
Else
Forms.frmWorkOrders2.sfrmClientBuildings.Form.txtJobAddress2 = ""
End If
If Not IsNull(Me.lstBuildings.Column(3)) Then
Forms.frmWorkOrders2.sfrmClientBuildings.Form.txtJobAddress3 =
Me.lstBuildings.Column(3)
Else
Forms.frmWorkOrders2.sfrmClientBuildings.Form.txtJobAddress3 = ""
End If
If Not IsNull(Me.lstBuildings.Column(4)) Then
Forms.frmWorkOrders2.sfrmClientBuildings.Form.txtJobAddress4 =
Me.lstBuildings.Column(4)
Else
Forms.frmWorkOrders2.sfrmClientBuildings.Form.txtJobAddress4 = ""
End If
End If
End Sub
The purpose of the form is to add Work Orders to tblWorkOrders but the
purpose of the subform is to simply display info from listbox. When user
clicks on combo box, the listbox fills, then when listbox is clicked, the
subform fills. The record source for the subform is tlbClientBuildings.
When I try to add a new Work Order, that is if I click on the listbox for a
new Work Order, or if I try to change info on a old Work Order by clicking on
the listbox, I get error message "The changes you requested to the table were
not sucessful because they would create duplicate values in the index,
primary key or relationship..... I think the problem is that VB thinks I'm
trying to change the record behind the subform - maybe the recordsource is
the problem but I don't know how to assign on the fly. Any help appreciated.
Marshall Barton said:
I really do not understand what you are trying to do. I
thought you said you did not want to allow any changes, but
then you say you can't update a record that was added.
Somewhere else you said the main form's list box is used to
add data to the subform, but you get an error about
duplicate records. Somehow AllowAdditions comes into the
discussion as a way to prevent the error.
If you want to add a record and then be able to change it, I
don't see how anything we've discussed applies to your
problem. I still do not know how the list box creates a new
subform record or how you are trying to update a record that
was added.
At this point, I suspect that AllowAdditions is the wrong
idea and the problem is in what the list box is doing.
Maybe I can get a useful idea if you posted the code for the
list box???
--
Marsh
MVP [MS Access]
JIM wrote:
I'm assuming you meant = False for all the allow commands -which I tried in
the open event. This doesn't work for me because I am updating the subreport
with information from the listbox. And with no additions, edits or adds and
you are trying to create a new record on the main form, the subform goes
blank and when the subform is blank it can't receive info from the list box.
I'm sure there is a work around for this as it seems like a basic design that
many would need.
:
You should be able to that just about anywhere (as long as
the current record is not dirty). In your situation, the
first place I would try is the Open event:
Me.AllowEdits = True
Me.AllowAdditions = True
Me.AllowDeletions = True
Using all three statements will prevent any changes to the
form's recordset.
JIM wrote:
You got it. I just want to show the user that they have picked the correct
building and I'm updating the work order with the building number, a foreign
key, to reference when work order is printed. Where do you suggest I put the
allow commands? Do I have to use all three in order for it to work? I can
do that and it won't be a problem. Just want to cover all the bases while I
have your expertise.
:
I'm still not following it. Are you saying that the subform
in frmWorkOrders is for searching? If so, it should be
unbound so there is no way for Access to try to save your
search criteria.
OTOH, maybe the frmWorkOrders instance of the subform is
only for selecting/viewing existing records and you do not
want to allow any changes. In this case, setting its
AllowEdits, AllowAdditions and AllowDeletions to False
should do what you want.
JIM wrote:
Marshall, I should explain more: on the main form I have a bound list box and
when clicked the subform is populated with the correct record. The listbox
is filled from a combo box on the main form. As stated before this
arrangement works well to create a work the first time. If I go back to made
a change to the subform record then the error message is displayed. Do I
have to have an unbound list box?
:
My comments also apply to AllowAdditions.
If you do not want to save any changes that have already
been made to the currrent record, then you need to undo the
change(s)
If Me.Dirty Then Me.Undo
JIM wrote:
I'm not concerned with AllowEdits, that's working fine when subform is on
frmCustomer. When the subform is on frmWorkOrders I need a way to tell
subform not to try to add a record - I thought AllowAditions = False would do
it. When subform is on frmWorkOrders I click on a listbox and the fields are
populated. If I move to another record then come back to first record and
have to change something I get the error message.
JIM wrote:
I keep getting the message: "The changes you requested to the table were not
sucessful because they would create duplicate values in the index, primary
key or relationship.....
I want to use the subform to update a file when it's a on frmCustomer but
when it's on frmWorkOrder I'm using the subform to just collect data. It
works fine if a work order is created for the first time but I get the above
message if I try to go back and change the data in the subform. Here's my
code: