PC Review


Reply
Thread Tools Rate Thread

creating a table

 
 
Rick
Guest
Posts: n/a
 
      7th Feb 2005
I need to create a table in code with certain fields that
I have stored in a recordset. Is there a way to do it
other than a MakeTable sql? How would I add the fields?

Thanks.

Rick
 
Reply With Quote
 
 
 
 
Brendan Reynolds
Guest
Posts: n/a
 
      7th Feb 2005
Public Sub TableFromRecordset()

Dim db As DAO.Database
Dim rstSource As DAO.Recordset
Dim rstTarget As DAO.Recordset
Dim fldSource As DAO.Field
Dim fldTarget As DAO.Field
Dim tdf As DAO.TableDef

Set db = CurrentDb
Set rstSource = db.OpenRecordset("SELECT * FROM tblTest")
Set tdf = db.CreateTableDef("MyNewTable")
For Each fldSource In rstSource.Fields
Set fldTarget = tdf.CreateField(fldSource.name, fldSource.Type,
fldSource.Size)
tdf.Fields.Append fldTarget
Next fldSource
db.TableDefs.Append tdf
Set rstTarget = db.OpenRecordset("SELECT * FROM MyNewTable")
Do Until rstSource.EOF
rstTarget.AddNew
For Each fldSource In rstSource.Fields
rstTarget.Fields(fldSource.name).Value = fldSource.Value
Next fldSource
rstTarget.Update
rstSource.MoveNext
Loop
rstSource.Close
rstTarget.Close

Application.RefreshDatabaseWindow

End Sub

--
Brendan Reynolds (MVP)


"Rick" <(E-Mail Removed)> wrote in message
news:006f01c50d4d$29291bd0$(E-Mail Removed)...
>I need to create a table in code with certain fields that
> I have stored in a recordset. Is there a way to do it
> other than a MakeTable sql? How would I add the fields?
>
> Thanks.
>
> Rick



 
Reply With Quote
 
Rick
Guest
Posts: n/a
 
      8th Feb 2005
Brendan,

Thanks, but my this looks like it is creating a table
using fields from an existing table. My fields for the new
table are stored as records in the source table. I can
loop through the recordset getting the field name but I'm
getting a data type conversion error. I think it may be in
trying to assign the Type and Size properties? I set up a
FieldType and a FieldSize field in the source table and
filled the records with Text and 150 but I still get the
error. What am I doing wrong?

Thanks again. Rick
>-----Original Message-----
>Public Sub TableFromRecordset()
>
> Dim db As DAO.Database
> Dim rstSource As DAO.Recordset
> Dim rstTarget As DAO.Recordset
> Dim fldSource As DAO.Field
> Dim fldTarget As DAO.Field
> Dim tdf As DAO.TableDef
>
> Set db = CurrentDb
> Set rstSource = db.OpenRecordset("SELECT * FROM

tblTest")
> Set tdf = db.CreateTableDef("MyNewTable")
> For Each fldSource In rstSource.Fields
> Set fldTarget = tdf.CreateField(fldSource.name,

fldSource.Type,
>fldSource.Size)
> tdf.Fields.Append fldTarget
> Next fldSource
> db.TableDefs.Append tdf
> Set rstTarget = db.OpenRecordset("SELECT * FROM

MyNewTable")
> Do Until rstSource.EOF
> rstTarget.AddNew
> For Each fldSource In rstSource.Fields
> rstTarget.Fields(fldSource.name).Value =

fldSource.Value
> Next fldSource
> rstTarget.Update
> rstSource.MoveNext
> Loop
> rstSource.Close
> rstTarget.Close
>
> Application.RefreshDatabaseWindow
>
>End Sub
>
>--
>Brendan Reynolds (MVP)
>
>
>"Rick" <(E-Mail Removed)> wrote in

message
>news:006f01c50d4d$29291bd0$(E-Mail Removed)...
>>I need to create a table in code with certain fields that
>> I have stored in a recordset. Is there a way to do it
>> other than a MakeTable sql? How would I add the fields?
>>
>> Thanks.
>>
>> Rick

>
>
>.
>

 
Reply With Quote
 
Brendan Reynolds
Guest
Posts: n/a
 
      8th Feb 2005
Sorry, Rick, I'm afraid I don't understand your description of what you're
trying to do.

--
Brendan Reynolds (MVP)


"Rick" <(E-Mail Removed)> wrote in message
news:07ff01c50df2$77746290$(E-Mail Removed)...
> Brendan,
>
> Thanks, but my this looks like it is creating a table
> using fields from an existing table. My fields for the new
> table are stored as records in the source table. I can
> loop through the recordset getting the field name but I'm
> getting a data type conversion error. I think it may be in
> trying to assign the Type and Size properties? I set up a
> FieldType and a FieldSize field in the source table and
> filled the records with Text and 150 but I still get the
> error. What am I doing wrong?
>
> Thanks again. Rick
>>-----Original Message-----
>>Public Sub TableFromRecordset()
>>
>> Dim db As DAO.Database
>> Dim rstSource As DAO.Recordset
>> Dim rstTarget As DAO.Recordset
>> Dim fldSource As DAO.Field
>> Dim fldTarget As DAO.Field
>> Dim tdf As DAO.TableDef
>>
>> Set db = CurrentDb
>> Set rstSource = db.OpenRecordset("SELECT * FROM

> tblTest")
>> Set tdf = db.CreateTableDef("MyNewTable")
>> For Each fldSource In rstSource.Fields
>> Set fldTarget = tdf.CreateField(fldSource.name,

> fldSource.Type,
>>fldSource.Size)
>> tdf.Fields.Append fldTarget
>> Next fldSource
>> db.TableDefs.Append tdf
>> Set rstTarget = db.OpenRecordset("SELECT * FROM

> MyNewTable")
>> Do Until rstSource.EOF
>> rstTarget.AddNew
>> For Each fldSource In rstSource.Fields
>> rstTarget.Fields(fldSource.name).Value =

> fldSource.Value
>> Next fldSource
>> rstTarget.Update
>> rstSource.MoveNext
>> Loop
>> rstSource.Close
>> rstTarget.Close
>>
>> Application.RefreshDatabaseWindow
>>
>>End Sub
>>
>>--
>>Brendan Reynolds (MVP)
>>
>>
>>"Rick" <(E-Mail Removed)> wrote in

> message
>>news:006f01c50d4d$29291bd0$(E-Mail Removed)...
>>>I need to create a table in code with certain fields that
>>> I have stored in a recordset. Is there a way to do it
>>> other than a MakeTable sql? How would I add the fields?
>>>
>>> Thanks.
>>>
>>> Rick

>>
>>
>>.
>>



 
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 can I detail Pivot Table data without creating a table (Excel2007) Skeletiko Microsoft Excel Misc 0 5th May 2010 12:21 AM
Creating a registration table list based on an existing table ChuckW Microsoft Access Forms 3 16th Dec 2007 12:35 PM
Creating a registration table list based on an existing table ChuckW Microsoft Access Queries 0 12th Dec 2007 04:13 PM
Creating a table from a recordset query then having a report form use the resulting table. LordHog@hotmail.com Microsoft Access 0 18th Oct 2005 07:00 PM
Automatically creating records in one table based on values in another table wslayton Microsoft Access Database Table Design 1 1st Nov 2003 08:47 PM


Features
 

Advertising
 

Newsgroups
 


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