473,379 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

How to copy multiple fields from one record to an existing record

I was hoping that someone may give me some help in regards to writing some code.

I have the following code that works - basically a user selects a record from a list box & it copies the fields name & title into the new record with the key(id) already assigned.

What I want to be able to do is the same idea but copy all fields from the selected user record into the new record (except for the id). Obviously the other fields are not available in the list box. So I don't have to do each field separately is there a way using the recordset to do this. I am a novice at writing code using the record set so any help would be appreciated.

Code:

Private Sub cmdCopy_Click()
On Error GoTo Err_cmdCopy_Click
Dim wrk As Workspace
Dim dbs As Database
Dim rst As Recordset
Dim sql As String
Dim stDocName As String
Dim StLinkCriteria As String

Set wrk = DBEngine.Workspaces(0)

Set newId = [Forms]![frmdelnewproperties]![id]


Set dbs = CurrentDb()

'The following 4 lines copies the name & title from the list box into the current record (works!!!)

sql = "update delegation " & _
"Set [delegation].[name] = '" & lstDelFrom.Column(1) & "'," & _
"[delegation].[title] = '" & lstDelFrom.Column(2) & "'" & _
"Where [delegation].[id] = '" & [Forms]![frmdelnewproperties]![id] & "'"



MsgBox "Copying details from Delegation - " & lstDelFrom.Column(0) & " " & lstDelFrom.Column(1) & "-" & lstDelFrom.Column(2)

wrk.BeginTrans
dbs.Execute sql
wrk.CommitTrans

lstDelFrom.Value = 0
'close out of the form
DoCmd.Close


Exit_cmdCopy_Click:

Exit Sub

Err_cmdCopy_Click:
wrk.Rollback
MsgBox Err.Description
Resume Exit_cmdCopy_Click

End Sub
Jan 10 '08 #1
6 2750
zaidlig
45
First I don't know how many fields each record has. You can have many fields in a list box but make their column widths 0 so they don't show. That way you select the record based on the two columns that show but you can read the 14 that are hidden using the ".column(#)" property of the list box.

However the easy way to do it would be set your list box so that the bound column is the ID and set its column width to 0. Now with the ID you can create a simple append query: appending all the fields from table A to table B where the ID you selected is equal the ID in table A.

This query can easily be created as a query and then steal the SQL text and use it in you code to add the variable ID you selected. Also Docmd.RunSQL would be easier than the recordset method in this instance.
Jan 11 '08 #2
First I don't know how many fields each record has. You can have many fields in a list box but make their column widths 0 so they don't show. That way you select the record based on the two columns that show but you can read the 14 that are hidden using the ".column(#)" property of the list box.

However the easy way to do it would be set your list box so that the bound column is the ID and set its column width to 0. Now with the ID you can create a simple append query: appending all the fields from table A to table B where the ID you selected is equal the ID in table A.

This query can easily be created as a query and then steal the SQL text and use it in you code to add the variable ID you selected. Also Docmd.RunSQL would be easier than the recordset method in this instance.

Many thanks for your reply.

The number of fields each record has is 15. But the way I was doing it I only had 3 of the fields making up the list box - ie) the id, title & name where the user picked the record to copy from.

When you say steal the SQL text - do you mean from the update in my code above???

Do I then list each of the fields that I am adding from the text box ie) similar to the following:

sql = "update delegation " & _
"Set [delegation].[name] = '" & lstDelFrom.Column(1) & "'," & _
"[delegation].[title] = '" & lstDelFrom.Column(2) & "'" & _
"Where [delegation].[id] = '" & [Forms]![frmdelnewproperties]![id] & "'"


but continue on with the other 12 fields after the [delegation][title] = lstDelFrom(2) ????

Thanks

Tonia
Jan 15 '08 #3
zaidlig
45
Many thanks for your reply.

The number of fields each record has is 15. But the way I was doing it I only had 3 of the fields making up the list box - ie) the id, title & name where the user picked the record to copy from.

When you say steal the SQL text - do you mean from the update in my code above???

Do I then list each of the fields that I am adding from the text box ie) similar to the following:

sql = "update delegation " & _
"Set [delegation].[name] = '" & lstDelFrom.Column(1) & "'," & _
"[delegation].[title] = '" & lstDelFrom.Column(2) & "'" & _
"Where [delegation].[id] = '" & [Forms]![frmdelnewproperties]![id] & "'"


but continue on with the other 12 fields after the [delegation][title] = lstDelFrom(2) ????

Thanks

Tonia
No I was suggesting create a simple append query that appends all the fields you want based on a example ID. Once that works copy the SQL to your code and replace the "Where [delegation].[id] = '5'" with Where [delegation].[id] ='' & me![ID] & "'". Now when you run the SQL it will append all the fields based on the current value of the ID.
Jan 15 '08 #4
No I was suggesting create a simple append query that appends all the fields you want based on a example ID. Once that works copy the SQL to your code and replace the "Where [delegation].[id] = '5'" with Where [delegation].[id] ='' & me![ID] & "'". Now when you run the SQL it will append all the fields based on the current value of the ID.

