473,725 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code to add a field to multiple tables in design mode

I have a need to add another field to all of my tables (over 150). Not data,
but an actual field.

Can I code this somehow. So the code presumabley would loop through all the
tables, open each table in design mode and then add the new field and set its
properties.

Thanks.

--
Message posted via http://www.accessmonster.com

Dec 24 '06 #1
5 4097
Hi.
I have a need to add another field to all of my tables (over 150).
Whoa! You need to add a column (presumably a foreign key) to more than 150
tables in your database? This sounds like a design problem. (One that you're
going to create for yourself, not one you're trying to work your way out of.)
Perhaps you can explain what it is that needs to be added to the present design
now that the organization's needs have changed since the original design was
implemented, and we can possibly offer other suggestions.
Can I code this somehow.
Yes. It's very easy. Each table needs a table lock while it's being modified,
so other users should be out of the database while this code runs to prevent
locking problems. But first let's discuss _why_ so many of these tables need a
new column, instead of only a half dozen or so tables needing a foreign key to
relate to a new table's primary key.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/ex...ributors2.html for contact info.
"rdemyan via AccessMonster.c om" <u6836@uwewro te in message
news:6b37ec654f 421@uwe...
I have a need to add another field to all of my tables (over 150). Not data,
but an actual field.

Can I code this somehow. So the code presumabley would loop through all the
tables, open each table in design mode and then add the new field and set its
properties.

Thanks.

--
Message posted via http://www.accessmonster.com

Dec 24 '06 #2
Okay, but remember you asked for it :).

I'm creating my own table synchronization and opting out of Access
replication and syncing. Let's not get into why I want to do this, but I
have considered the problem carefully and feel comfortable that I can come up
with something that will work for my unique situation.

I'm going to take data that has been updated in the tables of a remote user
and write it to a parallel temporary database. The records to write will be
the incremental updates/additions. This small temporary database then can
be quickly file copied to a server and dropped in a folder for later updating
of the server backend file (homegrown indirect syncing).

The problem is deletions. So I thought I would add a field to the tables in
the temp database that indicates whether the record in any table is to be
added/edited or deleted from the corresponding table when this temp database
data is imported into the server backend.

I'm concerned about the fact that the tables in the small temp updateDB will
be different from the corresponding tables in the server backend by this one
field that indicates if the record should be added/updated or deleted.

Because I have so many tables, I want to be able to loop through the
tabledefs and use Select* or not have to specify each field specifically to
retrieve/update the records from any table so that I can do the Joins, etc.
I don't want the fact that the tables have one column difference to screw up
my ability to use Select * or not specify the fields specifically.

As an example of what I'm talking about

UPDATE table1
INNER JOIN table1 ON table1.PK = table2.PK
SET table1.field = table2.field2
WHERE table1.field <table2.field 2

I'm not sure if I'm going to be trying to link tables from the the files or
if I'll have to specify the files names in the action query (I don't know how
yet and not even sure if I can), but this is just an example. What happens in
this action query when table2 has that extra field that table1 doesn't??

Maybe things won't get screwed up anyway, but I'm not sure because I've never
done this before. So I thought I would pose this question.

Thanks.

'69 Camaro wrote:
>Hi.
>I have a need to add another field to all of my tables (over 150).

Whoa! You need to add a column (presumably a foreign key) to more than 150
tables in your database? This sounds like a design problem. (One that you're
going to create for yourself, not one you're trying to work your way out of.)
Perhaps you can explain what it is that needs to be added to the present design
now that the organization's needs have changed since the original design was
implemented, and we can possibly offer other suggestions.
>Can I code this somehow.

Yes. It's very easy. Each table needs a table lock while it's being modified,
so other users should be out of the database while this code runs to prevent
locking problems. But first let's discuss _why_ so many of these tables need a
new column, instead of only a half dozen or so tables needing a foreign key to
relate to a new table's primary key.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/ex...ributors2.html for contact info.
>I have a need to add another field to all of my tables (over 150). Not data,
but an actual field.
[quoted text clipped - 4 lines]
>>
Thanks.
--
Message posted via http://www.accessmonster.com

Dec 24 '06 #3
Hi.
Okay, but remember you asked for it :).
Yeah. But only _after_ you asked for a lot of trouble by trying to replace
replication with your own home grown data sync system. I will start off by
saying this is not a good idea, even for people with plenty of experience with
SQL. I don't want to be mean to you, but I have to tell you this. You aren't
there yet. The only way to get there is for you to do exercises just like this
one, until you become experienced. That's why I'm not going to lead you down
the garden path and give you the solution right away. I'll point out the
mistakes in your theories and your queries, make suggestions, and give you
example queries that are used in similar (though not identical) situations.
The problem is deletions. So I thought I would add a field to the tables in
the temp database that indicates whether the record in any table is to be
added/edited or deleted from the corresponding table when this temp database
data is imported into the server backend.
Oh, boy. Let's come back to this idea later. (NOTE: I wouldn't design it like
that.)
I'm concerned about the fact that the tables in the small temp updateDB will
be different from the corresponding tables in the server backend by this one
field that indicates if the record should be added/updated or deleted.
You don't need to worry about this because each column will need to be named
explicitly to update it with the SET keyword in the query.
Because I have so many tables, I want to be able to loop through the
tabledefs and use Select*
Sorry. Not gonna happen. Can't "SET * = SELECT * FROM . . . " You can append
records with "SELECT * " but updating records doesn't work with that type of
syntax.
UPDATE table1
INNER JOIN table1 ON table1.PK = table2.PK
SET table1.field = table2.field2
WHERE table1.field <table2.field 2
You have a typo. The JOIN should be on Table2, not Table1 joining itself.
Also, I can see that you didn't test this air code out, because once you fix the
table name, you'll find that only the records in both tables that have the same
primary key will be updated from the new record to the baseline record. If the
new table (table2) has any additional records, they'll be ignored. That's why
an INNER JOIN won't work. You need an OUTER JOIN.

