Solved

Default values for Access fields

Posted on 1998-08-27
4
159 Views
Last Modified: 2010-08-05
Is there any way to set the default value of a field when you use the ALTER TABLE command to create new fields on an Access table? Currently it defaults to null, but I need it to be zero.
0
Comment
Question by:BergJC
  • 2
4 Comments
 
LVL 1

Expert Comment

by:DennisH
ID: 1431350
Would it be possible to use DAO for this?  It's a lot easier that way.  Then after you create the fields in the table, you can select the Field object from the Fields collection of the table and set it's Default property.  
0
 
LVL 2

Author Comment

by:BergJC
ID: 1431351
I don't know if DAO would work...could you show me how this could be done?
0
 
LVL 9

Accepted Solution

by:
Dalin earned 100 total points
ID: 1431352
BergJC,
Try this:

Dim MyDB As Database
Dim tdfMyTable As TableDef

Set MyDB = OpenDatabase("YourDBNAmeWithPath")
Set tdfMyTable = MyDB.TableDefs!YourTableNAme

tdfMyTable.Fields!PostalCode.DefaultValue = "0" ' Or 0 without quote, depend on how you define it

MyDB.Close
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1431353
You should replace "PostalCode" with your fieldName
    tdfMyTable.Fields!YourFieldNAmeHere.DefaultValue = 0 ' assume it is a number field
Regards
Dalin
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

789 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