Sorry not quite clear on what you mean. Do I list each field separately in the append query as indicated in the update query or do I do something else.

Thanks.
Jan 16 '08 #5
No I was suggesting create a simple append query that appends all the fields you want based on a example ID. Once that works copy the SQL to your code and replace the "Where [delegation].[id] = '5'" with Where [delegation].[id] ='' & me![ID] & "'". Now when you run the SQL it will append all the fields based on the current value of the ID.

Sorry not quite clear on what you mean. Do I list each field separately in the append query as similar to how I am doing it the update query or do I do something else. If I am allowing the user to choose the ID/Name/Title from the list box do I copy these in from the list box??

Thanks.
Jan 16 '08 #6
sierra7
446 Expert 256MB
Sorry not quite clear on what you mean. Do I list each field separately in the append query as similar to how I am doing it the update query or do I do something else. If I am allowing the user to choose the ID/Name/Title from the list box do I copy these in from the list box??

Thanks.
Hi Tonia
I have been looking over your previous posts and you seem to have been stuck for a month on this problem. You must be really fed-up!!!

I think that you were on the right track with this thread but left 'dangling'

You have been right all along, when you have one or two fields to copy then it is quite simple to use the combo-box /List-box in the way you started.

However, if you have 15 fields it gets a bit tedious writing the code, plus as you have found, if any of you 'donor' field are blank or Null, then complications arise when writing the SQL to accommodate these situations.

You were given the right advice (in my opinion) a few posts ago with Zaidlig. You need an Append query.

The way you create this is as follows. We will assume you source table name is tblDonorTable and you want to append the fields to tblTargetTable.
  1. Open a new query - choose Design View
  2. Add tblDonorTable, then double-click on all of the fields that you want to copy, so they appear in the grid.
  3. Now in the Main Menu at the top click Query and choose Append Query to change this query from a SELECT to an APPEND
  4. You will then be prompted for the table name to append to - In this example I have used tblTargetTable but yours may be Delegation, I think.
  5. If the field names are the same in both tables then the wizard will automatically complete the destinations, otherwise set them manually in the Append to line. The cells become combo coxes when you enter them
  6. You only want one record to be copied from tblDonorTable, so arbitarily enter '5' in the Criteria line under ID.
  7. Now click on View in the top menu and select SQL View. Copy and past this code to you SQL string in the After_Update procedure for your listbox.
It should be something like;-
Expand|Select|Wrap|Line Numbers
  1. strSQL = "INSERT INTO tblTargetTable ( id, seg, other, name, title, [section], keywords ) " & _
  2. "SELECT tblDonorTable.id, tblDonorTable.seg, tblDonorTable.other, tblDonorTable.name, tblDonorTable.title, tblDonorTable.section, tblDonorTable.keywords " & _
  3. "FROM tblDonorTable " & _
  4. " WHERE (((tblDonorTable.id)=5)); "
  5.  
NB that [Section] is in square brackets becaue I have used a reserved word for a field name!

You need to customize it so that instead of appending record ID=5, it picks the ID from your list box
Expand|Select|Wrap|Line Numbers
  1.  
  2. "WHERE (((tblDonorTable.id)=" & lstDelFrom.Column(1) & "));"
  3.  
That's about it. I believe that this method does not get involved with Nulls and the like because it is all handled within the SQL and is not trying to interprete what is written in text- or list-boxes.

I'll go and comment on your other string now - about Null Dates, in-case you are too far down that road to turn back !

S7
Feb 4 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: news.hp.com | last post by:
I have situation where I need to copy multiple records (only certain fields) from a Rules table to an Events table based on a selection identified in a combo box. When the selection is made in a...
3
by: Steven Stewart | last post by:
Hi there, I have posted about this before, but yet to arrive at a solution. I am going to try to explain this better. I have an Employees table and a Datarecords table (one-to-many...
11
by: dskillingstad | last post by:
I've been struggling with this problem for some time and have tried multiple solutions with no luck. Let me start with, I'm a novice at Access and I'm not looking for someones help to design my...
3
by: david | last post by:
Hi, I've been reading tons of posts on how to copy records, but to no avail....i'm still stuck. There are three tables: Main, Sub-Form1 & Sub-Form2 I have a form which displays some data....
19
by: davidgordon | last post by:
Hi, I need some pointers/help on how to do the following if it possible: In my access db, I have the following: Tables: Products, Sub-Assembly, Product-Pack Table, Products
18
by: Gleep | last post by:
I've searched google intensely on this topic and it seems noone really knows how to approch this. The goal I don't want clients to give out their usernames and passwords to friends, since the site...
0
by: igendreau | last post by:
I have a database with a Header table. Each record in tblHeader has two One-to-Many Relationships: with tblLines and tblKeys. The HeaderID field ties tblHeader to the other two tables. The data...
5
by: Kaur | last post by:
Hi, I have been successful copying a vba code from one of your posts on how to copy and paste a record by declaring the desired fields that needs to be copied in form's declaration and creating two...
4
4Him
by: 4Him | last post by:
First off, let me say this is a great site! I've just started working with Access and much of my success is from what I've read here! Background: I have a form, driven off a single table. Goal:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.