Results 1 to 4 of 4

VBA to add 2 fields to an existing table (Access 2007)

Threaded View

  1. #1
    Registered User
    Join Date
    08-31-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    87

    VBA to add 2 fields to an existing table (Access 2007)

    I need help with the code below. I would like to add 2 extra fields to an existing table in Access.

    I am using Access 2007.

    First field is the Order Period field. It will be in a text format. Examples of the records in the Order Period field would be jun-12, jul-12, aug-12, may-12 (month-year). I want it to be automatically filled with the current month and current year of the above format when I run the macro.

    Second field is the Order Period Date field. It will also be in a text format. Examples of the records here would be 7/1/2012, 8/1/2012, 9/1/2012. It will always be in the 1st of each month in the current year. I want it to be automatically filled with the current month and current year of the above format when I run the macro.

    First field will be places in the third column and second field will be in the fourth column.

    Can anyone help me fix this code?

    Thanks!

    Private Sub AddColumn()
    Dim curDatabase As Database
    Dim tblTooAddToo As TableDef
    Dim colFullName1 As Field
    Dim colFullName2 As Field
    
    ' Get a reference to the current database
    Set curDatabase = CurrentDb
    
    ' Get a reference to a table named TestTable - NOTE: The table MUST exist
    Set tblTooAddToo = curDatabase.TableDefs("Order data")
    
    
    'define the fields using the CreateField method
    ' Syntax: .CreateField("FIELD NAME", TYPE, [Length])
    ' I used the predefined types already in Access
    Set colFullName1 = tblTooAddToo.CreateField("Order Period", dbText, 12)
    Set colFullName2 = tblTooAddToo.CreateField("Order Period Date", dbDate / Time, 12)
    
    'use the APPEND method to add the fields to the table
    With tblTooAddToo.Fields
    .Append colFullName1
    .Append colFullName2
    End With
    
    End Sub

    Cross Post here: http://forums.aspfree.com/microsoft-...le-535740.html
    Last edited by ewong; 09-10-2012 at 11:49 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1