The following is an example where the baseline table, tblMembers, is being
updated by records in a linked table, tblMembers_Link . Both tables have a
compound primary key, MbrName and UserID. Sometimes the columns in these tables
have zero-length strings (not a good design practice, but that's what we get
with legacy data sometimes). Because of the way this query is written, existing
records in the baseline table will be updated with the new data (if there is
any), and any new records in tblMembers_Link will be appended to the baseline
table. Therefore, it's an update _and_ an append query all rolled into one.

UPDATE tblMembers AS BL RIGHT JOIN tblMembers_Link AS NGR
ON (BL.MbrName = NGR.MbrName) AND (BL.UserID = NGR.UserID)
SET BL.UserID = IIF((ISNULL(NGR .UserID) OR (NGR.UserID = "")), BL.UserID,
NGR.UserID),
BL.MbrName = IIF((ISNULL(NGR .MbrName) OR (NGR.MbrName = "")), BL.MbrName,
NGR.MbrName),
BL.LastUpd = IIF((NGR.LastUp d NZ(BL.LastUpd, #1/1/1900#)), NGR.LastUpd,
BL.LastUpd);

I want you to build a query that updates one of your baseline tables with your
linked UpdateDB table of the same structure. Put some sample data into both
tables and test your query until you are satisfied that running the query will
update the baseline and add new records from the UpdateDB table. When you're
confident with your query, you can post it here.

Why am I making you do the work? Because you need the practice and you need to
work out some of the gotchas yourself if you're going to become good enough with
SQL to pull off tasks like this successfully without help. Replication is much
more complex than it looks. You will have to build 150+ update queries like
this one. You will also have to build 150+ delete queries. You need to
consider whether or not record conflicts have to be dealt with, and be able to
carry this out on 150+ tables.

Honestly, I'd recommend you stick with built-in replication instead of building
your own. But there are other alternatives to replication. Have you considered
Terminal Server? How about a Web application for the offsite personnel?
Perhaps Sharepoint Services?

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/ex...ributors2.html for contact info.
"rdemyan via AccessMonster.c om" <u6836@uwewro te in message
news:6b3957e2aa 0a8@uwe...
Okay, but remember you asked for it :).

I'm creating my own table synchronization and opting out of Access
replication and syncing. Let's not get into why I want to do this, but I
have considered the problem carefully and feel comfortable that I can come up
with something that will work for my unique situation.

I'm going to take data that has been updated in the tables of a remote user
and write it to a parallel temporary database. The records to write will be
the incremental updates/additions. This small temporary database then can
be quickly file copied to a server and dropped in a folder for later updating
of the server backend file (homegrown indirect syncing).

The problem is deletions. So I thought I would add a field to the tables in
the temp database that indicates whether the record in any table is to be
added/edited or deleted from the corresponding table when this temp database
data is imported into the server backend.

I'm concerned about the fact that the tables in the small temp updateDB will
be different from the corresponding tables in the server backend by this one
field that indicates if the record should be added/updated or deleted.

Because I have so many tables, I want to be able to loop through the
tabledefs and use Select* or not have to specify each field specifically to
retrieve/update the records from any table so that I can do the Joins, etc.
I don't want the fact that the tables have one column difference to screw up
my ability to use Select * or not specify the fields specifically.

As an example of what I'm talking about

UPDATE table1
INNER JOIN table1 ON table1.PK = table2.PK
SET table1.field = table2.field2
WHERE table1.field <table2.field 2

I'm not sure if I'm going to be trying to link tables from the the files or
if I'll have to specify the files names in the action query (I don't know how
yet and not even sure if I can), but this is just an example. What happens in
this action query when table2 has that extra field that table1 doesn't??

Maybe things won't get screwed up anyway, but I'm not sure because I've never
done this before. So I thought I would pose this question.

Thanks.

'69 Camaro wrote:
>>Hi.
>>I have a need to add another field to all of my tables (over 150).

