473,414 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Add new field in the table using macro

Hello All,
How to add a new field in the table using vba or macro. Pls. help.
Thanks.
Dec 4 '07 #1
2 12752
I normally use VBA and DAO to do this.

CreateField Method


Creates a new Field object (Microsoft Jet workspaces only).

Syntax

Set field = object.CreateField (name, type, size)

The CreateField method syntax has these parts.

Part Description
field An object variable that represents the Field object you want to create.
object An object variable that represents the Index, Relation, or TableDef object for which you want to create the new Field object.
name Optional. A Variant (String subtype) that uniquely names the new Field object. See the Name property for details on valid Field names.
type Optional. A constant that determines the data type of the new Field object. See the Type property for valid data types.
size Optional. A Variant (Integer subtype) that indicates the maximum size, in bytes, of a Field object that contains text. See the Size property for valid size values. This argument is ignored for numeric and fixed-width fields.



Remarks

You can use the CreateField method to create a new field, as well as specify the name, data type, and size of the field. If you omit one or more of the optional parts when you use CreateField, you can use an appropriate assignment statement to set or reset the corresponding property before you append the new object to a collection. After you append the new object, you can alter some but not all of its property settings. See the individual property topics for more details.

The type and size arguments apply only to Field objects in a TableDef object. These arguments are ignored when a Field object is associated with an Index or Relation object.

If name refers to an object that is already a member of the collection, a run-time error occurs when you use the Append method.

To remove a Field object from a Fields collection, use the Delete method on the collection. You can't delete a Field object from a TableDef object's Fields collection after you create an index that references the field.
Example code from Access VBA Helpfile

Expand|Select|Wrap|Line Numbers
  1. CreateField Method Example
  2.  
  3. This example uses the CreateField method to create three Fields for a new TableDef. It then displays the properties of those Field objects that are automatically set by the CreateField method. (Properties whose values are empty at the time of Field creation are not shown.)
  4.  
  5. Sub CreateFieldX()
  6.  
  7.     Dim dbsNorthwind As Database
  8.     Dim tdfNew As TableDef
  9.     Dim fldLoop As Field
  10.     Dim prpLoop As Property
  11.  
  12.     Set dbsNorthwind = OpenDatabase("Northwind.mdb")
  13.  
  14.     Set tdfNew = dbsNorthwind.CreateTableDef("NewTableDef")
  15.  
  16.     ' Create and append new Field objects for the new 
  17.     ' TableDef object.
  18.     With tdfNew
  19.         ' The CreateField method will set a default Size
  20.         ' for a new Field object if one is not specified.
  21.         .Fields.Append .CreateField("TextField", dbText)
  22.         .Fields.Append .CreateField("IntegerField", dbInteger)
  23.         .Fields.Append .CreateField("DateField", dbDate)
  24.     End With
  25.  
  26.     dbsNorthwind.TableDefs.Append tdfNew
  27.  
  28.     Debug.Print "Properties of new Fields in " & tdfNew.Name
  29.  
  30.     ' Enumerate Fields collection to show the properties of 
  31.     ' the new Field objects.
  32.     For Each fldLoop In tdfNew.Fields
  33.         Debug.Print "  " & fldLoop.Name
  34.  
  35.         For Each prpLoop In fldLoop.Properties
  36.             ' Properties that are invalid in the context of
  37.             ' TableDefs will trigger an error if an attempt
  38.             ' is made to read their values.
  39.             On Error Resume Next
  40.             Debug.Print "    " & prpLoop.Name & " - " & _
  41.                 IIf(prpLoop = "", "[empty]", prpLoop)
  42.             On Error GoTo 0
  43.         Next prpLoop
  44.  
  45.     Next fldLoop
  46.  
  47.     ' Delete new TableDef because this is a demonstration.
  48.     dbsNorthwind.TableDefs.Delete tdfNew.Name
  49.     dbsNorthwind.Close
  50.  
  51. End Sub
  52.  
  53.  
Dec 4 '07 #2
missinglinq
3,532 Expert 2GB
As evidenced by Coach's answer, it can be done, but that doesn't necessarily mean it should be done! Why do you want to do this?

Welcome to TheScripts!

Linq ;0)>
Dec 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: GL | last post by:
Hi, Is there a way to add a field to an existing table using a query of some sort (without needing to manually add a field to the table). I know how to do it with a make table query, but I have...
0
by: Scott Van | last post by:
Hello- I am trying to create an Access table using DDL that has a memo field. I have the syntax to create the memo field but I can't find the syntax to also set whether the memo field should...
1
by: shinde.shailesh | last post by:
Hi All, I have an xls file which contains data in one column. Also Having .MDB file with Table name "TableTest" and one field "Tabletst". I ahve to extract data from xls to MDB using Macro. ...
0
by: pmc1 | last post by:
Hi, I have a table (tblResults) that was created uning a make table query. I will be using the table to export the results of the query to a csv file. Every field in the first 2 columns of...
2
by: musa.biralo | last post by:
Hi, i want to read each row of specific column of musa.dbf using macro (VBA) in excel. my main gole is to copy the data of dbf file to excel file. I am new but want to do something..i tried and...
2
by: clevelander | last post by:
Hi, Is there a way to add a permanent field to a table using a macro? I have a number of different tables that I need to add the same field name to, then I update that field based on info in the...
0
by: bimeldip | last post by:
Hi, I have managed to create codes to display data from the database in a html page.I have gone on to create a page to allow users to manipulate the table via a html page. For instance users will be...
6
by: =?Utf-8?B?RGFu?= | last post by:
I am reposting a question from about 3 weeks ago ("sorting capability"). I have an aspx page in which I get the data from a database dynamically, through C# code, by creating a dynamic table...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.