A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Database Design
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

how to add new table programatically



 
 
Thread Tools Display Modes
  #1  
Old February 2nd, 2005, 09:11 PM
Ed White
external usenet poster
 
Posts: n/a
Default how to add new table programatically

I am using VB .Net, although if you tell me how to do it in VBA that it is
just as well.

I have an existing Access database, and I want to add a new Table to that
database via a VB (or VBA) program. What References and commands do I use to
do this?
--
Ed
  #2  
Old February 2nd, 2005, 10:20 PM
Brendan Reynolds
external usenet poster
 
Posts: n/a
Default

In VBA there are at least three possible ways of doing this - using DAO,
using ADOX, or using a DDL query. I would imagine the first two could be
done from .NET via com interop, but I have not tested this. The DDL method
should certainly work.

Public Sub CreateTables()

'DAO

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

Set db = CurrentDb
Set tdf = db.CreateTableDef("FirstTable")
Set fld = tdf.CreateField("TestField", dbText, 50)
tdf.Fields.Append fld
db.TableDefs.Append tdf

'ADOX

Dim cat As ADOX.Catalog
Dim tbl As ADOX.Table
Dim col As ADOX.Column

Set cat = New ADOX.Catalog
Set cat.ActiveConnection = CurrentProject.Connection
Set col = New ADOX.Column
With col
.Name = "TestField"
.Type = adVarWChar
.DefinedSize = 50
End With
Set tbl = New ADOX.Table
With tbl
.Name = "SecondTable"
.Columns.Append col
End With
cat.Tables.Append tbl

'DDL

CurrentProject.Connection.Execute "CREATE TABLE ThirdTable (TestField
VARCHAR(50))"

End Sub


--
Brendan Reynolds (MVP)


"Ed White" .(donotspam) wrote in message
...
I am using VB .Net, although if you tell me how to do it in VBA that it is
just as well.

I have an existing Access database, and I want to add a new Table to that
database via a VB (or VBA) program. What References and commands do I use
to
do this?
--
Ed



  #3  
Old February 3rd, 2005, 09:28 AM
onedaywhen
external usenet poster
 
Posts: n/a
Default


Brendan Reynolds wrote:
What References and commands do I use

In VBA there are at least three possible ways of doing this - using

DAO,
using ADOX, or using a DDL query. I would imagine the first two could

be
done from .NET via com interop, but I have not tested this. The DDL

method
should certainly work.


For .NET, use System.Data.OleDb e.g.

using System.Data.OleDb;
....
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source='C:\\MyJetDB.xls'";
OleDbConnection Conn = new OleDbConnection(strConn);
Conn.Open();
string strSQL = "CREATE TABLE MyTable (MyCol INT NOT NULL);";
OleDbCommand Comm = new OleDbCommand(strSQL, Conn);
Comm.ExecuteNonQuery();
Comm.Connection.Close();

Jamie.

--

  #4  
Old February 4th, 2005, 02:53 AM
Ed White
external usenet poster
 
Posts: n/a
Default

The CREATE TABLE approach works great...thanks.

I was trying as well to use the DAO and ADOX approaches from VB .NET, and as
much as I tried, I could not figure out how to obtain an object reference to
an Access database that could be used with the DAO and ADOX commands. For
example, I tried:

Dim AccessApp As New Microsoft.Office.Interop.Access.Application
AccessApp.OpenCurrentDatabase("C:\Documents and Settings\...\filename.mdb")
Dim cat As ADOX.Catalog
cat.ActiveConnection = AccessApplic.CurrentProject.Connection

The last statement gives me a "Object reference not set to an instance of an
object" error. How do I obtain such a reference to an Access db object from
VB .NET?


"onedaywhen" wrote:


Brendan Reynolds wrote:
What References and commands do I use

In VBA there are at least three possible ways of doing this - using

DAO,
using ADOX, or using a DDL query. I would imagine the first two could

be
done from .NET via com interop, but I have not tested this. The DDL

method
should certainly work.


For .NET, use System.Data.OleDb e.g.

using System.Data.OleDb;
....
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source='C:\\MyJetDB.xls'";
OleDbConnection Conn = new OleDbConnection(strConn);
Conn.Open();
string strSQL = "CREATE TABLE MyTable (MyCol INT NOT NULL);";
OleDbCommand Comm = new OleDbCommand(strSQL, Conn);
Comm.ExecuteNonQuery();
Comm.Connection.Close();

Jamie.

--


 




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Get data from combo box to popluate the next box Lin Light Using Forms 4 December 30th, 2004 05:01 PM
Duplicate data Rob Green Database Design 3 November 7th, 2004 03:08 AM
Access & OleDb - generating schema changes, problem with identity/counter fields. Thomas Tomiczek [MVP] Database Design 9 November 5th, 2004 10:32 AM
Semicolon delimited text query help Al Guerra Running & Setting Up Queries 3 August 12th, 2004 11:50 AM
Name not showing ID is René Setting Up & Running Reports 11 June 29th, 2004 01:40 AM


All times are GMT +1. The time now is 08:15 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.