473,394 Members | 1,752 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,394 software developers and data experts.

Copy Record with Autonumber & Relationships

I hope someone can help me...I haven't got a clue how to do this.

I have a table with several one-to-many relationships. One of the
related tables has it's own one-to many relationships as well.

I would like to copy a record when I am in a form and append it to the
last record. To make it more complicated, I have an auto-number
(primary key) on the main table and some of the related tables also.

I can't do what I want to do using the command button wizard.
Apparently, I need to do this by code. I've read a bunch of postings
and can't find the code I need to use. My head is spinning with trying
to find code that will work. I've never coded with VB before so I'm
really lost!

Can anyone help??

Dec 11 '06 #1
3 2771
Swinky,
I've used the following to copy records. It's not pretty, but seems to work
OK.

db.Execute "INSERT INTO tblName SELECT " _
& FieldNames(tblName, "AutoNumberField", "", "", "") _
& " FROM tblName" _
& "WHERE fldname= " & somevalue, dbFailOnError

where FieldNames is a function which returns the names of the fields you
wish to copy, and excludes those you don't. The arguments are the fields (up
to 4) that you wish to exclude. My linked table names all start with "t" and
queries with "q".

Public Function FieldNames(tblName As String, exclFld1 As String, _
exclFld2 As String, exclFld3 As String, exclFld4
As String) As String
On Error GoTo ErrorFieldNames
Dim fldNames As String, td As TableDef, qd As QueryDef, db As Database, fld
As Field
Set db = CurrentDb
If Left(tblName, 1) = "t" Then
Set td = db.TableDefs(tblName)
For Each fld In td.Fields
If fld.Name <exclFld1 And fld.Name <exclFld2 And fld.Name <>
exclFld3 And fld.Name <exclFld4 Then
If fldNames <"" Then
fldNames = fldNames & ", " & fld.Name
Else
fldNames = fld.Name
End If
End If
Next
Else
Set qd = db.QueryDefs(tblName)
For Each fld In qd.Fields
If fld.Name <exclFld1 And fld.Name <exclFld2 And fld.Name <>
exclFld3 And fld.Name <exclFld4 Then
If fldNames <"" Then
fldNames = fldNames & ", [" & fld.Name & "]"
Else
fldNames = "[" & fld.Name & "]"
End If
End If
Next
End If
FieldNames = fldNames

Exit Function
ErrorFieldNames:
MsgBox Error$
Exit Function
End Function

Hope this helps
--
Bob Darlington
Brisbane
"Swinky" <sw********@lunt.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>I hope someone can help me...I haven't got a clue how to do this.

I have a table with several one-to-many relationships. One of the
related tables has it's own one-to many relationships as well.

I would like to copy a record when I am in a form and append it to the
last record. To make it more complicated, I have an auto-number
(primary key) on the main table and some of the related tables also.

I can't do what I want to do using the command button wizard.
Apparently, I need to do this by code. I've read a bunch of postings
and can't find the code I need to use. My head is spinning with trying
to find code that will work. I've never coded with VB before so I'm
really lost!

Can anyone help??

Dec 11 '06 #2
I'm running into errors with this code. I get a "ByRef argument type
mismatch" compile error on the code:

& FieldNames(tblName, "OANo", "BudgetID", "ToolID", "RouteID") _

"tblName" is the highlighted word in the error.
Bob Darlington wrote:
Swinky,
I've used the following to copy records. It's not pretty, but seems to work
OK.

db.Execute "INSERT INTO tblName SELECT " _
& FieldNames(tblName, "AutoNumberField", "", "", "") _
& " FROM tblName" _
& "WHERE fldname= " & somevalue, dbFailOnError

where FieldNames is a function which returns the names of the fields you
wish to copy, and excludes those you don't. The arguments are the fields (up
to 4) that you wish to exclude. My linked table names all start with "t" and
queries with "q".

Public Function FieldNames(tblName As String, exclFld1 As String, _
exclFld2 As String, exclFld3 As String, exclFld4
As String) As String
On Error GoTo ErrorFieldNames
Dim fldNames As String, td As TableDef, qd As QueryDef, db As Database, fld
As Field
Set db = CurrentDb
If Left(tblName, 1) = "t" Then
Set td = db.TableDefs(tblName)
For Each fld In td.Fields
If fld.Name <exclFld1 And fld.Name <exclFld2 And fld.Name <>
exclFld3 And fld.Name <exclFld4 Then
If fldNames <"" Then
fldNames = fldNames & ", " & fld.Name
Else
fldNames = fld.Name
End If
End If
Next
Else
Set qd = db.QueryDefs(tblName)
For Each fld In qd.Fields
If fld.Name <exclFld1 And fld.Name <exclFld2 And fld.Name <>
exclFld3 And fld.Name <exclFld4 Then
If fldNames <"" Then
fldNames = fldNames & ", [" & fld.Name & "]"
Else
fldNames = "[" & fld.Name & "]"
End If
End If
Next
End If
FieldNames = fldNames

