Solved

Default values for Access fields

Posted on 1998-08-27
4
153 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now