tags:

views:

261

answers:

4

In an access table I have a column which has the "Required" property set to "True". I need a query which would change it to "False". I tried the following without success:

ALTER TABLE [MyTbl] ALTER COLUMN [MyCol] VARCHAR(30) NULL;
+3  A: 

Jet SQL, the underlying SQL engine in Access does not allow you to modify the null property on columns. The work around for doing this is to:

  1. Create a new temporary column (B) which allows nulls.
  2. Copy the data from the old column (A) to the new column (B).
  3. Drop the old column (A).
  4. Create a new column (C) with the same name as the old column (A). Make sure the new column (C) has the correct null constraint.
  5. Copy the data back from the temporary new column (B) to the newly added column (C).
  6. Drop Column B.
tschaible
+1. Been there, done that. Horrible, horrible engine.
tomfanning
tschaible...pretty sure you answer is plain wrong. See my answer below.Seth
Seth Spearman
-1 "Jet SQL, the underlying SQL engine in Access does not allow you to modify the null property on columns" is false.
onedaywhen
+1 I tested this again and seems I was wrong. Indeed can't be done with SQL DDL.
onedaywhen
+1  A: 

I am pretty sure that the tschaibles answer given above is COMPLETELY INCORRECT. It IS true that you cannot change the required property using the query engine. But I am almost positive you can change it using VBA.

Your question is confusing...could you clarify a few things.

Do you want to permanently changed the required property. If so, you can change that property in the table designer at any time.

Do you want to change the property under certain conditions at runtime? Sort of a Change the property, Do Something, Change it back scenario. In that case you can't use the query engine to do it but I am almost positive you can do it through VBA. So it can be done. Most if not all things that can be done through the UI can be done through the VBA code.

If, you want to do this at runtime but you are merely using an Access file as the data store of a .net application (using the OleDB provider of ado.net) then you do have an issue. I would recommend you use another data provider (like vistaDB). So can you provide some details then I might can dig a little and give you a more thorough answer.

Seth

Seth Spearman
Thanks Seth. I cannot use VBA or user interaction. The query should be part of an upgrade script which is executed automatically by an application.
Tihauan
I think you are out of luck completely. I am pretty sure that using ADO.NET there is no way of doing ddl statements against an Access database. Yuck. If you could guarantee that access was installed you could use ole automation to fire up access and do it. But you have no control over the target environment or know that access is not and will not be installed then I am pretty sure there is nothing you can do.Seth
Seth Spearman
Why can't you use VBA? I use DAO collections all the time to update/add tables, fields, indexes and relationships.
Tony Toews
Tony,Wouldn't access have to be installed to do that?Seth
Seth Spearman
Seth, good point. I'm used to posters who have Access to be asking these kinds of questions. <smile>
Tony Toews
No. DAO is installed on every version of Windows starting with 2000, because it's the native interface for Jet, which is a Windows component.
David-W-Fenton
-1 "It IS true that you cannot change the required property using the query engine" is false ;)
onedaywhen
David, I know dao is installed...the issue is, is vba installed. This question was vague on the details. If the questioner has complete control over installation of his app then no problem. If not then this matters a great deal.onedaywhen. Do you have a code example or another question you have answered that shows using JET (or some other NATIVELY INSTALLED LIBRARY) to make ddl changes on an access database. (Or I guess changing the required property?) I am not disputing your answer, I merely would like to know how to do this?Seth
Seth Spearman
As for Jet being installed on every version of windows. That is true...but there is NO 64 bit version of jet so only 32 bit apps can use it. I mention that as an interesting aside. And microsoft has no plans to do the port to 64-bit. Does not bode well for jets future. Seth
Seth Spearman
@Seth: I edited this answer to add links to an old article on SQL DDL for the Access database engine.
onedaywhen
@Seth: then I tested it again just to be sure and, what do you know, it indeed isn't possible with SQL DDL. Sorry for the time waste.
onedaywhen
I am very surprised to learn that Jet DOES support some DDL constructs. I never new that. HEre is an article. http://technet.microsoft.com/en-us/library/cc966376.aspx.
Seth Spearman
A: 

Try referenceing ADOX in your application (.NET ?). ADOX On the Table object you can set the attribute to Nullable (You'll have to look into the new conventions in .NET compared to VB 6 which handled this similar to VBA).

Jeff O
+3  A: 

Jet DDL will allow you to change the "required" property from False to True. The following statement changed "required" to True:

CurrentProject.Connection.Execute _
    "ALTER TABLE People ALTER COLUMN lname TEXT(255) NOT NULL"

However, I couldn't find a way to change "required" from True to False. This statement did not cause an error message, but left the "required" property unchanged (True):

CurrentProject.Connection.Execute _
    "ALTER TABLE People ALTER COLUMN lname TEXT(255) NULL"

This syntax is only supported when using the Access database engine's ANSI-92 Query Mode. The Access object model's CurrentProject.Connection object is an ADODB.Connection object using the OLE DB provider and OLE DB always uses ANSI-92 Query Mode.

HansUp
Thanks for the edit, ODW. I realized ADO was proficient with DDL, but didn't understand the particulars as to why.
HansUp