Access, Update Query that does not overwrite data with blank cells - sql

With this Update Query, how can I prevent it from overwriting data in existing rows with blank cells?
UPDATE tbl1
INNER JOIN tbl2
ON tbl1.thing0 = tbl2.thing0
SET tbl2.[thing1] = tbl1.[thing1], tbl2.[thing2] = tbl1.[thing2], tbl2.[thing3] = tbl1.[thing3];
I have users on-site updating a table in real time and worry that remote users updating the table once they have a connection will overwrite the on-site users data with a bunch of blank cells.
Will something like this work if I add it to the end?
WHERE Not Null;
Edit1 for Hansup
Empty in this case means on row1 of the tbl1 where, Onsite User entered data in columns 2 through 7(things redacted in code above). Offsite user has entered nothing in this row today. Offsite user uses the query above to update tbl1. Since columns 2 through 7 are empty on his table, I don't want his work to overwrite Onsite User's edits with blank cells.
Edit2 For HanSup and luk2302
Each row is a project. The projects are created by one user, then multiple users update the projects over their life. So Picture Row 1, thing1, as a customer name that has already been entered. Now, onsite user makes some updates. Offsite user makes none to this row, but then runs the update query.
Offsite user's update should only add info to existing rows, but not remove any data. If it overwrites data with data, I'm fine with that. We are just recording contact info and when certain milestones are reached. So long as the data gets there. I'm not worried about who inputs it.

Excuse formatting, I'm answering from my phone...
You could replace the elements of the SET such that, for example:
SET tbl2.[thing1] = Iif(tbl1.[thing1] is null, tbl2.[thing1], tbl1.[thing2])
In that way, if your new value is null then the other value will be used.
Be aware of possible different behavior of null and empty string, depending on your use case... But you can change the condition to check for empty string too.

Related

Limit a table to hold a max of 100 records and delete the oldest

Its my intention to implement a History function in my database where users can see what records they've previously worked on. To achieve this initially, I created an extra field in the table they we're looking at and added their name from the login screen and timestamp to the record. This works, but the problem is if another user loads up the record, it overrides the last person since its just one field and its inconsistent.
So instead I created a new table and added their names and timestamps in there as a new line. The issue here is that eventually there's going to be 1000's of records in a table eating away at my hard drive space over time and eventually since the max for a table is 2GB, its just going to break one day.
After researching, Queries have a max limit, but you've still got to link that to a table that holds the data. I would like a table that contains, lets say 100 records, and at the 101st record, the oldest line is overridden and so on.
There's no code to share since my problem isn't with VBA, but rather finding a work around for this, I would of assumed this would be implemented into Access by default. I just have a table called 'History' and in VBA, an 'open recordset' function and '.addnew' 'User' and 'Date'.
You could use an After Insert data macro to remove the oldest row when a new one is added:

Get data from another table in a ms Access data macro

I have a tblCustomer and a tblContact, where the information ultimately comes from two different other programs. In order to get around complex ID issues and keep things easy for users, I'm trying to do the following:
If either the CompMyobID or Company (lookup which points to the Primary Key autonumber in tblCustomer) field of a tblContact record are updated, the data macro finds the corresponding tblCustomer record, and uses the Primary Key or the MyobID to update the non-updated field in tblContact.
To achieve that, I've created the following after-update macro - First an If to work out if one of those fields (and which one) has been updated, then an EditRecord, alias not set in order to edit the current record where I SetField the field with the other name to... and that's where I get stuck. I tried to just run some sql, but at the moment nothing actually happens when I update either field...
Edit:
Because, as Erik pointed out, this method locks me out of the current record, and opens me up to recursive issues, Here's another angle that's not yet working:
in a Before Change Data Macro, the following:
You've got two main errors in that data macro:
The just updated record in an after update macro is read-only. This means calling EditRecord will fail. You can work around that by looking up the record you just updated, as shown here, but this requires you
You can't just set the value equal to an SQL clause, and expect the database to execute that clause. Usually, you could lookup records using DLookUp, but data macro's use the Look Up A Record In blocks to look up records instead
The final macro should look something like this (only updating one):
If Updated("CompMyobID") Or Updated("Company") Then
SetLocalVar
NewID
=[ID]
SetLocalVar
NewCompany
=Company
SetLocalVar
NewCompMyobID
=CompMyobID
If Updated("CompMyobID")
Look Up A Record In tblCustomer
Where Condition CompMyobID = NewCompMyobID
Alias T
SetLocalVar
LookupCompany
=T.ID
For Each Record In tblContact
Where Condition ID = NewID
EditRecord
SetField
Company
LookupCompany
End EditRecord
End If
End If
A strong warning: This macro will recursively call itself the way you have designed it! If you update Company, the macro will edit CompMyobID, triggering the macro, then updating Company again, triggering it again, etc.
Access has protection against recursive macro's, so aside from the errors you're triggering, you will probably not notice the recursion, and it won't cause performance problems. I strongly recommend not relying on this protection, and not storing information that could be looked up, but instead using a relation to look up the appropriate company name.
In the end, after the comment discussion below, We used a Before Change data macro, which avoids the need to look up the changed record before altering it, and avoids the chance of the macro calling itself because a Before Update macro edits the current record before it gets updated, instead of updating it again after it gets updated. We changed the macro to check if the value was Null or a zero-length string because Updated is not available in a before update macro.
The final solution was equal to the last edit of the question, with the exception of using Company & "" = "" instead of Company = "" to account for Null values.
You can read more on Null vs zero-length string here. The essence is that a zero-length string is just a string with 0 characters, but Null is a place where no value has been entered. Null behaves a bit oddly, returning Null when you compare it with anything. This means Null = "A" is Null, and gets treated as false, but Null <> "A" is also Null, and also gets treated as false.