Whoa! You need to add a column (presumably a foreign key) to more than 150
tables in your database? This sounds like a design problem. (One that you're
going to create for yourself, not one you're trying to work your way out of.)
Perhaps you can explain what it is that needs to be added to the present
design
now that the organization's needs have changed since the original design was
implemented , and we can possibly offer other suggestions.
>>Can I code this somehow.

Yes. It's very easy. Each table needs a table lock while it's being
modified,
so other users should be out of the database while this code runs to prevent
locking problems. But first let's discuss _why_ so many of these tables need
a
new column, instead of only a half dozen or so tables needing a foreign key to
relate to a new table's primary key.

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/ex...ributors2.html for contact info.
>>I have a need to add another field to all of my tables (over 150). Not
data,
but an actual field.
[quoted text clipped - 4 lines]
>>>
Thanks.

--
Message posted via http://www.accessmonster.com


Dec 24 '06 #4
nice one Clart

Heres the code using DAO (not ADO)
You may need to reference DAO 3.6 if not already done to get this working,
but its simple.

The error handler will cope with the system objects, which cant add a field
to.
NOTE: Backup your database first.....

Stick this into a module
'###########
Sub subAddFieldToEv eryTable()
On Error Resume Next
Dim db As Database
Dim tdf As TableDef

Set db = CurrentDb
For Each tdf In db.TableDefs
tdf.Fields.Appe nd tdf.CreateField ("MyNewField ", dbText, 50)
db.TableDefs.Ap pend tdf
Next
Set tdf = Nothing
Set db = Nothing
End Sub
'###########
"rdemyan via AccessMonster.c om" <u6836@uwewro te in message
news:6b37ec654f 421@uwe...
>I have a need to add another field to all of my tables (over 150). Not
data,
but an actual field.

Can I code this somehow. So the code presumabley would loop through all
the
tables, open each table in design mode and then add the new field and set
its
properties.

Thanks.

--
Message posted via http://www.accessmonster.com

Dec 24 '06 #5
Thanks!

tombsy wrote:
>nice one Clart

Heres the code using DAO (not ADO)
You may need to reference DAO 3.6 if not already done to get this working,
but its simple.

The error handler will cope with the system objects, which cant add a field
to.
NOTE: Backup your database first.....

Stick this into a module

'###########
Sub subAddFieldToEv eryTable()
On Error Resume Next
Dim db As Database
Dim tdf As TableDef

Set db = CurrentDb
For Each tdf In db.TableDefs
tdf.Fields.Appe nd tdf.CreateField ("MyNewField ", dbText, 50)
db.TableDefs.Ap pend tdf
Next
Set tdf = Nothing
Set db = Nothing
End Sub
'###########
>>I have a need to add another field to all of my tables (over 150). Not
data,
[quoted text clipped - 7 lines]
>>
Thanks.
--
Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200612/1

Dec 26 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6216
by: Rick Caborn | last post by:
Does anyone know of a way to execute sql code from a dynamically built text field? Before beginning, let me state that I know this db architecture is built solely for frustration and I hope to make it better soon. Unfortunately, there is never a non-crucial time in which we can do an upgrade, so we are stuck for now. Point 1: There are multiple tables: students, courses, cross-reference
45
3608
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
26
14138
by: pb648174 | last post by:
I have a table called BidItem which has another table called BidAddendum related to it by foreign key. I have another table called BidFolder which is related to both BidItem and BidAddendum, based on a column called RefId and one called Type, i.e. type 1 is a relationship to BidItem and type 2 is a relationship to BidAddendum. Is there any way to specify a foreign key that will allow for the different types indicating which table the...
8
20351
by: Vladimir | last post by:
Hello, I have a table in MS Access database. It has one field (with BYTE datatype) that has several properties set in Lookup tab of table Design View. Display Control = Combo Box. Row Source Type = Value List. Row Source = "1; "Above"; 2; "Below"; 3; "Equal"". When I try to SELECT <field> FROM <table> in my C++ application through ADO, I get numeric value of the field. How can I get string representation of this numeric value from the...
13
2914
by: Peter | last post by:
Can anyone tell me how to change the data type of a field in a table created with a make table query? The field is a binary and must be changed to text. alternately does anyone know how to specify the field type when running a make table query? Thanks, Sven
4
1485
by: Wayne | last post by:
I'm trying to understand at what point the use of code tables contributes to the efficient running of a database when weighed up against the added work of implementing them in data entry and queries. For instance if I have a lookup table called "tblRisk" which has one field called "Risk" containing the values "High", "Medium" and "Low" is it better design to have a second field in the table called "RiskCode" with the numbers 1, 2 and 3...
18
18408
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on a remote update of tables and fields and can't find enough information on these things. Also, how do you index a field in code?
3
1771
by: silvastein | last post by:
It would be great if someone can help me with this. It seems like it should either be easy (so far, not!) or maybe it isn't possible. Here goes: Table B has TextField1 that looks up values from a single field in Table A. When I create a form for filling in Table B, TextField1 appears as a long list of options from Table A. If I were to make the field really tall, it would show multiple rows of data from Table A. I would prefer...
4
2661
by: Don Do | last post by:
Help I built a form/subform/subsubform setup using the access forms wizard. I have a table1 = parent, table2 = child, table3 = (grandchild?). There will be multiple records in table2 that tie to table1, and multiple records in table 3 that tie to table2. Both on "1 to many" joins. Each of the forms are bound to the fields in the respective tables. The subform & subsubform are datasheet view. When I enter any data, I
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.