473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to use VBA to remove attribute dbAutoIncrField?

TheSmileyCoder
2,322 Expert Mod 2GB
Im trying to convert an existing autonumber field into a numeric long field.

How can I do this in VBA?

What I have tried:
Expand|Select|Wrap|Line Numbers
  1. Dim DB As dao.Database
  2.     Set DB = CurrentDb()
  3.     Dim tblDef As dao.TableDef
  4.     Set tblDef = DB.TableDefs("hist_Tbl_Obs")
  5.     Dim f As dao.Field
  6.     Dim p As Property
  7.     For Each f In tblDef.Fields
  8.         If f.Attributes And dbAutoIncrField Then
  9.             Debug.Print f.Name
  10.             'f.Attributes = dbFixedField Or dbUpdatableField
  11.             'f.Attributes = dbNumeric
  12.             'f.Attributes=0
  13.             'f.Type = dbNumeric
  14.             'f.Properties(dbAutoIncrField) = False
  15.         End If
  16.     Next
  17.  
  18. 'Cleanup
  19.     Set f = Nothing
  20.     Set tblDef = Nothing
  21.     Set DB = Nothing
  22.  
The commented lines all fail with error 3219: Invalid Operation, with the exeption of the last one, which has no effect.
Jan 4 '12 #1

✓ answered by ADezii

@TheSmileyCoder:
Simply Pass to this Function a Table Name and it will:
  1. Determine the Name of an AutoNumber Field, if one exists.
  2. Convert that AutoNumber Field to a LONG INTEGER Data Type.
  3. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fModifyFieldType(strTableName As String)
    2. Dim strSql As String
    3. Dim db As DAO.Database
    4. Dim tdf As DAO.TableDef
    5. Dim fld As DAO.Field
    6.  
    7. Set db = CurrentDb()
    8. Set tdf = db.TableDefs(strTableName)
    9.  
    10. For Each fld In tdf.Fields
    11.   If (fld.Attributes And dbAutoIncrField) <> 0 Then     'Yep, an AutoNumber Field
    12.     strSql = "ALTER TABLE " & strTableName & " ALTER COLUMN " & fld.Name & " LONG;"
    13.       DBEngine(0)(0).Execute strSql, dbFailOnError
    14.         Exit For
    15.   End If
    16. Next
    17.  
    18. Set fld = Nothing
    19. Set tdf = Nothing
    20. Set db = Nothing
    21. End Function
  4. Sample Function Call:
    Expand|Select|Wrap|Line Numbers
    1. fModifyFieldType("Table1")
  5. Critical Points to remember:
    1. The Code WILL work if the AutoNumber Field is also the Primary Key, and NOT involved in any Relationships.
    2. The Code WILL NOT work if the AutoNumber Field is also the Primary Key, and IS involved in any Relationship.
  6. 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 :
Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 5 '12 #2
TheSmileyCoder
2,322 Expert Mod 2GB
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.
Jan 5 '12 #3
ADezii
8,834 Expert 8TB
@TheSmileyCoder:
Simply Pass to this Function a Table Name and it will:
  1. Determine the Name of an AutoNumber Field, if one exists.
  2. Convert that AutoNumber Field to a LONG INTEGER Data Type.
  3. Function Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Function fModifyFieldType(strTableName As String)
    2. Dim strSql As String
    3. Dim db As DAO.Database
    4. Dim tdf As DAO.TableDef
    5. Dim fld As DAO.Field
    6.  
    7. Set db = CurrentDb()
    8. Set tdf = db.TableDefs(strTableName)
    9.  
    10. For Each fld In tdf.Fields
    11.   If (fld.Attributes And dbAutoIncrField) <> 0 Then     'Yep, an AutoNumber Field
    12.     strSql = "ALTER TABLE " & strTableName & " ALTER COLUMN " & fld.Name & " LONG;"
    13.       DBEngine(0)(0).Execute strSql, dbFailOnError
    14.         Exit For
    15.   End If
    16. Next
    17.  
    18. Set fld = Nothing
    19. Set tdf = Nothing
    20. Set db = Nothing
    21. End Function
  4. Sample Function Call:
    Expand|Select|Wrap|Line Numbers
    1. fModifyFieldType("Table1")
  5. Critical Points to remember:
    1. The Code WILL work if the AutoNumber Field is also the Primary Key, and NOT involved in any Relationships.
    2. The Code WILL NOT work if the AutoNumber Field is also the Primary Key, and IS involved in any Relationship.
  6. The Code has been tested, and is fully functional. Any other questions, feel free to ask.
Jan 5 '12 #4
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.
Jan 6 '12 #5
TheSmileyCoder
2,322 Expert Mod 2GB
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.
Jan 6 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
3
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...
0
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...
6
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: ...
6
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: ...
4
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
2
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,...
0
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;}"); ...
11
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...
2
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.