How to interact between two tables on both directions in Excel?

I have 2 sheets in my Excel project:
In "Sheet1", I have a big data table, with 4 columns:
DataTable[Country], DataTable[Brand], DataTable[Parameter], DataTable[Calculated].
On "Sheet 2" I have a dashboard. I have there a table called FilteredTable that presents the data from DataTable. I have there a drop down command that lets the user select country, and the table is filtered accordingly.
I want that the FilteredTable will not only show data from the original DataTable, but to let the user to change the [Parameter] column. When changing it, I want that the [Calculated] column in both tables will be updated accordingly. If the user will change the country, then the FilteredTable will show the parameter that is stored in DataTable for that combination of [Country] and [Brand]. And if the user will get back to the first country, the displayed parameter will be the last one that the user entered.
I am a bit confused how to do it.
What I have done so far is:
1) to read into FilteredTable rows from DataFilter, using a formula array. I am mirroing this way [country],[brand] and [calculated]
2) in the DataTable[paramter], I read from FilteredTable[parameter] the same way, with a formula array.
It works fine, untill I change a country, and then the parameters in FilteredTable are already do not match the new country, and in DataTable, the parameters for the old country where changed to 0.
I'm in a logical loop. Is there a way out?
Thanks
Have you considered using 3 tables instead of 2? This would be: the original data table, a table which pulls in all values created through manual entry from the display table, and the display table itself.
In VBA, create a function which runs each time a formula is changed in the display table. That function should take the new data from the user, and copy the altered row onto the 'manual entry' table. For the display table itself, you could either recalculate manually through VBA every time a change is made, or have a formula which looks to see whether there is data in the manual table. If there is data in the manual table, that is where the value is pulled from. Otherwise, data is pulled from the original data table.

Update only one column value in Access Database - VB

I have a feature in my application where a user can opt to UNsubscribe itself. When this happens, there is no change made to the other fields in database and only user's Subscription flag is unchecked. I am doing this by getting a datarow and settingone of the field values and then updating the datarow in the table.
This is working fine for all of the recently created records, however some of the old records have a blank value set for a date field which is now mandatory. Hence when I try to unsubscribe old record of this type, it tries to retrieve null value from the database and then update the same back resulting in an error - "Cannot insert null value in Date Field"
First, if you have a table with a null value in a mandatory column, you would be better served to run an update query on the table that will enter a default value for this mandatory/required column. Wayne G. Dunn mentioned this in the comments to your question. For example:
UPDATE [TABLE1] SET [datefield] = '4/28/1949' where [datefield] = null
**Be sure to backup your data prior to running this kind of query and test with a test table before you execute the query on production data.
Second, if you are doing an update to a row and setting the checkbox value to true, you can review any columns that are required and set their default value in the update query. This makes the query more complex and more difficult to support.
I advise you go with the first option and run an update query on the table. The second option adds unnecessary code to your query that updates the checkbox.
If you post some query examples and table structure, you will get more specific answers. You may consider editing your question to include these details.

Updating DB sync with API - will not update to show when custom fields are changed to blank

The code I created takes data from Highrise API and imports into our MySQL database tables.
This doesn't go to and from the db to Highrise. It simply goes from Highrise to the DB when the sales reps click a "sync" button I created.
Everything works fine when they populate the Highrise custom fields and click Sync. The problem happens when they delete data from a custom field and click "Sync".
I have a loop that does this for each:
mysql_query("INSERT lld_listing_constants (client_hr_id, customvalue, unique_field_id, customglobalid) VALUES ('".addslashes($co_id_hr)."', '".addslashes($subjectdatainner->{'value'})."', '".addslashes($subjectdatainner->{'id'})."', '".addslashes($subjectdatainner->{'subject_field_id'})."')
ON DUPLICATE KEY UPDATE customvalue = '".addslashes($subjectdatainner->{'value'})."', customglobalid = '".addslashes($subjectdatainner->{'subject_field_id'})."'");
So it obviously will INSERT just fine.. or UPDATE if there is a duplicate.. but what if it suddenly becomes blank? How would I check if it's blank?
I think the problem is that pulling the API data - it doesn't return custom fields that are blank.
Generally you'll want to check for the presence of a field before saving its value. If the field exists, assign its value to a variable you pass to the DB query. If not, assign the value to null and make sure the DB query sets null in the database accordingly.