Query to change the default column value in a table

I have an Access 2003 Database in which I would like to run a query to change the default values for several columns within a table to NULL. Is it possible to write a query without VBA to do so?

I created a new query went to the SQL view and entered in the following:

ALTER TABLE My_Table ALTER COLUMN My_Column LONG DEFAULT NULL

Access lets me save the query; however, it appears to do nothing when run.

FYI: I already removed the relationships regarding this column.
LVL 1
solaris01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Try running that from the VBA Immediate Window ...

mx
0
Jinesh KamdarCommented:
>> however, it appears to do nothing when run

I guess that statement sets the default value as NULL for all INSERTs performed after running it, not before it.
Do not expect any of the existing values in that column to change to NULL, that won't happen I believe.
After running this query, if u do an INSERT, it will set the default value for this column to NULL and not the one that was defined at table-creation time.
0
solaris01Author Commented:
I apologize it did not let me save the query:

ALTER TABLE My_Table ALTER COLUMN My_Column LONG DEFAULT NULL

It did let me save and execute the following query and I saw the expected result:
ALTER TABLE My_Table ALTER COLUMN My_Column LONG

I guess Access does not like the DEFAULT keyword. So I guess I'm looking for the correct SQL syntax to use with Access to set the default value in the ALTER COLUMN statement .
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Jinesh KamdarCommented:
ALTER TABLE My_Table ALTER COLUMN My_Column LONG would itself take care of the DEFAULT value since none is specified, i guess. Doesn't it?
0
solaris01Author Commented:
jinesh_kamdar:

I had previous set it up with a default value of 0 now I need to change it to NULL so I can enforce referential integrity on the column.
0
Jinesh KamdarCommented:
Try this.

ALTER TABLE My_Table ALTER COLUMN My_Column LONG DROP DEFAULT
0
solaris01Author Commented:
jinesh_kamdar:

I tried your suggestion in the last post and it didn't work. Your somewhat right though. If I could just drop my current default value of 0 it would default to null; however, the SQL you have provided does not do that.

From the query paine within Access if you were to click "New Query", go into the SQL view, then enter that SQL it will tell you there is a syntax error. This error prevents the query from being saved or executed.

Thanks for your suggestion anyhow and I would be pleased to try anymore you may have.

0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
solaris - did you try my post.  That can only be executed in VBA
0
Eric ShermanAccountant/DeveloperCommented:
solaris ....

I ran into this many many moons ago and spent a lot of time trying to accomplish what you are trying to do with the ALTER TABLE statement all in SQL and setting the DEFAULT value of the field ....  "To No Avail on an Access Table".  I should say Access 2k table as that what I was working with at the time.

Here is an easy way to handle that.  Below I am setting the TRACK_POOL field to Bit in the POOL_DATA table and then setting the default to True (-1).  I alter the table first then modify the default value in a separate line of code using the CurrentDb.TableDefs.

    Dim strSQL As String
    strSQL = "ALTER TABLE  POOL_DATA ALTER COLUMN TRACK_POOL YESNO;"
    CurrentDb.Execute strSQL
    CurrentDb.TableDefs("POOL_DATA").Fields("TRACK_POOL").DefaultValue = -1


With all the reasearch I did I never found a way "That Works" to do that in one Alter Table statement on a Access table.  If you do I would like to know.

ET
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
solaris01Author Commented:
If you were to look in the msdn knowlege base it show the syntax for doing an Alter Column with a default value; however, it complains of a syntax error. The method you provided with get me going. Thanks!
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Like I said ... you have to run that SQL in code :-)

mx
0
solaris01Author Commented:
DatabaseMX:

I did try your solution and thank you for your post. It just appears that Access will not accept the following SQL nomatter where it is comming from (Access API or VBA). It complains of a syntax error on the 'DEFAULT' keyword despite the fact that Microsoft's MSDN page show the usage of the exact syntax.

ALTER TABLE table1 ALTER COLUMN column1 INT DEFAULT NULL

Anyhow,
Thanks you and everyone else for your responses.
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
np

MSDN .... not ...... always reliable :-(

mx
0
Eric ShermanAccountant/DeveloperCommented:
As I posted ....

Been there & done that!!!!!  I must have wasted a week off and on trying to get that to work ...  To No Avail!!!

ET

What's up today MX????

Quite a Profile you have there.  I noticed you are a very talented musician .....  but ...  you are missing something from the list!!!!

No Zydeco on your list!!!  What's with that???  :-)

I grew up on that in So. Louisiana (Let the good times roll!!!)

ET
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Zydeco is cool.  In fact, some of my musician colleagues in San Diego have a Zydeco band ... and I sit in occasionally, although I'm not in SD any longer, but I still do treasurer and webmaster for www.blusd.org

mx
0
Eric ShermanAccountant/DeveloperCommented:
Thanks and a nice site.  I didn't know San Diego had a presence on the Blues Scene.  I always think of everything on the West Coast as surf boards and the Beach Boys.

I will check it out!!!

ET
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
"San Diego had a presence on the Blues Scene"
Yes ... a common misconception, lol.

International Blues Challenge (IBC)
2006 2nd place in Band category
2007 1st place in Single/Duo category
... as in the World.
Sadly this year, our 2006 2nd place finisher went again this year, but did not make the finals.

mx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.