Linked variables - if/then statements in Access - ms-access

Is there a way in Access to link two variables together across separate tables?
Ex. I have one variable in table A that says Consent1 was signed.
Table B will be a summary table showing which consents are needed. In other words, If Consent1 was signed in table A, table B will show Consent1 is NOT NEEDED.

You could build a trigger to update Table B, but if your database is normalized properly there's no need to do that. Just build a query that does the same thing. Join Table A to Table B linked on whatever common field they have (I can't tell you exactly how to do that since you've offered no table structure), and then add a field to the query that looks something like this:
Consent: IIF([Table A].Consent = "Signed", "Not Needed", "Needed")

Related

How do I create a table name in MySQL using a select?

I'm building a MySQL event to make a copy of a table in the database with a timestamp in the name.
CREATE TABLE `db_name`.`tbl_prefix_(SELECT TO_SECONDS(NOW()))` ( [the rest...]
Obviously this isn't working. What should I do to make it work?
Any suggestions are welcome.
Thanks
This is a bad architecture. Generating tables on the fly is not something you should do.
Instead, create a single table with a timestamp column. For instance, if you would before have 3 tables with three timestamps A, B, and C, you now have one table with a timestamp column containing the values A, B, and C, respectively.
In order to do this, you would need to use "dynamic SQL". That is, make use of the MySQL PREPARE statement.
What you'd need to do is populate a variable with a string that contains the SQL text you want to execute. Doing variable substitution into string is trivial.
The "trick" is to take that dynamic string and execute it like it was a SQL statement.
And that's what the PREPARE statement does for us, takes in a variable, and reads the contents of that variable like it were a SQL statement.
With that said, rather than give an example code that demonstrates this in more detail, I'm going to suggest that you re-think this idea of creating a table with timestamp value as part of the name.
What problem is that designed to solve? And carefully consider whether the proposed design for a solution will introduce a bigger problem than it solves.

MySQL Replace old ID with new one based on another table

Maybe my way of thought is wrong, so first off I will try to explain the situation in words.
I have a Database with about 40000 Titles, some of them are related to each other. This Relations are saved in a different database in a simple a b type manner. The problem is that the system was changed so there is a special row in the main database which contains the old id. And the b value is also the old id... However now I want to replace the b value with the new auto increment id given by the database for all the entries in the relations table. So I thought that I need to create a new table, copy the contents but replacing the b value with the new id...
CREATE TABLE list_relations_new LIKE list_listitem_relations;
INSERT list_relations_new
SELECT lr.a, li.id, lr.typeID
FROM list_listitems li
LEFT JOIN list_listitem_relations lr
ON li.oldID = lr.b
Executing this query doesn't give any errors, but also doesn't even create a new table? I hope somebody can help me to sort this out...

sync records of two tables in the same database in MYSQL

I have two tables with some same fields like:
Table A: fname, lname, address, age, email, mobile, website, blog
Table B: fname, lname, address, age, email
Both these tables are used by different modules on my website. I want to sync the first five fields of both tables in such a way that whenever a new row is added or an existing row is modified in Table A, the Table B is updated automatically and vice versa.
For Example.
A user created a new record in Table A. Now the Table B should also be updated with this new information. and vice versa if a user creates a new record in Table B, the Table A should also be updated with this new information.
A user modified a record in Table A. Now the Table B should also be updated with this modified information. and vice versa if a user modifies a new record in Table B, the Table A should also be updated with this modified information.
How can I achieve this. I thought of using triggers but would it not create an inifinite loop resulting is server error!
Is any field among those 5 guaranteed to be unique? You could add a conditional to the trigger to check to see if that field exists before inserting the record in the table.
You might want to rethink the design also. Storing duplicate records in 2 places seems a little scaring. You're going to have to have triggers for updates, inserts, and deletes.
If u just need to update one table in case the other table gets updated, Instead of creating a table (as a part of some other table), create a View which is also like a table but virtual (not real).
but since u've asked for both sides update.
What I believe is that you should go back little back of this problem....and tell us why u need to update both the tables according to the other table,,,
Because you are just keeping duplicate data at two places that is of no need.
So, try to think whether it can be done without creating two tables, or something like create one table and one view for partial columns requirement.
It is not an answer to your problem, but I am trying to solve your problem in an optimized way which is good for everyone's health....
Hope you understood what i tried to tell. :)

MS Access - how to create a label on a form that populates data from another table

