Im trying to convert an existing autonumber field into a numeric long field.
How can I do this in VBA?
What I have tried: - Dim DB As dao.Database
-
Set DB = CurrentDb()
-
Dim tblDef As dao.TableDef
-
Set tblDef = DB.TableDefs("hist_Tbl_Obs")
-
Dim f As dao.Field
-
Dim p As Property
-
For Each f In tblDef.Fields
-
If f.Attributes And dbAutoIncrField Then
-
Debug.Print f.Name
-
'f.Attributes = dbFixedField Or dbUpdatableField
-
'f.Attributes = dbNumeric
-
'f.Attributes=0
-
'f.Type = dbNumeric
-
'f.Properties(dbAutoIncrField) = False
-
End If
-
Next
-
-
'Cleanup
-
Set f = Nothing
-
Set tblDef = Nothing
-
Set DB = Nothing
-
The commented lines all fail with error 3219: Invalid Operation, with the exeption of the last one, which has no effect.
@TheSmileyCoder:
Simply Pass to this Function a Table Name and it will: - Determine the Name of an AutoNumber Field, if one exists.
- Convert that AutoNumber Field to a LONG INTEGER Data Type.
- Function Definition:
-
Public Function fModifyFieldType(strTableName As String)
-
Dim strSql As String
-
Dim db As DAO.Database
-
Dim tdf As DAO.TableDef
-
Dim fld As DAO.Field
-
-
Set db = CurrentDb()
-
Set tdf = db.TableDefs(strTableName)
-
-
For Each fld In tdf.Fields
-
If (fld.Attributes And dbAutoIncrField) <> 0 Then 'Yep, an AutoNumber Field
-
strSql = "ALTER TABLE " & strTableName & " ALTER COLUMN " & fld.Name & " LONG;"
-
DBEngine(0)(0).Execute strSql, dbFailOnError
-
Exit For
-
End If
-
Next
-
-
Set fld = Nothing
-
Set tdf = Nothing
-
Set db = Nothing
-
End Function
- Sample Function Call:
- fModifyFieldType("Table1")
- Critical Points to remember:
- The Code WILL work if the AutoNumber Field is also the Primary Key, and NOT involved in any Relationships.
- The Code WILL NOT work if the AutoNumber Field is also the Primary Key, and IS involved in any Relationship.
- The Code has been tested, and is fully functional. Any other questions, feel free to ask.
5 5956 NeoPa 32,556
Expert Mod 16PB
You are looking to change the value of Attributes from an AutoNumber field which has the following two flags set - dbAutoIncrField (&H10) & dbFixedField (&H01), into a standard Long field which has only one flag set - dbFixedField (&H01). Essentially to turn of the dbAutoIncrField flag. This seems to be disallowed from my tests, but if it were allowed would look like : - f.Attributes = (f.Attributes And Not dbAutoIncrField)
Unfortunately, using the Watch Pane of the IDE I checked all properties of an AutoNumber field as well as those of a Long and compared them. Only the Attributes property was different (apart from obviously unhelpful ones such as the Name). I couldn't find a way of changing a field from AutoNumber to Long using VBA.
Well that is also what my own tests revealed, using the approach described.
Doing it manually through the standard access table design GUI, however seems so easy, that I thought it would be available somehow.
I guess I could write a VBA routine to add an extra field of type number,Long, and then update its values to the key column values, delete the key field, and rename the extra field added. I just prefer to avoid complicating matters if possible. :)
Thank you for your time.
@TheSmileyCoder:
Simply Pass to this Function a Table Name and it will: - Determine the Name of an AutoNumber Field, if one exists.
- Convert that AutoNumber Field to a LONG INTEGER Data Type.
- Function Definition:
-
Public Function fModifyFieldType(strTableName As String)
-
Dim strSql As String
-
Dim db As DAO.Database
-
Dim tdf As DAO.TableDef
-
Dim fld As DAO.Field
-
-
Set db = CurrentDb()
-
Set tdf = db.TableDefs(strTableName)
-
-
For Each fld In tdf.Fields
-
If (fld.Attributes And dbAutoIncrField) <> 0 Then 'Yep, an AutoNumber Field
-
strSql = "ALTER TABLE " & strTableName & " ALTER COLUMN " & fld.Name & " LONG;"
-
DBEngine(0)(0).Execute strSql, dbFailOnError
-
Exit For
-
End If
-
Next
-
-
Set fld = Nothing
-
Set tdf = Nothing
-
Set db = Nothing
-
End Function
- Sample Function Call:
- fModifyFieldType("Table1")
- Critical Points to remember:
- The Code WILL work if the AutoNumber Field is also the Primary Key, and NOT involved in any Relationships.
- The Code WILL NOT work if the AutoNumber Field is also the Primary Key, and IS involved in any Relationship.
- The Code has been tested, and is fully functional. Any other questions, feel free to ask.
NeoPa 32,556
Expert Mod 16PB Smiley:
I just prefer to avoid complicating matters if possible. :)
Makes perfect sense to me. There is an alternative, which is to use SQL, but it seems ADezii has beaten me to that and, as is quite unsurprising, has already knocked up some good code to illustrate the approach.
It's not strictly a VBA approach (although it is all managed within a VBA procedure), but I think we know by now that the strict VBA approach is not supported so this may well be an acceptable solution.
Thank you for your code Adezii.
If possible I would liked to do it entirely by VBA, but in reality its merely because I am mostly used to working in VBA, and not so much in SQL, and also because I wanted to make sure that it was not simply my lack of understanding or lack of correct method that was preventing me from using a pure VBA solution.
That said, the solution does cover my, shall we say REAL need, which is to be able to drop the autonumber attribute without having to do it manually.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Greg |
last post by:
Hi. I have a rather large xml document (object) that can have one or
more nodes with a certain attribute throughout (at ANY depth, not at
the same level necessarily). I need to find this...
|
by: Peter Seif |
last post by:
I have a PC that has windows 2000 Server ,Microsoft .Net Framework 1.0
installed
First time I tried to create a web application and debug it i had the error
"unable to debub ..." so i changed the...
|
by: Jonas |
last post by:
Hi!
I'm working with an ASP.NET web site that needs to conform with XHTML 1.0
Transitional and got an ASP.NET HtmlInputButton which automatically adds the
attribute language="javascript" which...
|
by: tshad |
last post by:
Is there a way during Page_Load to change or add an attribute to the Body
tag?
I want to be able to change the onLoad body attribute to do a focus on one
of my text boxes, such as:
...
|
by: Pete Davis |
last post by:
I'm fairly weak with ASP.NET. What I'm trying to do is pass an attribute to
a UserControl. The attribute is a value from the CodeBehind for the page.
So in my .aspx I have the following:
...
|
by: kevanbulmer |
last post by:
c#
by default the 'border' attribute is added to an asp.net image, how do
i remove this attribute, as i cant validate my page as xhtml 1.1
thanks
kb
|
by: Nicolas |
last post by:
Hi everybody...
In Xerces 2.7.0 it is explicitly stated that, no matter how one removes
an attribute
(attList->removeNamedItem, attList->removeNamedItemNS,
domElement->removeAttributeNode,...
|
by: Ismail |
last post by:
Guys,
I have the following code in page_load event of user control
Go.Attributes.Add("onclick","if(" + search.ClientID +
".value==''){alert('" + noSearchText + "');return false;}");
...
|
by: Richard Maher |
last post by:
Hi,
I have read many of the copius entries on the subject of IE performance (or
the lack thereof) when populating Select Lists.
I don't mind the insert performance so much, (I get 100x120byte...
|
by: Nathan Sokalski |
last post by:
I am attempting to create icons for controls I have created using VB.NET by
using the System.Drawing.ToolboxBitmap attribute. I have managed to do this
in C# by specifying the path to the *.ico...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |