0

I am trying to change the default value of a field I just added through VBA. I have this line of code below that I added in to do so but for some reason it is saying the item is not in the collection and I am really confused as to why. Any help would be greatly appreciated!

AddColumn = "ALTER TABLE ShouldImportMetricsIDsTable " & _
"ADD COLUMN [ImportStatus] TEXT"

db.Execute AddColumn

CurrentDb.TableDefs("ShouldImportMetricsIDs").Fields("ImportStatus").DefaultValue = "No"

I have also included the line that adds the field.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pablo
  • 93
  • 3
  • 16

2 Answers2

3

You just pointed to different tables in ALTER TABLE you referenced ShouldImportMetricsIDsTable and in VBA code ShouldImportMetricsIDs.

P.S. DefaultValue property is build in so you can easily set its value

CurrentDb.TableDefs("ShouldImportMetricsIDsTable").Fields("ImportStatus").DefaultValue = "No"

but some other field properties are odd so you have to use constructions like that:

Dim p As Property
Set p = CurrentDb.TableDefs("ShouldImportMetricsIDsTable").Fields("ImportStatus").CreateProperty("DefaultValue", dbText, "No")
CurrentDb.TableDefs("ShouldImportMetricsIDsTable").Fields("ImportStatus").Properties.Append (p)
4dmonster
  • 3,012
  • 1
  • 14
  • 24
  • Thank you! This did change the default property but this table is a copy of another so there are already records in it. How would I get all the existing records to be no as well? – Pablo Dec 22 '14 at 19:40
  • Execute UPDATE query with `WHERE [ImportStatus] is null` or `WHERE [ImportStatus]=''` – 4dmonster Dec 22 '14 at 19:42
1

I think you can add the default value to the ALTER TABLE expression:

CurrentProject.Connection.Execute "ALTER TABLE ShouldImportMetricsIDsTable " & _
"ADD COLUMN [ImportStatus] TEXT" & _
"DEFAULT ""No"";"

I'm basing my answer off this question: SQL SET DEFAULT not working in MS Access

EDIT: Looking at the answer from HansUp in that link, I edited the answer above to reflect the syntax he uses there. I hope this works for you.

Community
  • 1
  • 1
Access_Query
  • 99
  • 1
  • 11