E
efandango
Graham, one lsight problem...
I have one crucial missing field that has not copied across. The field is
[GetRoundPoint] and is the name of the actual point that the copied record is
going to.
I get it from this combobox: [cbo_copy_to_new_point_ID.Column(1) which is
the same combobox that gives us the reference for its ID as in:
!GetRoundPoint_ID = cbo_copy_to_new_point_ID.
How can I incorporate the additional field in the code?
regards
Eric
I have one crucial missing field that has not copied across. The field is
[GetRoundPoint] and is the name of the actual point that the copied record is
going to.
I get it from this combobox: [cbo_copy_to_new_point_ID.Column(1) which is
the same combobox that gives us the reference for its ID as in:
!GetRoundPoint_ID = cbo_copy_to_new_point_ID.
How can I incorporate the additional field in the code?
regards
Eric
Graham Mandeno said:Hi Eric
I'm glad it's all working! Sleep well!
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
efandango said:Oh my... Is that a light at the end of the tunnel is see?... I think we're
getting there... (This has been killing me for days now... )
you may well chuckle to yourself at this, but I have been working in
parallel on a ludicrously mad 'Heath Robinson syle' multi-environment
crash
inducing, multi-button, multi hidden-text boxes, multi-query,
multi-everything but the kitchen sink solution.
Mainly because just in case things didn't work out with the code, and
partly
because I am (slightly better with queries and levers, than I am with
syntax
heavy code. The upshot is that I very nearly... got it all working... and
then the cavelry turned up in the shape of your good self. Needless to
say,
that your code works simply beautifully, and super quick!!!
I am truly greatful to you Graham, for your help, perseverance and
patience.
it's 1.30am here now, and I'm exhausted but smiling...
have a sunny day.
regard
Eric
Graham Mandeno said:Hi Eric
Ahhhh! Eureka!
Then your code must do the following:
1. Add a record to a recordset based on tbl_Getrounds.
2. Copy all the fields from the current record to the new one EXCEPT for
the
autonumber (GetRound_ID) and the one you want to change
(GetRoundPoint_ID).
3. Set the new GetRoundPoint_ID and save the new GetRound_ID.
4. Save the new record.
5. Execute an INSERT INTO to copy the related records.
The code below should go most of the way to doing the trick.
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
============= start code ===================
Private Sub btn_Do_Copy_Click()
Dim strSql As String
Dim lngNewID As Long
Dim strFieldList As String
On Error GoTo ProcErr
'Save any edits first
If Me.Dirty Then
Me.Dirty = False
End If
'Make sure there is a record to duplicate.
If Me.NewRecord Then
MsgBox "Select the record to duplicate."
GoTo ProcExit
End If
'Duplicate the main record: add to form's clone.
With Me.RecordsetClone
.AddNew
!FromStreetNameID = FromStreetNameID
!ToStreetNameID = ToStreetNameID
!From_PostCode = From_PostCode
!To_PostCode = To_PostCode
!Direction_From = Direction_From
!Direction_To = Direction_To
'etc for other fields.
' set the new GetRoundPoint_ID
!GetRoundPoint_ID = cbo_copy_to_new_point_ID
' save the new GetRound_ID
lngNewID = !GetRound_ID
.Update
' make a list of all the child table fields *except*
' GetRound_Detail_ID and GetRound_ID
strFieldList = ", Run_Direction, Run_waypoint" _
& ", Postcode, Lat, Notmapped, Run_No, StreetNameID "
strSql = "INSERT INTO tbl_Getround_Detail " _
& "(GetRound_ID" & strFieldList & ") " _
& "SELECT " & lngNewID & " As NewID" & strFieldList _
& "FROM tbl_Getround_Detail " _
& "WHERE GetRound_ID=" & Me.GetRound_ID & ";"
CurrentDb.Execute strSql, dbFailOnError
' navigate to the new record
Me.Bookmark = .LastModified
End With
ProcExit:
Exit Sub
ProcErr:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description
Resume ProcExit
End Sub
=============== end code =================
Hi Graham, (PS I wrote this in word, so it should cut n paste nicely
for
easier reading if you need to)
OK, let's see if I can make myself completely clear (bear with me
here.)
I want to insert into both tables: tbl_Getrounds and its subtable
'tbl_Getround_Detail'.
The table below (tbl_Getrounds) is what holds any given single record
that
I
want to selectively copy by saying, "when I copy this record, I want
Access
to generate a new Autonumber 'GetRound_ID' for the new record. But I
want
to
specfiy via a combo box which (already existing) 'GetRoundPoint_ID' to
assign
it to. Once it has done that, I then want the donor records in the
subtable
to also be copied to a new subtable matching the new/chosen
'GetRoundPoint_ID' record.
So that I end up with a mirror copy of the master/child records, but
assigned to another record (via GetRoundPoint).
The name of the box that I will select the 'destination' record for is:
[Forms]![frm_Runs]![frm_Getrounds].[Form]![cbo_copy_to_new_point_ID]
I hope that makes sense for you, (If only Microsoft would allow some
kind
of
image transfer, then I could just paste a screenshot which once you saw
it
would make perfect sense, 1st time around) meanwhile.
Table Specs for both tables below: (For now I have put just the table
fields, if you still need the indexes let me know, I didn't want to
swamp
you
with 5 pages of data.)
Table: tbl_Getrounds (Master containing subtable: tbl_Getround_Detail)
Link Master Fields: txtCurrentPoint
Link Child Fields: GetRoundPoint_ID
Columns
Name Type Size
GetRound_ID (Autonumber) Long Integer 4
GetRoundPoint_ID Long Integer 4
Run_No Long Integer 4
FromStreetNameID Long Integer 4
ToStreetNameID Long Integer 4
FromGetRound Text 100
ToGetRound Text 100
From_PostCode Text 8
To_PostCode Text 8
Reason Text 150
GetRoundPoint Text 100
GetRound_Note Text 255
GetRound_SetDown Text 10
Copied Yes/No 1
Table: tbl_Getround_Detail (Linked to Master table : tbl_Getrounds)
Link Master Fields: GetRound_ID
Link Child Fields: GetRound_ID
Columns
Name Type Size
GetRound_Detail_ID (Autonumber) Long Integer 4
GetRound_ID Long Integer 4
Run_Direction Text 255
Run_waypoint Text 255
Postcode Text 8
Lat Text 30
Notmapped Yes/No 1
Run_No Long Integer 4
StreetNameID Long Integer 4
:
Hi Eric
If I've understood you correctly, you don't want to insert ANY records
into
the parent table, only the child table.
I wasn't aware of your table names until now, and you're right, the
SQL
line
*was* inserting records into the parent table (tbl_Getrounds) not the
child
table (tbl_Getround_Detail).
Also, since GetRound_Detail_ID is the primary key of that table, and
an
autonumber, you should not include it in the INSERT statement,
otherwise
you
will be attempting to create duplicates and nothing will happen at
all!
Here is an emended version:
strSql = "INSERT INTO tbl_Getround_Detail " _
& (GetRound_ID, Run_Direction, Run_waypoint, Postcode) " _
& "SELECT " & Me.Getround_ID & " As NewID, Run_Direction, " _
& "Run_waypoint, Postcode FROM tbl_Getround_Detail " _
& "WHERE GetRound_ID = " & Me.cbo_copy_to_new_point_ID & ";"
CurrentDb.Execute strSql, dbFailOnError
This will duplicate all the child records associated with the current
GetRound_ID on the mail form and associate those copies with the new
GetRound_ID that has been selected in your combo box.
Only three fields will be copied - Run_Direction, Run_waypoint, and
Postcode. The other field (StreetNameID) will be Null, or will get
its
default value if it has one. I don't know whether or not this is what
you
intend.
The code below that is dying with error 3022 is attempting to
duplicate
the
parent record, which is NOT what I understand you are trying to do.
If
this
IS what you require, then post back with the details of the fields in
tbl_Getrounds, including all the indexes.
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
Graham,
I tried the code below: But still get Error 3022. I have spent a lot
of
time
trying to figure out why this is so because as you suggested I have
remmed
out the line: !GetRound_ID = Me.GetRound_ID, but the code stops on
this
yellow line: .Update
another thing is; the SQL line; is it for inserting the subrecords
or
the
main records? the reason I ask is because I seem to have a an
instruction