Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 3rd, 2003, 04:04 AM
Registered User
 
Join Date: Nov 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Machiaveli280278
Default Automatically create tables

Hi everyone,

Who can help me to automatically create tables by using a table. I have a table where i store tabledefinitions such as the table name, fieldname, datatype, etcetera. What i would like to work is that table are created by the values of that table.

Example:
-in field1, i have the name of the table (e.g. tblOrders)
-in field2, i have the name of the fields (e.g. customerId)
-in field3, i have the datatype of the fieldname (e.g. numeric)
-in field4, i have the characterlength of the fieldname (e.g. long integer)

How can i automate this proces without manually create the tables.

Greetings

Mach

 
Old November 3rd, 2003, 10:48 AM
sal sal is offline
Friend of Wrox
 
Join Date: Oct 2003
Posts: 702
Thanks: 0
Thanked 0 Times in 0 Posts
Default

check this site for some sample code for the ADOX library. Also, you can look under ADO - ADOX in MS Access Help for more info.

http://www.4guysfromrolla.com/ASPscr...013101-1.shtml





Sal
 
Old November 3rd, 2003, 03:20 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Mach,

You may want to take a look at this article: http://Imar.Spaanjaars.com/QuickDocI...QUICKDOCID=143

It describes how to create tables from an ASP page, but the principle is the same for VB and VBA.

The code is written like this:

  Dim catNewDB ' As ADOX.Catalog

So all you need to do is remove the ' to make it a strongly typed example.

HtH,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old September 26th, 2006, 04:20 PM
pjm pjm is offline
Authorized User
 
Join Date: Jul 2006
Posts: 70
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My first suggestion would be to make Field3 match the Access field types (ie. not "Numeric", but "Integer", "Long", "Double", etc.) and make Field4 the actual length. You really only need to store the length for text fields since you won't need to specify the length for the numeric datatypes.

Dim db As Database, rs As Recordset
Dim tdf As TableDef, fld As Field
Dim r As Long, nRecords As Long

    Set db = CurrentDb()
    rs = Openrecordset(DefinitionTableName, dbOpenDynaset)

    rs.MoveLast
    nRecords = rs.RecordCount
    rs.MoveFirst

    Set tdf = db.CreateTableDef(rs("Field1"))

    For r = 1 To nRecords
        if rs("Field3") = "Text" then
                Set fld = tdf.CreateField(rs("Field2"), rs("Field3"), rs("Field4"))
        else
                Set fld = tdf.CreateField(rs("Field2"), rs("Field3"))
        end if

        tdf.Fields.Append fld
        fld.AllowZeroLength = True
    Next r

    tdf.Fields.Refresh

    db.TableDefs.Append tdf
    db.TableDefs.Refresh

    rs.Close

I realize that this code only allows for one table to be defined and there may be other things that I haven't accounted for, but I do have to leave you some things to do.


-Phil-





Similar Threads
Thread Thread Starter Forum Replies Last Post
automatically create new records scandalous Access VBA 3 February 27th, 2007 03:08 PM
create a relation betwn 2 tables in a dataset dhol General .NET 1 August 9th, 2005 09:17 AM
Create a report from multiple tables in a Dataset verdun Crystal Reports 4 May 12th, 2005 09:20 AM
Dynamically create tables vickriz Javascript How-To 8 August 5th, 2003 03:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.