PC Review


Reply
Thread Tools Rate Thread

Append entire recordset

 
 
=?Utf-8?B?S1NI?=
Guest
Posts: n/a
 
      23rd Jan 2006
Is there an easy way to append an entire recordset to a table? I want all
the fields but I don't want to use this statement because it can cause
duplicate id's. I want different autonumber ID's in the destination table.

INSERT INTO TblSab900_history SELECT * FROM TblSab900 Where ID=" & Rs!ID

The only other way I know is to define to recordsets and move each
individual field and then to an update to the destination table.

Any advice would be appreciated.

Thanks,

Kerry

--
KSH
 
Reply With Quote
 
 
 
 
Van T. Dinh
Guest
Posts: n/a
 
      23rd Jan 2006
INSERT INTO ... SQL is more efficient than traversing a Recordset,
especially with large number of rows to be inserted.

All you need to do is to list the Fields except for the ID Field, something
like:

INSERT INTO TblSab900_history
( {List of Fields except ID Field} )
SELECT {List of corresponding Fields except ID Field}
FROM TblSab900
WHERE {criteria if required}


--
HTH
Van T. Dinh
MVP (Access)



"KSH" <(E-Mail Removed)> wrote in message
news:8F476451-6315-40A8-AD95-(E-Mail Removed)...
> Is there an easy way to append an entire recordset to a table? I want all
> the fields but I don't want to use this statement because it can cause
> duplicate id's. I want different autonumber ID's in the destination
> table.
>
> INSERT INTO TblSab900_history SELECT * FROM TblSab900 Where ID=" & Rs!ID
>
> The only other way I know is to define to recordsets and move each
> individual field and then to an update to the destination table.
>
> Any advice would be appreciated.
>
> Thanks,
>
> Kerry
>
> --
> KSH



 
Reply With Quote
 
=?Utf-8?B?S1NI?=
Guest
Posts: n/a
 
      23rd Jan 2006
Thanks but I was trying to avoid that also as to not list all the field names
in case new ones are added. But unless I hear of something else that is what
I will do.

Thanks,

Kerry

--
KSH


"Van T. Dinh" wrote:

> INSERT INTO ... SQL is more efficient than traversing a Recordset,
> especially with large number of rows to be inserted.
>
> All you need to do is to list the Fields except for the ID Field, something
> like:
>
> INSERT INTO TblSab900_history
> ( {List of Fields except ID Field} )
> SELECT {List of corresponding Fields except ID Field}
> FROM TblSab900
> WHERE {criteria if required}
>
>
> --
> HTH
> Van T. Dinh
> MVP (Access)
>
>
>
> "KSH" <(E-Mail Removed)> wrote in message
> news:8F476451-6315-40A8-AD95-(E-Mail Removed)...
> > Is there an easy way to append an entire recordset to a table? I want all
> > the fields but I don't want to use this statement because it can cause
> > duplicate id's. I want different autonumber ID's in the destination
> > table.
> >
> > INSERT INTO TblSab900_history SELECT * FROM TblSab900 Where ID=" & Rs!ID
> >
> > The only other way I know is to define to recordsets and move each
> > individual field and then to an update to the destination table.
> >
> > Any advice would be appreciated.
> >
> > Thanks,
> >
> > Kerry
> >
> > --
> > KSH

>
>
>

 
Reply With Quote
 
John Vinson
Guest
Posts: n/a
 
      24th Jan 2006
On Mon, 23 Jan 2006 13:21:03 -0800, KSH
<(E-Mail Removed)> wrote:

>Thanks but I was trying to avoid that also as to not list all the field names
>in case new ones are added. But unless I hear of something else that is what
>I will do.


That is what's needed, if you want the ID to autoincrement. * means
append all fields, and if you append all fields you're appending the
ID - and getting ID errors as a result.

You don't need to type them all out though: just shift-select them in
the list of fields, and drag to the Field row of the query grid. Three
seconds tops.

