473,396 Members | 2,002 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,396 software developers and data experts.

How Do I Append a Field?

i wrote this code. it runs fine until the last light where a msg box says "cannot define field more than once".

Have i typed in some wrong code?



Dim rst As DAO.Recordset
Dim tbl As DAO.TableDef
Dim db As DAO.Database
Dim fld As DAO.Field

Set db = CurrentDb
Set tbl = db.TableDefs("Pol")



Set fld = tbl.CreateField("fundbalhk", dbDouble)
fld.Properties("Required").Value = False
tbl.Fields.Append fld



Thank you!!
Nov 27 '08 #1
3 2003
Stewart Ross
2,545 Expert Mod 2GB
Hi. It's what you do with your code that is wrong, not the code itself. You can't add the same field 'fundbalhk' twice to the same table ('pol' in this case). Your code as it stands will only work if table 'pol' does not have field 'fundbalhk' already defined.

And even if that is so, you can run your code just once - because after that a field of that name will exist in your table.

It kind of begs the question of why you are trying to add fields in code this way, where the table name and field name etc. are hard-coded into your VBA - makes no sense to me. Far quicker to add the field using the Table Design dialogue - unless you are intending to generalise the routine to be able to add fields of differing names to differing tables at will.

-Stewart
Nov 27 '08 #2
missinglinq
3,532 Expert 2GB
@Stewart Ross Inverness
And that idea also begs the question "Why?" I've always thought this idea of adding things on the fly, be they fields or textboxes or whatever, generally smacks of very poor database design! And how are you going to populate this field you've added thru code? You have no controls on forms to take care of the job. Are you going to try to add a control on a form on the fly as well?

Maybe if you could give us your reason for wanting to do this we could give you a better alternative.

Linq ;0)>
Nov 27 '08 #3
ADezii
8,834 Expert 8TB
After taking into consideration the expert advice from both linq and Stewart, if you reaaaaally need to create this Field on the fly for whatever reason, one fix would be:
Expand|Select|Wrap|Line Numbers
  1. Dim rst As DAO.Recordset
  2. Dim tbl As DAO.TableDef
  3. Dim db As DAO.Database
  4. Dim fld As DAO.Field
  5. Dim intNumFields As Integer
  6. Dim intCounter As Integer
  7.  
  8. Set db = CurrentDb
  9. Set tbl = db.TableDefs("Pol")
  10.  
  11. intNumFields = tbl.Fields.Count
  12.  
  13. For intCounter = 0 To intNumFields - 1
  14.   If tbl.Fields(intCounter).Name = "fundbalhk" Then Exit Sub
  15. Next
  16.  
  17. Set fld = tbl.CreateField("fundbalhk", dbDouble)
  18. fld.Properties("Required").Value = False
  19.  
  20. tbl.Fields.Append fld
Nov 27 '08 #4

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

Similar topics

9
by: JMCN | last post by:
hi- i have inherited an access 97 database that keeps track of the loans. i have been running into referential intergrity problems when i try to append new loans to table. first of all is a...
1
by: Jason | last post by:
I've got two different tables with similar data but the fieldnames are different and laid out slightly different. Is there a quick way to build an append query where maybe I could draw lines...
2
by: Danny | last post by:
I want to extract a subset of fields from one table into another the master table has many fields the subset has about half, but still many. Is there a way I can just append the master into the...
1
by: Aaron | last post by:
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; ...
1
by: David Barger | last post by:
Greetings, It appears that an Append Query I run in Access XP is randomly failing to append a field. I have payroll data being entered into a payroll database. This data is exported daily to...
2
by: Ray Holtz | last post by:
I have a form that shows a single record based on a query criteria. When I click a button it is set to use an append query to copy that record to a separate table, then deletes the record from the...
5
by: Michael C via AccessMonster.com | last post by:
Hello, I have a table that I am appending 3 seperate tables into. My main problem is that each time I append the data, it simply adds to the data already there. That might sound ok, except that...
3
by: a_masselink | last post by:
It doesn't append anything to the primary key (autonumber, long integer), only to all the other fields. Even if I set all the fields to allow zero length: yes, indexed: no and required no. All the...
4
by: dougmeece | last post by:
Morning Everyone... I have a table that needs to be append to and also updated. All the fields in the table are populated with data from the text boxes and combo boxes on a form. The Date...
1
by: sparks | last post by:
We have 3 databases that are just copies of the master. I have no idea why this is but I need to put them all in one. They contain a main table and 2 subtables linked one to many. I am sure that...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.