Solved

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

Posted on 2015-02-05
8
41 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
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now