John W. Vinson[MVP]
 
Reply With Quote
 
Dirk Goldgar
Guest
Posts: n/a
 
      24th Jan 2006
"KSH" <(E-Mail Removed)> wrote in message
news:FBCAACF8-185C-40BF-B2BA-(E-Mail Removed)
> Thanks but I was trying to avoid that also as to not list all the
> field names in case new ones are added. But unless I hear of
> something else that is what I will do.
>
> Thanks,
>
> Kerry
>
>
>> INSERT INTO ... SQL is more efficient than traversing a Recordset,
>> especially with large number of rows to be inserted.
>>
>> All you need to do is to list the Fields except for the ID Field,
>> something like:
>>
>> INSERT INTO TblSab900_history
>> ( {List of Fields except ID Field} )
>> SELECT {List of corresponding Fields except ID Field}
>> FROM TblSab900
>> WHERE {criteria if required}


If you're building the SQL statement in code, you could build the field
list on the fly by inspecting the table definition. The following rough
routine gives the idea, though it doesn't contain any error-handling or
cover all possible bases:

'----- start of code -----
Function fncFieldList( _
pstrTableName As String, _
Optional pblnExcludeAutonumber As Boolean) _
As String

Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim strFieldList As String

Set db = Application.DBEngine.Workspaces(0).Databases(0)

Set tdf = db.TableDefs(pstrTableName)
For Each fld In tdf.Fields
If (fld.Attributes And dbAutoIncrField) Then
If Not pblnExcludeAutonumber Then
strFieldList = strFieldList & ", [" & fld.Name & "]"
End If
Else
strFieldList = strFieldList & ", [" & fld.Name & "]"
End If
Next fld

Set tdf = Nothing
Set db = Nothing

If Len(strFieldList) > 0 Then
fncFieldList = Mid$(strFieldList, 3)
End If

End Function
'----- end of code -----

In your case, you'd want to call the function with the option argument
pblnExcludeAutonumber set to True.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


 
Reply With Quote
 
Van T. Dinh
Guest
Posts: n/a
 
      24th Jan 2006
While there ways to handle this by code (e.g. Dirk's posted code), IMHO,
this is generally not necessary since in a properly structured / developed
and implemented database, new Fields are rarely added since adding new
Fields means that all other Access objects that are based on the relevant
Tables will need to be modified to work with the new Table Fields. You
already found that out about Queries but there are Forms / Reports / VBA
codes ... that are more likely to be more complex to modify.

Remember that in a properly designed / developed and implemented database
application, we add data by adding Records, not Fields unless there is a
change in the requirements that necessitate the modification of the Table
Structure. If you see that you will need to regularly add Fields, you may
have an incorrect Table Structure. If this is the case, you should really
look at the Table Structure and re-design it so that data can be added by
adding Records and not Fields.

--
HTH
Van T. Dinh
MVP (Access)



"KSH" <(E-Mail Removed)> wrote in message
news:FBCAACF8-185C-40BF-B2BA-(E-Mail Removed)...
> Thanks but I was trying to avoid that also as to not list all the field
> names
> in case new ones are added. But unless I hear of something else that is
> what
> I will do.
>
> Thanks,
>
> Kerry
>
> --
> KSH
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Append a Filtered Recordset To Another Recordset James Microsoft Access 3 7th Aug 2006 07:06 PM
How To Append Record To Recordset from another Filtered Recordset James Microsoft Access 0 4th Aug 2006 07:45 PM
Append a recordset =?Utf-8?B?QWRtU3RlY2s=?= Microsoft Access VBA Modules 1 25th Jul 2005 05:08 PM
Re: append only recordset BJ Freeman Microsoft Access ADP SQL Server 0 30th Jun 2003 02:56 PM
Re: append only recordset Graham R Seach Microsoft Access ADP SQL Server 0 30th Jun 2003 01:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:53 PM.