Exit Function
ErrorFieldNames:
MsgBox Error$
Exit Function
End Function

Hope this helps
--
Bob Darlington
Brisbane
"Swinky" <sw********@lunt.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
I hope someone can help me...I haven't got a clue how to do this.

I have a table with several one-to-many relationships. One of the
related tables has it's own one-to many relationships as well.

I would like to copy a record when I am in a form and append it to the
last record. To make it more complicated, I have an auto-number
(primary key) on the main table and some of the related tables also.

I can't do what I want to do using the command button wizard.
Apparently, I need to do this by code. I've read a bunch of postings
and can't find the code I need to use. My head is spinning with trying
to find code that will work. I've never coded with VB before so I'm
really lost!

Can anyone help??
Dec 12 '06 #3
tblname is a string variable which should be set to the name of the relevant
table. Or just insert the name of the table in quotes.

--
Bob Darlington
Brisbane
"Swinky" <sw********@lunt.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
I'm running into errors with this code. I get a "ByRef argument type
mismatch" compile error on the code:

& FieldNames(tblName, "OANo", "BudgetID", "ToolID", "RouteID") _

"tblName" is the highlighted word in the error.
Bob Darlington wrote:
>Swinky,
I've used the following to copy records. It's not pretty, but seems to
work
OK.

db.Execute "INSERT INTO tblName SELECT " _
& FieldNames(tblName, "AutoNumberField", "", "", "")
_
& " FROM tblName" _
& "WHERE fldname= " & somevalue, dbFailOnError

where FieldNames is a function which returns the names of the fields you
wish to copy, and excludes those you don't. The arguments are the fields
(up
to 4) that you wish to exclude. My linked table names all start with "t"
and
queries with "q".

Public Function FieldNames(tblName As String, exclFld1 As String, _
exclFld2 As String, exclFld3 As String,
exclFld4
As String) As String
On Error GoTo ErrorFieldNames
Dim fldNames As String, td As TableDef, qd As QueryDef, db As Database,
fld
As Field
Set db = CurrentDb
If Left(tblName, 1) = "t" Then
Set td = db.TableDefs(tblName)
For Each fld In td.Fields
If fld.Name <exclFld1 And fld.Name <exclFld2 And fld.Name <>
exclFld3 And fld.Name <exclFld4 Then
If fldNames <"" Then
fldNames = fldNames & ", " & fld.Name
Else
fldNames = fld.Name
End If
End If
Next
Else
Set qd = db.QueryDefs(tblName)
For Each fld In qd.Fields
If fld.Name <exclFld1 And fld.Name <exclFld2 And fld.Name <>
exclFld3 And fld.Name <exclFld4 Then
If fldNames <"" Then
fldNames = fldNames & ", [" & fld.Name & "]"
Else
fldNames = "[" & fld.Name & "]"
End If
End If
Next
End If
FieldNames = fldNames

Exit Function
ErrorFieldNames:
MsgBox Error$
Exit Function
End Function

Hope this helps
--
Bob Darlington
Brisbane
"Swinky" <sw********@lunt.comwrote in message
news:11**********************@j72g2000cwa.googleg roups.com...
>I hope someone can help me...I haven't got a clue how to do this.

I have a table with several one-to-many relationships. One of the
related tables has it's own one-to many relationships as well.

I would like to copy a record when I am in a form and append it to the
last record. To make it more complicated, I have an auto-number
(primary key) on the main table and some of the related tables also.

I can't do what I want to do using the command button wizard.
Apparently, I need to do this by code. I've read a bunch of postings
and can't find the code I need to use. My head is spinning with trying
to find code that will work. I've never coded with VB before so I'm
really lost!

Can anyone help??

Dec 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Frances | last post by:
Hi All, I'm having a problem trying to add a record to a simple Access 2000 db (db is very similar to an address book but with more info than the usual address, phone, etc.). The database is...
0
by: elvin | last post by:
Okay - apologize in advance for the length, but I want to make sure all you knowledgeable and helpful people have all the details you need to hopefully point my newbie rear in the right direction....
5
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one...
1
by: interuser | last post by:
Hi I have a table STUDENTS with key (autonumber) StudentId. Studentid is a lookup column in other tables and forms I want to make it number. When I try, I get "you can't change the data type or...
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....
5
by: Swinky | last post by:
I have a form "AccountInfo" that contains company names. I have inserted a subform "Contacts" with contact names and have established parent/child relationships between the two forms. All works...
2
by: Swinky | last post by:
I hope someone can help...I feel like I'm walking in the dark without a flashlight (I'm NOT a programmer but have been called to task to do some work in Access that is above my head). I have...
12
by: magmike | last post by:
Accidentally deleted a record. Anyway to get it back? If not, I know the ID number - which is an autonumber field. Because of the related data from other tables, would I be able to create a new...
2
kcdoell
by: kcdoell | last post by:
Hello: I have four tables: tblDivision tblWorking_Region tblCredit_Region tblForecast (This is the main data entry table for all forecasting records) tblDivision has the following fields:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.