Locking records in back-end using relationship in frontend - vba

I am trying to figure out how to protect records in access back-end with a relationship in access front-end.
I have the following table in back-end:
tblSit(linked from back-end)
tblSitID(autonumber) ProductID LocationID
1 1 2
2 5 1
3 8 3
temp_tblToMove(table in front-end)
temp_tblToMoveID(autonumber) tblSitID
1 1
2 3
what I want to do is move the product from one location to another. The idea is:
I select the record in tblSit that stores locations for each product. Then I insert that ID in temp_tblToMove local table. Then I have a form that in the end will delete the selected records from tblSit and insert them again in tblSit changing LocationID.
I want record locking so that if two users try to move the same product then they get error when trying to delete the record from tblSit.
if I would have temp_tblToMove in back-end then the relationship would prevent record deleting. But I'd like to keep temp_tblToMove in front-end, but here the ralationship doesn't include "Enforce referential integrity".
Thanks for the help.
PS: sorry if I didn't do a good job at explaining what I want.

Any reason you can't just update the existing records?
UPDATE tblSit SET Location = NewLocationID WHERE ID = WhicheverID;

Related

Handling CustomField Insert - Which option is efficient and easier to maintain

We have the following Table structure:
User Table
UserId CompanyId FullName Email
1 1 Alex alex#alex.com
2 1 Sam sam#sam.com
3 2 Rohit rohit#rohit.com
CustomField Table
CustomFieldId CompanyId Name Type
1 1 DOB Datetime
2 1 CompanySize Number
3 2 LandingPage Text
CustomFieldValue Table
UserId CustomFieldId DatetimeValue NumberValue TextValue
1 2 01-01-2020
1 2 10
1 3 Home
2 1
2 2 20
2 3 Product
Please consider the following facts:
There are millions of users in a particular CompanyId
When displaying a particular user in the UI we need to show all the Custom Fields that an end customer can fill up.
How to handle CustomFieldValues table in this case? We are considering the following options
When a new CustomField row is created for a particular CompanyId have a After Insert Trigger to create all corresponding rows in CustomFieldValue table for all users.
This I think would have an initial cost of creating so many rows for each Custom Field in the CustomFieldValue Table. (This may also lock up the table and users of the application would have to wait till all the inserts are done).
Same issue for deleting all CustomFieldValue rows when a CustomField row is deleted from a Company
But easier for UI and backend developers as they don't need to worry about whether a CustomFieldValue doesn't have an entry for a Custom Field that has been created for a Company
Don't create CustomFieldValue rows when a CustomField is added to the Company. Create the CustomFieldValue whenever user fills up the relevant input field in the UI view
This would have negligible insert cost and users would not have to wait for insert or delete to complete in CustomFieldValue table for all the users in a particular company.
The downside is that developers would have to make sure that relevant CustomFields are displayed in the frontend even though no relevant records yet exist in the CustomFieldValue table.
On each Custom Field input update by the end user, the developers would have to first check if a corresponding CustomFieldValue row exits, if so - store the updated value, if not - create the CustomFieldValue row.
Kindly suggest a solution which is efficient and easier to maintain.

I want to joint multiple ID'S in my SQL table

I have a table when my database Users and which one has an ID, and i want to say like ID 1 and 2 belong to the same Project, i cannot do that, what i can do i say that ID 1 bellong to this project but i really need to add multiple ID'S to the project, if anyone could help me please.

Not Equal / Exist in Access Database

So I am unable to wrap my brain around this.
I have 2 Tables in my database with matching fields in it
Table1: ComplaintsLogged ( This table contains all the data and all records)
Table2: ComplaintsClosed (This table only contains the complaints that have been closed)
Now I am trying to run a query which can be represented something like this.
ComplaintsLogged.status - ComplaintsClosed.taskStatus
Subtract the closed status in ComplaintsClosed from ComplaintsLogged and show me the rest of the records in complaintsLogged
both have a common field of complaint No. in them.
This is one way of doing what you asked
select ComplaintsLogged.id from ComplaintsLogged
where not exists (select 1 from ComplaintsClosed
where ComplaintsClosed.id = ComplaintsLogged.id)

How to merge two identical database data to one?

