MS-Access (2007) updating one (or two) related tables from another table/query - ms-access-2007

Query1 contains the records I would like to insert in the related tables Categorie and Sub_Categorie. How do I do that.
This is MS-ACCESS 2007.
I would like to update the categorie. categorie from query1.cat (only if there is a new category to be inserted). The query1.s_cat and query1.label need to be inserted into the subcategory table If that combination, label & subcategory is new to the corresponding category. Of course integrity has to be maintained.

Related

SSAS - Cannot create relationships due to dupicate column entries

I am currently building a tabular cube in SSAS, however I'm having issues when creating relationships.
I have 3 tables
Master
Supplier
Customer
In the master table I have a list of unique IDs, these ID's appear in the other 2 tables (there can be duplicate records for the ID in the Supplier and Customer table).
What I'm tring to do is create a relationship between the Master table and the Supplier and Customer table. One to Many. But SSAS gives me an error The relationship cannot be created because each column contains duplicate values. Select at least one column that contains only unique values.
Any advise would be greatly appreciated
Based on the error message it seems to be there are some duplicate values in the master table within the column that is being selected for relationship.

Delete an item (row) from table but keep the unused ID number for a future insertion

I have a database with an "inventory" table which contains products from a shop. Each product is identified using an ID number set in the "ID" column of the table.
I want to be able to delete a product from the table, but keeping the deleted product's id number for future product insertions into the database.
As a demonstration I inserted 4 items and named all of them "test"
And just as an example I named the "deleted" product as "vacio" (empty in spanish) to show the one that i deleted.
Now, if want to add another product in the future, the id number 2 is unused and I want to add the product with that id number instead of 4 (following the given example).
The DELETE query is no good since it erases the id number as well so its a no go.
I thought about checking for the first row of the table that contains the value "vacio" and using the UPDATE query in all fields except id but this doesnt feel "classy" and is not very efficient as It should have to update values a lot of times.
Is there some nice way of doing this?
I would not actually recommend reusing old identifiers. For one, this prevents you from using the auto_increment feature, which mean that you need to manually handle the column for each and every insertion: this adds complexity and is not efficient. Also, it might cause integrity issues in your database if you have other tables referencing the product id.
But if you really want to go that way: I would go for the deletion option. If there are foreign keys referencing the column, make sure that they have the on delete cascade option enabled so data is properly purged from dependent tables when a product is dropped.
Then, you can fill the first available gap the next time your create a new product with the following query:
insert into products(id, categoria, producto)
select min(id) + 1, 'my new category', 'my new product'
from products p
where not exists (select 1 from products p1 where p1.id = p.id + 1)
You could have a new column ESTADO where you handle if a record is active (1) or inactive (0). Then, to obtain only "undeleted" records you just have to filter by the new column. That way, you also prevent changing the product name to "vacio", which might be useful in the future.

How do I move a field to a different table and maintain relationships?

I figure this has to be easy, I'm just not sure how to ask the question.
I have thousands of records I imported from a Excel Spreadsheet in a Microsoft Access table with a field that I want to extract into a new table. How do I move the data from the field in the existing table to a new table and maintain the relationships to the record?
The goal is to move the existing data from the one field into a new table that will have a one-to-many relationship with the existing parent table.
For example, in a table called tblProperties I have the following fields:
Property_Address | Property_Owner | UtilityMeter_Number
I want to maintain a history of utility meters on properties as they are replaced, so I want to move the UtilityMeter_Number field from tblProperties into a new table called tblMeters and create a one-many relationship between the two so I can have multiple meter records for each property record.
How do I move all the existing data from the UtilityMeter_Number field in tblProperties into tblMeters, and maintain the relationship?
What is what I'm trying to do called, and how do I do it?
This is called normalizing data structure.
Use a SELECT DISTINCT query to create unique records. Use that dataset as source to create a new table. Something like:
SELECT DISTINCT CustID, LName, FName, MName INTO Customers FROM Orders;
Now delete unnecessary LName, FName, MName fields from Orders table.
Tables are related on the common CustID fields. An autonumber primary key is not utilized. If you do want a relationship on autonumber PK, then continue with following steps:
add an autonumber field in new table
create a number field in original table
run an UPDATE action SQL to populate new number field with autonumber value from new table - join tables on common CustID fields
also delete CustID field from original table

How to get the name of a table in MS access?

I have one table that has a list of Facilities, then I have many other tables, one for each Facility with the equipment that is there.
I am trying to make a query that can bring all of these tables together. The problem is that equipment names can be repeated from Facility to Facility so I need to get the Facility name to be associated with the equipment to have unique records. I do not have the ability to edit these equipment tables so I can't just add a column to the table and the only place that the Facility name is referenced in the Equipment Tables is in the title of the Table itself.
Is there any way that I can link the Facility Table records to the names of the Equipment Tables?
The Tables are similar to this:
Facilities
Column1
EI-456
EI-497
EI-456
Column1
Pump1
Pump2
FT1
EI-497
Column1
TT1
Pump1
Riser1
Hopefully that makes it a little more clear.
That looks like terrible design. You should have:
a Facilities table
an Equipments table (optional, depending on your needs)
a Locations table (or Assignment or...) where you just store the FacilityId and the EquipmentId.
I suggest that you read a bit about database normalisation. That will really pay off in the long run.
What you're asking for can be done dynamically through code (vba, etc) using your row record value to populate column.name in a query string.
I'd run an initial query to determine the facility name and then do:
Dim sillyString as String
sillyString = .row(?).item("column") = "EI-456"
strSQL = "SELECT * FROM " & sillyString & "WHERE ..."

sql: update a given field in all tables instead of just one table

As per another question, sql: update a column if another column poses a conflict in namespace, we know that the following could be used to ensure that a set of 10000 unique package names and 100 categories, having 15000 unique combinations (thus table entries in table categories), could be updated to ensure that the two namespaces don't collide with one another (affecting about 10 entries in total):
UPDATE categories
SET fullpkgpath = fullpkgpath || ',pkg'
WHERE fullpkgpath IN (SELECT value
FROM categories)
However, the fullpkgpath field also repeats in other tables within my sqlite3 database, as per the schema.
Is there a way to have the above UPDATE statement applied to all other tables within a given sqlite3 database with the same fullpkgpath field, without having to manually specify any such extra tables?
If the answer to be above is, "no", then how would I manually specify which other tables I want the statement applied to? Consider that only the categories table has the categories that could be directly compared with names of packages (to be fair, the ports table also has a categories field, but it has all the categories of a given port jammed into one field (space separated), as opposed to separate table entries as is the case in the categories table).