473,378 Members | 1,422 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,378 software developers and data experts.

Add a new field in every table in a database (100 tables)

Hi All,

I inherited an old database. Not designed too well. Nearly 100 tables with different types of names. Some table names start with and "_", some with "tbl" and some just names.

I want to add a new field called "TimeStamp", date/time, and default value Now() to all the tables. Any pointers would be of help.


Thanks

Regards

Raghu
Apr 16 '15 #1

✓ answered by Seth Schrock

If you are looking for a VBA approach, then it should be fairly simple to loop through all your tables and add the field that you want then.

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim td As DAO.TableDef
  3. Dim fl As DAO.Field
  4.  
  5. Set db = CurrentDb()
  6.  
  7. For Each td In TableDefs
  8.     Set fl = td.CreateField("TimeStamp", dbDate)
  9.     fl.DefaultValue = "=Now()"
  10.     td.Fields.Append fl
  11. Next

6 2446
Seth Schrock
2,965 Expert 2GB
If you are looking for a VBA approach, then it should be fairly simple to loop through all your tables and add the field that you want then.

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim td As DAO.TableDef
  3. Dim fl As DAO.Field
  4.  
  5. Set db = CurrentDb()
  6.  
  7. For Each td In TableDefs
  8.     Set fl = td.CreateField("TimeStamp", dbDate)
  9.     fl.DefaultValue = "=Now()"
  10.     td.Fields.Append fl
  11. Next
Apr 16 '15 #2
It is even trying to add the field to the MSys tables too. Otherwise this is good.

Thank you Seth Schrock
Apr 17 '15 #3
zmbd
5,501 Expert Mod 4TB
Enclose lines 8 thru 10 in conditional if-then
check left-4 of name for MSYS Ucase(left(td.name,4))<>"MSYS" and if not equal then change the table otherwise go to the next object.
Apr 17 '15 #4
Hi All this is working fine. I am able to amend the tables by adding a field.

How do I put default value as Now() and caption as "Time Stamp"

Expand|Select|Wrap|Line Numbers
  1. Public Sub AddDate2AllTbls()
  2. Dim tdf As TableDef
  3. Dim fld As DAO.Field
  4. Dim strTableName As String
  5. For Each tdf In CurrentDb.TableDefs   'scan each tbl
  6.  
  7.    strTableName = tdf.Name
  8.  
  9.        If Left(strTableName, 4) = "~TMP" Then GoTo SkipTable
  10.        If Left(strTableName, 4) = "ztbl" Then GoTo SkipTable
  11.        If Left(strTableName, 4) = "MSys" Then GoTo SkipTable
  12.        If Left(strTableName, 4) = "Usys" Then GoTo SkipTable
  13.        If Left(strTableName, 2) = "f_" Then GoTo SkipTable
  14.  
  15.    tdf.Fields.Append tdf.CreateField("TimeStamp", dbDate)
  16. '  set defaultvalue = "=Now()"
  17. '  set caption = "Time Stamp"
  18.  
  19. SkipTable:
  20. Next
  21. Set tdf = Nothing
  22. MsgBox "done"
  23. End Sub
  24.  
  25.  

Thanks you
Apr 21 '15 #5
zmbd
5,501 Expert Mod 4TB
Go back to Seth's code,
Note how the field is created in lines 8 thru 10.
That is how you will need to set the default.

Not sure what you are referring to as "Field Caption"
Apr 21 '15 #6
twinnyfo
3,653 Expert Mod 2GB
hrprabhu,

Are you trying to add the value of Now() to the field as well? That would require accessing each table and cycling through all records.

Modifying Seth's code:

Expand|Select|Wrap|Line Numbers
  1. Dim db As DAO.Database
  2. Dim td As DAO.TableDef
  3. Dim fl As DAO.Field
  4.  
  5. Set db = CurrentDb()
  6.  
  7. For Each td In TableDefs
  8.     If Not (Left(strTableName, 4) = "~TMP" Or _
  9.         Left(strTableName, 4) = "ztbl" Or _
  10.         Left(strTableName, 4) = "MSys" Or _
  11.         Left(strTableName, 4) = "Usys") Or _
  12.         Left(strTableName, 2) = "f_" Then
  13.  
  14.         Set fl = td.CreateField("TimeStamp", dbDate)
  15.         fl.DefaultValue = "=Now()"
  16.         fl.Caption = "Time Stamp"
  17.         td.Fields.Append fl
  18. Next
should do the trick.

Hope this hepps!
Apr 29 '15 #7

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

Similar topics

7
by: Marco Simone | last post by:
Hi, What is your opinion about using Lookup field in table. I would like to use lookup field in table 1, so that I can choose data in combo box from table 2. Is this good design of database? ...
6
by: Lewis Veale | last post by:
I have an Access 2000 front-end pointing at a SQL Server backend, with around 80 linked tables and views. I frequently need to point the front-end at different versions of the back-end, and achieve...
0
by: Bennett Haselton | last post by:
It seems whenever I add a new field to a database table that's queried by DataAdapters in my project, I get run-time errors because I don't go back and re-generate all the DataAdapters to read the...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
1
by: Magnus | last post by:
I'm testing walkthrough saving data to a Database (Multiple Tables). http://msdn2.microsoft.com/en-us/library/4esb49b4(VS.80).aspx In the famous Customer/Order example, I'm getting referential...
4
by: kcopson | last post by:
I need a routine to add a new field to an existing table in a database that is in another town. Situation: I maintain a database with 10 copies in different offices. Everytime we want to make a...
16
by: Charles A. Landemaine | last post by:
I set a table with 100% width to occupy all available space, but in IE7 it uses more than that, it uses 100% of the page width instead of 100% of the table container. Could you test the page in IE7...
48
by: Scott Deming | last post by:
Ok here's the deal. I'm in Afghanistan and attempting to create an Access database for my pilot's to debrief on when they come back from a mission. Included fields would be things like names,...
10
by: Diplodok | last post by:
My question is closed to remove replication data from my database. For that I create new database and write vba code for transfer all objects from old replicable database, except tables. For tables...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.