?
Solved

Query to change the default column value in a table

Posted on 2008-02-12
17
Medium Priority
?
2,627 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:solaris01
  • 6
  • 5
  • 3
  • +1
17 Comments
 
LVL 75
ID: 20878208
Try running that from the VBA Immediate Window ...

mx
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20878506
>> 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
 
LVL 1

Author Comment

by:solaris01
ID: 20878635
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20878738
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
 
LVL 1

Author Comment

by:solaris01
ID: 20878939
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
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20879129
Try this.

ALTER TABLE My_Table ALTER COLUMN My_Column LONG DROP DEFAULT
0
 
LVL 1

Author Comment

by:solaris01
ID: 20879373
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
 
LVL 75
ID: 20879412
solaris - did you try my post.  That can only be executed in VBA
0
 
LVL 19

Accepted Solution

by:
Eric Sherman earned 200 total points
ID: 20879546
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
 
LVL 1

Author Closing Comment

by:solaris01
ID: 31430301
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
 
LVL 75
ID: 20880119
Like I said ... you have to run that SQL in code :-)

mx
0
 
LVL 1

Author Comment

by:solaris01
ID: 20880129
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
 
LVL 75
ID: 20880166
np

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

mx
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 20880312
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
 
LVL 75
ID: 20880362
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
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 20880402
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
 
LVL 75
ID: 20880423
"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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Blockchain technology enhances society similar to the Internet. Its effects are broad, disruptive, and will boost global productivity.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

601 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