Solved

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

Posted on 2012-03-23
5
316 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
  • 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

Industry Leaders: 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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

680 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