I have a fairly simple database that I inherited. For the purposes of this question, there are two tables:
Mastertable and Providertable.
Mastertable references Providertable thruogh provid, which is a FK to Providertable PK (provid).
So it looks like this:
Mastertable:
acct (PK)
(other fields)
provid (FK)
Providertable
provid (PK)
provname
provspecialty
Simple right? However, the Mastertable!provid field is actually a lookup table which displays Providertable!provname but stores provid. There is a form the users use to populate the Mastertable, and it has this lookup field shown.
The users now want to show the provider specialty based on what they select as the provid. I can't figure this out to save my life. I'm pretty well versed in SQL, having written many stored procedures and created a few db apps using .NET, but this is quite challenging. I tried creating a lookup field called provspeciality, but that's not what they want. I tried changing the "OnUpdate" event for the lookup field to point the Provider Specialty label to the right thing.
Right now, I can't even get a simple select going that joins the two tables since they are using this lookup field as the FK and Access I guess can't understand it. Any help appreciated.
Since the Mastertable provid field is a lookup type, the displayed value is the lookup value rather than the value which is actually stored in the field. This query will show you the stored provid values.
SELECT acct, provid
FROM Mastertable;
And I think you should be able to retrieve the matching provider specialties with a query similar to this:
SELECT m.provid, p.provname, p.provspecialty
FROM
Mastertable AS m
INNER JOIN Providertable AS p
ON p.provid = m.provid;
You may even be able to use that query as the Row Source for a combo or list box on your form. Make provid the bound column. You may wish to set the provid column width to 0" so it is not actually displayed in the control, but still stored in Mastertable.
I think you should modify the table to make provid a normal (numeric?) field instead of a lookup. Fortunately you indicated this is "fairly simple database", so that will hopefully limit the amount of additional changes you need to be compatible with the redesigned table. Good luck.
I created an "On Change" event for the form field "provid" which is a lookup field that displays the provider name while putting the provider id into the master table as a FK. However apparently Access is not able to do lookups based on this field (or I am doing something wrong) using queries - as shown above in comments. What I did is use this as the event code. Important, you must enable macros for this to work!
Private Sub provid_Change()
Me.txtProviderSpecialty = DLookup("Provspecialty", "Providertable", "provid = " & Me.provid.Value)
End Sub

adding data to interrelated tables..easier way?

I am a bit rusty with mysql and trying to jump in again..So sorry if this is too easy of a question.
I basically created a data model that has a table called "Master" with required fields of a name and an IDcode and a then a "Details" table with a foreign key of IDcode.
Now here's where its getting tricky..I am entering:
INSERT INTO Details (Name, UpdateDate) Values (name, updateDate)
I get an error: saying IDcode on details doesn't have a default value..so I add one then it complains that Field 'Master_IDcode' doesn't have a default value
It all makes sense but I'm wondering if there's any easy way to do what I am trying to do. I want to add data into details and if no IDcode exists, I want to add an entry into the master table. The problem is I have to first add the name to the fund Master..wait for a unique ID to be generated(for IDcode) then figure that out and add it to my query when I enter the master data. As you can imagine the queries are going to probably get quite long since I have many tables.
Is there an easier way? where everytime I add something it searches by name if a foreign key exists and if not it adds it on all the tables that its linked to? Is there a standard way people do this? I can't imagine with all the complex databases out there people have not figured out a more easier way.
Sorry if this question doesn't make sense. I can add more information if needed.
p.s. this maybe a different question but I have heard of Django for python and that it helps creates queries..would it help my situation?
Thanks so much in advance :-)
(decided to expand on the comments above and put it into an answer)
I suggest creating a set of staging tables in your database (one for each data set/file).
Then use LOAD DATA INFILE (or insert the rows in batches) into those staging tables.
Make sure you drop indexes before the load, and re-create what you need after the data is loaded.
You can then make a single pass over the staging table to create the missing master records. For example, let's say that one of your staging table contains a country code that should be used as a masterID. You could add the master record by doing something along the lines of:
insert
into master_table(country_code)
select distinct s.country_code
from staging_table s
left join master_table m on(s.country_code = m.country_code)
where m.country_code is null;
Then you can proceed and insert the rows into the "real" tables, knowing that all detail rows references a valid master record.
If you need to get reference information along with the data (such as translating some code) you can do this with a simple join. Also, if you want to filter rows by some other table this is now also very easy.
insert
into real_table_x(
key
,colA
,colB
,colC
,computed_column_not_present_in_staging_table
,understandableCode
)
select x.key
,x.colA
,x.colB
,x.colC
,(x.colA + x.colB) / x.colC
,c.understandableCode
from staging_table_x x
join code_translation c on(x.strange_code = c.strange_code);
This approach is a very efficient one and it scales very nicely. Variations of the above are commonly used in the ETL part of data warehouses to load massive amounts of data.
One caveat with MySQL is that it doesn't support hash joins, which is a join mechanism very suitable to fully join two tables. MySQL uses nested loops instead, which mean that you need to index the join columns very carefully.
InnoDB tables with their clustering feature on the primary key can help to make this a bit more efficient.
One last point. When you have the staging data inside the database, it is easy to add some analysis of the data and put aside "bad" rows in a separate table. You can then inspect the data using SQL instead of wading through csv files in yuor editor.
I don't think there's one-step way to do this.
What I do is issue a
INSERT IGNORE (..) values (..)
to the master table, wich will either create the row if it doesn't exist, or do nothing, and then issue a
SELECT id FROM master where someUniqueAttribute = ..
The other option would be stored procedures/triggers, but they are still pretty new in MySQL and I doubt wether this would help performance.