Solved

ADODB.Recordset to new access table same fields names, types and size

Posted on 2015-02-05
8
60 Views
Last Modified: 2016-07-12
Hi there

i would like to create a function that create a table in (Access database) with the same fields name/type/size of the ADODB.Recordset that i passed to the function.

Also if the table is exists, then we need to check all the fields are exists too with the same types, otherwise update the table to match the recordset

the function will look something like that

Public Function NewRSTable(rs As ADODB.Recordset, cn ad ADODB.Connection, Optional OutputTableName As String) as Boolean
if database in cn is not exist
    create empty database using same cn path
else
   1- check if OutputTableName table exist - match all fileds with the rs otherwise, update the table definition to match the RS (or we can simply delete the table and re-create it again)
   2- create a new table using same field name/type/size in the recordset
end if

return yes/no and why not

i hope you understand what i mean

kind regards,
Mohamed
0
Comment
Question by:Mohamed ElSheikh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
8 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40591901
Why not just copy the table, then clear it out?

'Copy the table to a new table based on he names in two textboxes
DoCmd.CopyObject , Me.txtNewTableName, acTable, Me.txtTableName
'Clear all the records from the new table.
CurrentDb.Execute "DELETE * FROM " & txtNewTableName, dbFailOnError

JeffCoachman
0
 

Author Comment

by:Mohamed ElSheikh
ID: 40591942
Thank you for your feedback

RS contains data from different tables and i want this function to be re-useable for different tools

thanks
Mohamed
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40592031
OK, then lets see what other Exerts may contribute.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84
ID: 40592506
That's a pretty big request on a volunteer help forum ...

You can create a database using several methods. One is via the DAO.CreateDatabase method:

DAO.CreateDatabase "Full path to the db" , dbLangGeneral, dbVersion120

Here: https://msdn.microsoft.com/en-us/library/office/ff835033%28v=office.15%29.aspx

You can check whether it exists by using DIR. After parsing it from the Connection:

If Dir("YourPath") <> "" Then
  '/ it exists
Else
  '/ it does not exist
End If

To create the table, you'd have to loop through the Fields collection of the Recordset:

Dim fld As ADODB.Field
For each fld in rst.Fields
  '/ add the column to a table
Next fld
0
 

Author Comment

by:Mohamed ElSheikh
ID: 40592660
Hi Scott McDaniel,

thank you for your reply
how can we add fields to the new table to be similar (name/type/size) to fields in recordset
I am expecting the code to read the fields definitions in recordset and create same field definitions into the new table
I am able to loop in the RS fields and get the fields names. but to get the types it returns different types like "string" instead of "text". and i am not sure about the other types

Thanks
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 40593282
The ADO Field object would give you everything needed. The Field.Type would tell you the datatype and Field.DefinedSize would tell you the Length, in the case of Text field.

To add fields, you'd first add a Table, then append Fields to that Table:

Dim tdf As DAO.TableDef
Set tdfNew = CurrentDB.CreateTableDef("YourTableName")
tdf.Fields.Append tdf.CreateField(fld.Name, fld.Type, fld.DefinedSize)

I'm not sure if the ADO Field.Type will translate over to the DAO Field.Type, but you can give it a shot.

Note this does not create indexes, primary keys, or anything of that sort. This would just create a table with columns of a specific data type.

DAO CreateTableDef: https://msdn.microsoft.com/en-us/library/office/ff835094.aspx
DAO CreateField: https://msdn.microsoft.com/EN-US/library/office/ff821396.aspx
ADODB Field Object: http://www.experts-exchange.com/Database/MS_Access/Q_28611073.html#a40592660
0
 

Author Comment

by:Mohamed ElSheikh
ID: 40753951
Thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question