I'm trying to add a column of the type NUMBER to my table using VBA. That is working for me. But i also want to set the default value for the column to 0, which is not working properly. I believe i need some help.
I do not want to alter a column, i want to add a column and at the same time set a default value.
The following code is working, it adds a column with nothing in it.
strSQL = "ALTER TABLE testTable ADD COLUMN testColumn NUMBER;"
dbs.Execute strSQL
But this is not working and gives an error.
strSQL = "ALTER TABLE testTable ADD COLUMN testColumn NUMBER DEFAULT 0;"
dbs.Execute strSQL
The error message i get is "Run-time error'3293': Syntax error in ALTER TABLE statement."
Default only works when it's used in DDL executed from an ADO connection. The syntax for this, which I have tested would be this:
CurrentProject.Connection.Execute "ALTER TABLE testTable ADD COLUMN testColumn NUMBER"
CurrentProject.Connection.Execute "ALTER TABLE testTable ALTER COLUMN testColumn SET DEFAULT 0"
Also, this only sets the default for the table for all new records, and does not modify existing records to 0. If that is also your intention, simply run an update query, like this one.
strSQL = "UPDATE testTable SET testTable.testColumn = 0;"
CurrentDb.Execute strSQL
Related
I have a function that has to change a table field into a BIT type and i also want to set its control display property to listbox, so i tried like this but don't know how to continue..
Dim dbs As Database
Set dbs = CurrentDb
dbs.Execute "ALTER TABLE tblStore " _
& "ALTER COLUMN Storage BIT"
I've looked around online but can't find any clear answers for this. Is it possible to set the starting value of an Auto Increment field to a variable (example to follow) in MS Access VBA?
Private Sub Command10_Click()
Dim dbs As Database
Set dbs = CurrentDb
Dim tblTempMinID As String
tblTempMinID = "DMax(EntryID, tblCalendar) + 1"
dbs.Execute "ALTER TABLE tblTemp ALTER COLUMN EntryID AUTOINCREMENT(tblTempMinID)"
End Sub
I'm currently getting an error in field definition with the above code. Still doing research, but I figured it wouldn't hurt to put this out here.
Assuming you want to increment by 1:
dbs.Execute "ALTER TABLE tblTemp ALTER COLUMN EntryID AUTOINCREMENT(" & tblTempMinID & ",1)"
I am fairly new to using VBA in MS Access and I am having trouble embedding SQL statements into VBA code. I have a database with almost 200 tables, and I would like to change the data type of one column (named lake) in each table to a "text" data type. I wrote the following code, but keep getting a syntax error for the ALTER TABLE statement.
Public Sub changeDataType()
Dim db As DAO.Database
Dim table As DAO.TableDef
Set db = CurrentDb
For Each table In db.TableDefs
DoCmd.RunSQL "ALTER TABLE" & table.Name & "ALTER COLUMN [lake] TEXT(100);"
Next
Set db = Nothing
End Sub
Can anyone tell me why I'm getting the syntax error?
Thanks,
Paul
this statement will not be correct:
DoCmd.RunSQL "ALTER TABLE" & table.Name & "ALTER COLUMN [lake] TEXT(100);"
if, for instance "table.Name" = "myTable", the resulting statement will look like this:
DoCmd.RunSQL "ALTER TABLEmyTableALTER COLUMN [lake] TEXT(100);"
try adding a space to separate the name, like this:
DoCmd.RunSQL "ALTER TABLE [" & table.Name & "] ALTER COLUMN [lake] TEXT(100);"
I'm using MS Access 2010 and I'm trying to create tables using SQL. I would like to put in some check constraints but I'm having some troubles:
CREATE TABLE Test (
tester Text CHECK (tester IN ('ABC', 'BCD', 'CDE'))
);
I'm getting a syntax error,
Any suggestions?
Thank you!
EDIT: Sorry if I wasn't clear. What I would like actually is to CHECK that tester is either "ABC", "BCD" or "CDE" those are the only values he can have.
EDIT2: I tried something else:
CREATE TABLE Test (
tester Text NOT NULL,
CONSTRAINT m_pk PRIMARY KEY(tester),
CONSTRAINT check_tester CHECK (DATALENGTH(tester) > 2)
);
and I also get a syntax error. Is there something I'm really not understanding with checking Text values? I can't possibly see where either of these is wrong.
Beginning with Jet 4, CHECK contraints are supported for Access DDL executed from ADO, but not from DAO.
You can execute a single DDL statement which creates the table Test with your constraint. You don't need to execute one statement to create the table and then another to add the constraint.
CREATE TABLE Test
(
tester TEXT(255),
CONSTRAINT ABC_or_BCD_or_CDE CHECK
(
tester IN ('ABC', 'BCD', 'CDE')
)
);
I formatted it that way to make it easier to examine the pieces. You could use this VBA to execute the statement:
strSql = "CREATE TABLE Test ( tester Text(255)," & vbCrLf & _
"CONSTRAINT ABC_or_BCD_or_CDE" & vbCrLf & _
"CHECK ( tester IN ('ABC', 'BCD', 'CDE')));"
Debug.Print strSql
CurrentProject.Connection.Execute strSql
Notes:
CurrentProject.Connection is an ADO object, so its .Execute method succeeds. The same statement with CurrentDb.Execute (a DAO method) would fail.
With ADO, declaring a field as TEXT without including a length (tester TEXT instead of tester TEXT(255)) will give you a memo field.
You will need to run against a connection:
ssql = "CREATE TABLE Test (tester Text)"
CurrentProject.Connection.Execute ssql
ssql = "ALTER TABLE test ADD CONSTRAINT " _
& "myrule CHECK (tester IN ('ABC', 'BCD', 'CDE'))"
CurrentProject.Connection.Execute ssql
Or
sSQL = "CREATE TABLE Test (tester Text, " _
& "CONSTRAINT myrule CHECK (tester IN ('ABC', 'BCD', 'CDE')))"
Note that the name, myrule in this case, must not already exists, even for a different table.
Some notes: Is it possible to create a check constraint in access and/or DAO?
My bad,
It appears MS Access does not allow CHECK Constraints except for primary and foreign keys. My teacher taught her course with Oracle until last year and apparently did not see that this could not be done in MS Access.
Possible Duplicate:
DEFAULT clause in ALTER TABLE statement resulting in syntax error
I am trying to execute the following statement using a SQL query within MS Access;
ALTER TABLE [table] ALTER COLUMN [column] SET DEFAULT 'default value'
However, I get a dialog displaying the error Syntax error in ALTER TABLE statement.
And when I click OK it highlights the word DEFAULT. I also tried the following statement;
ALTER TABLE [table]
ADD CONSTRAINT [Default] DEFAULT 'default value' FOR [column]
And I get another error Syntax error in CONSTRAINT clause.
What is the correct syntax for setting a default value in MS Access? The db file is Access 2003 format.
Support for DEFAULT was included in Access DDL with Jet 4 (Access 2000). However it can only be used in DDL executed from an ADO connection.
This worked with Access 2007.
CurrentProject.Connection.Execute "ALTER TABLE MyTable " & _
"ALTER COLUMN field2 SET DEFAULT ""foo"";"
Note if your db file is Access 97 or earlier, you won't be able to set a field DEFAULT value from DDL.
It seems, there would be Constraint issue with your column. Although following DDL statement is the correct way.
ALTER TABLE Persons ALTER COLUMN City SET DEFAULT 'SANDNES'
Reference