Two customers are going to merge. They are both using my application, with their own database. About a few weeks they are merging (they become one organisation). So they want to have all the data in 1 database.
So the two database structures are identical. The problem is with the data. For example, I have Table Locations and persons (these are just two tables of 50):
Database 1:
Locations:
Id Name Adress etc....
1 Location 1
2 Location 2
Persons:
Id LocationId Name etc...
1 1 Alex
2 1 Peter
3 2 Lisa
Database 2:
Locations:
Id Name Adress etc....
1 Location A
2 Location B
Persons:
Id LocationId Name etc...
1 1 Mark
2 2 Ashley
3 1 Ben
We see that person is related to location (column locationId). Note that I have more tables that is referring to the location table and persons table.
The databases contains their own locations and persons, but the Id's can be the same. In case, when I want to import everything to DB2 then the locations of DB1 should be inserted to DB2 with the ids 3 and 4. The the persons from DB1 should have new Id 4,5,6 and the locations in the person table also has to be changed to the ids 4,5,6.
My solution for this problem is to write a query which handle everything, but I don't know where to begin.
What is the best way (in a query) to renumber the Id fields also having a cascade to the childs? The databases does not containing referential integrity and foreign keys (foreign keys are NOT defined in the database). Creating FKeys and Cascading is not an option.
I'm using sql server 2005.
You say that both customers are using your application, so I assume that it's some kind of "shrink-wrap" software that is used by more customers than just these two, correct?
If yes, adding special columns to the tables or anything like this probably will cause pain in the future, because you either would have to maintain a special version for these two customers that can deal with the additional columns. Or you would have to introduce these columns to your main codebase, which means that all your other customers would get them as well.
I can think of an easier way to do this without changing any of your tables or adding any columns.
In order for this to work, you need to find out the largest ID that exists in both databases together (no matter in which table or in which database it is).
This may require some copy & paste to get a lot of queries that look like this:
select max(id) as maxlocationid from locations
select max(id) as maxpersonid from persons
-- and so on... (one query for each table)
When you find the largest ID after running the query in both databases, take a number that's larger than that ID, and add it to all IDs in all tables in the second database.
It's very important that the number needs to be larger than the largest ID that already exists in both databases!
It's a bit difficult to explain, so here's an example:
Let's say that the largest ID in any table in both databases is 8000.
Then you run some SQL that adds 10000 to every ID in every table in the second database:
update Locations set Id = Id + 10000
update Persons set Id = Id + 10000, LocationId = LocationId + 10000
-- and so on, for each table
The queries are relatively simple, but this is the most work because you have to build a query like this manually for each table in the database, with the correct names of all the ID columns.
After running the query on the second database, the example data from your question will look like this:
Database 1: (exactly like before)
Locations:
Id Name Adress etc....
1 Location 1
2 Location 2
Persons:
Id LocationId Name etc...
1 1 Alex
2 1 Peter
3 2 Lisa
Database 2:
Locations:
Id Name Adress etc....
10001 Location A
10002 Location B
Persons:
Id LocationId Name etc...
10001 10001 Mark
10002 10002 Ashley
10003 10001 Ben
And that's it! Now you can import the data from one database into the other, without getting any primary key violations at all.
If this were my problem, I would probably add some columns to the tables in the database I was going to keep. These would be used to store the pk values from the other db. Then I would insert records from the other tables. For the ones with foreign keys, I would use a known value. Then I would update as required and drop the columns I added.

How do create table with dynamic no. of columns for data entry

I'm currently working on a project where I need to save data depending upon the active no. of items.
Its like..
Stores can have n no of items which can be dynamically added or reduced from admin back-end. Each item can either be activated or deactivated. NO problem.
But I need to manage/save these dynamic n no. of items in a table for each store. I'm not able to decide on the table schema.
Any help will be highly appreciated.
I suggest a standard many-to-many relationship using a middle table. So you would use 3 tables:
StoresTable: the list of stores
ItemsTable: the list of items
StoreItemsTable: a list of items-in-stores, each row will have a foreign key to both the stores table and the items table
Hope that helps.
your problem is actually not that hard if you use a different approach.
A store can have a number of items. So basically Susi's store has 1 item, but suddenly she wants 2, and you would like to add a column. This is very difficult if she suddenly wants to add 2000 items.
The best approach would be to use a store table (which has the name of the store, the date it was created and a primary key) and a items table. You can then add items as entries to the table and link them to the store via the stores primary key.
An example:
Store table:
PK Name Owner
1 Sunshine Store Susi
2 Moonstore Harald
Item table:
PK Name Store_id Price
1 Candle 1 2.44
2 Table 1 51.44
3 Chair 2 6.55
This allows you to add as many items to any store you want. The Store_id is called a Foreign Key in this example, because it links the items to the store. You can then use SQL commands to select the items, e.g.
"Select * from ITEMS where Store_id = 1;"
and get all of Susi's items as an answer.
Good luck!
i think you use active column and use that active bit for every purpose because that's very good for future in other transaction too.