Solved

How do you add a field to a MS Access table with a default value in VBA

Posted on 2012-03-23
5
317 Views
Last Modified: 2012-03-26
Hi,  

I need to add a currency field (Freight) to my table (tbl_Quotes).  I want to add a default value of 0 in the field so that it is not null.  This needs to be done in VBA code.  I'm somewhat familiar with the Alter Table command but do not know how to add a default value.

Thanks,
0
Comment
Question by:Marilync1266
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 37757984
You have to execute the sql through an ado connection,  which you can do just with ...

CurrentProject.Connection.Execute  "ALTER TABLE tablename ADD COLUMN columnname SHORT DEFAULT 0"
0
 

Author Comment

by:Marilync1266
ID: 37758401
How do I reference a database outside of the run where I'm running the code?
0
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 37765534
You can use DAO for this as well.  This function would add the field "Amount" to a table named "tbl_Debits" and set the default value to 0

Public Sub AddField()

    Dim db As DAO.Database
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
   
    Set db = DBEngine.OpenDatabase(FullPathAndNameOfExternalDatabase)
    Set tdf = db.TableDefs("tbl_Debits")
   
    Set fld = tdf.CreateField("Amount", dbCurrency)
    fld.DefaultValue = 0
   
    tdf.Fields.Append fld
   
    set fld = nothing
    set tdf = nothing
    db.Close
    Set db = Nothing
   
End Sub
0
 

Author Comment

by:Marilync1266
ID: 37768028
Great, this worked!
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 37768080
Glad to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

751 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