Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-23
5
Medium Priority
?
319 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 48

Accepted Solution

by:
Dale Fye earned 2000 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 48

Expert Comment

by:Dale Fye
ID: 37768080
Glad to help.
0

Featured Post

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 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