Update only one column value in Access Database - VB - vb.net

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.

Related

Find most recently edited row in MS Access

In Access VBA, is it possible to find the most recently edited row (Record) in a table?
Example below would be ID 3 as the most recently edited row
Since you are using Access 2010 you can use a Before Change data macro to automatically update a Date/Time field in your table. For example, if you add a Date/Time field named [LastUpdated] then the following macro will update that field whenever a record is inserted or updated:
The advantage of this approach is that it takes place at the table level so the [LastUpdated] field is updated regardless of how the record is inserted or modified. (That is, you don't need to rely on form code to update the field.)
You can add a time stamp field to your table and create a form based on the table. When shown as a table, the form will give you the same functionality as the table itself, but you will be able to set an event AfterUpdate The event can then (over)write the time stamp for that record. You can hide the time stamp control on the form if you like, or set Enabled=False

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

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.

MS Access Delete query based on combobox

I have a table with a bunch of different fields. One is named period.
The period is not part of the raw data but I run a query when I import new data to the database that gives each record a period.
Now I need a delete query that will delete all the records that have the same period as what is selected in a combobox.
The values in the combobox come from a calendar table that contain all the possible values that could be in that period column at any time.
This is the basic query i thought would solve this issue but it tells me it is going to delete 0 rows every time I run it:
DELETE *
FROM PlanTemp
WHERE PlanTemp.period = Forms![Plan Form]!Combo163;
If you don't need the key field, just remove it.
Look at the "PROPERTIES" section and look at the column names.
Ether remove it there, or from your QUERY source.
You can also look at the Data section of the properties, and change your BOUND column, to Column 2... or whatever holds the data you want to use.

How to reset an Access table's AutoNumber field? (it didn't start from 1) [duplicate]

This question already has answers here:
How to restart counting from 1 after erasing table in MS Access?
(6 answers)
Closed 8 years ago.
I have a INSERT INTO ... SELECT statement that copies data from one table to another.
The thing though is, the AutoNumber column value in the second table started from the last number in the first one.
Meaning the count of first table is 2000, then, the second table started from 2001.
Using an Access database, how to reset this value?
You can execute an Access DDL statement from ADO to reset the autonumber seed value. Here is an example Immediate window session:
strDdl = "ALTER TABLE Dummy ALTER COLUMN ID COUNTER(1, 1);"
CurrentProject.Connection.Execute strDdl
The statement must be executed from ADO. It will fail if you try it with DAO (such as CurrentDb.Execute strDdl), or from the Access query designer. The example succeeded because CurrentProject.Connection is an ADO object.
The two values following COUNTER are seed and increment. So if I wanted the autonumber to start from 1000 and increment by 2, I could use COUNTER(1000, 2)
If the table contains data, the seed value must be greater than the maximum stored value. If the table is empty when you execute the statement, that will not be an issue.
Looks like your only option is to move the data into a new table. The following link has some information about how to do it based on your version of access.
Note: be careful if you have relationships to other tables as those would need to be recreated.
http://support.microsoft.com/kb/812718
I ran across this little tid bit of info on how to set the value of a Microsoft Access AutoNumber Field.
Setting the value of a Microsoft Access AutoNumber Field
Using an Append Query to Set the Initial Value of a Microsoft Access AutoNumber Field:
By using an append query, you can change the starting value of an AutoNumber field in a table
to a number other than 1.
Microsoft Access always numbers AutoNumber fields beginning with the number 1.
If the table has data in it already, the starting value of the autonumber will be higher
than the highest value already in the table. You cannot manually edit an AutoNumber
field or change its starting value.
Overview: Changing Initial Value of an AutoNumber Field
In order to force Microsoft Access to number an AutoNumber field with a number you choose,
follow these general steps below:
For a new table that contains no records, you can change the starting value of an AutoNumber
field that has its NewValues property set to Increment to a number other than 1. For a table
that contains records, you can also use this procedure to change the next value assigned in an
AutoNumber field to a new number.
1. Create a temporary table with just one field, a Number field; set its FieldSize
property to Long Integer and give it the same name as the AutoNumber field in the table
whose value you want to change.
2. In Datasheet view, enter a value in the Number field of the temporary table that is 1
less than the starting value you want for the AutoNumber field. For example, if you want
the AutoNumber field to start at 100, enter 99 in the Number field.
3. Create and run an append query to append the temporary table to the table whose
AutoNumber value you want to change.
Note: If your original table has a primary key, you must temporarily remove the primary key
before running the append query. Also, if your original table contains fields that have the
Required property set to Yes, the Indexed property set to Yes (No Duplicates), or field and/or
record ValidationRule property settings that prevent Null entries in fields, you must
temporarily disable these settings.
4. Delete the temporary table.
5. Delete the record added by the append query.
6. If you had to disable property settings in step 3, return them to their original
settings.
When you enter a record in the remaining table, Microsoft Access uses an AutoNumber field
value 1 greater than the value you entered in the temporary table.
Note: If you want to compact the database after changing the starting AutoNumber value, make
sure to add at least one record to the table first. If you don't, when you compact the database,
the AutoNumber value for the next record added will be reset to 1 more than the highest previous
value. For example, if there were no records in the table when you reset the starting value,
compacting would set the AutoNumber value for the next record added to 1; if there were records
in the table when you reset the starting value and the highest previous value was 50, compacting
would set the AutoNumber value for the next record added to 51.
It worked for me find. Just follow the instructions to the letter. Not like me skipping around though it. I found out the hard way to do exactly as it says. I hope it'll help you out if I read your question right. I restart the autonumber field to 4556363 with a table with 8500 records in it and it didn't alter anything, just the autonumber field. I hope this ain't to late to help. Steven

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.