gumuk
12-17-2007, 06:57 AM
High all.
I would like to know how to set a fields default value on a table.
Eg - i have a Yes/No field that and i want the default value to be set to 'Yes'.
I cant use the front end application because the form its on is a generic form used by about 30 other tables.
At present it is not set to anything and so always defaults to 'No' on the form.
Thanks in advance.
boblarson
12-17-2007, 06:59 AM
If you open the table, you can go down to the properties and find DEFAULT and just change it from blank to YES (no quotes or anything).
gumuk
12-17-2007, 07:02 AM
Yeh, that much i know.
But we have approx 200 clients, we have a DBChange application that we ship to clients that creates or updates tables. This table already exists so i am simply adding a new field to it, but i need to programaticaly know how to set the default value after the field has been added.
boblarson
12-17-2007, 07:07 AM
You add the default the same way I just said. If you want to make all of the existing entries turn to yes (which seems odd), or a select number of them, then you would just use an update query to update the field after you have entered it. But you would still need to do what I said to set the actual default of the field. All new records would then be able to use it.
DJkarl
12-17-2007, 07:20 AM
How are you adding the field, are you manually adding the field to the table, or are you programatically adding using a query or tabledef?
gumuk
12-17-2007, 07:27 AM
programatically, using tbldefs.
The DBChange app adds a Boolean field to the table and then i want to set its default value to -1
Never had to do it before cos usually the form would handle this, but as the form is generic i can use that method, which is why i want to set it in the table.
DJkarl
12-17-2007, 07:39 AM
programatically, using tbldefs.
The DBChange app adds a Boolean field to the table and then i want to set its default value to -1
Never had to do it before cos usually the form would handle this, but as the form is generic i can use that method, which is why i want to set it in the table.
Ok well if you are using tabledefs you are probably using CreateField to add new fields I'm guessing. If you are setting a field object one of the properties of the field object is DefaultValue.
I haven't used tabledefs in a while so I can't write out the code for you, however a quick way to do this in SQL is this:
Currentproject.Connection.Execute "ALTER TABLE [YourTableName] ALTER COLUMN [YourColumnName] LOGICAL DEFAULT TRUE"