Microsoft access DoCmd.GoToRecord - vba

I'be been given a MA DB and I had to connect that to a MySQL server using ODBC. That was fine. Then I added a subform in the main form that shows all the "attachfiles" related to the main form. That looks fine too. I even created a button to add a new record of attachfile (I save only the path on the table) related to the current form (to the current object that is editable with the form). And another button that deletes all this records related to the current main form. It all looks fine.
But then when I try to create a new record of the main form it complains, runtime error 2105, you can't go to the specified record.And it highlights this line:
DoCmd.GoToRecord , , acNewRec
Why does that happen? With the DB that I was given, it adds a new record with no problem (but there was no subform and tables weren't linked). I am totally new to VBA and I am not sure if the problem is more for using the connector to MySQL or for the modifications I did. I am quite lost.
Any directions?

Double-check that the Record Source property of the form points to a valid linked table in the database. Sometimes when converting from local tables to linked tables the names can get muddled up (e.g., [Invoices] vs. [Invoices1]).

Related

MS Access - "This Record Has Been Changed By Another User Since You Started Editing It"

I know this has been asked many times, and I have tried many of the suggestions and solutions that worked for others, but I am not able to get this error to go away (please see attachment).
This is an Access application that is really just a UI to a SQL Server database, and uses all linked tables to the SQL Server database. The user opens access and they go to a form that opens a "Search", where the user selects a bunch of criteria and clicks "Apply Filter". A list of records appears that meet their criteria. They click the record they want, and a form is opened that displays the details about this record. They then click an "Edit" button that opens another form that allows them to edit certain fields. When they make changes and hit save is when this error appears. The odd thing to me, this only occurs if the user selects the first record in the Search list, but it doesn't seem to happen with the others in the list.
What I have tried is the following:
Added code on the "BeforeUpdate" and "Update Button Click" event to check "If Me.IsDirty = True Then Me.IsDirty = false" End If.
Added a datetime column to the SQL database that gets updated with a trigger ON UPDATE
Verified the database has no bit columns
Changed the "Record Locks" property on the form to "Edit Records"
None of these items made a difference.
Any help on what might be causing this is appreciated.
2. Added a datetime column to the SQL database that gets updated with a trigger ON UPDATE
This could actually be the cause of your problem - Access and Sql Server modifying the same row at the same time is causing the write conflict you see.
I'm afraid you have misinterpreted the valid advice you found.
What you want is a ROWVERSION aka TIMESTAMP column in your Sql Server table. That helps Access figuring out if a row has changed and solve write conflicts. It is fully automatic and needs no trigger.
More details (Albert always provides lots of details :p ) here:
MS Access Write Conflict - SQL Server - Me = Dirty
If this doesn't help: check if you have any other triggers, and post your VBA code for filter / edit / save.

MS-ACCESS 365 Write Conflict Issue

After my Access got upgraded to Office 365 i'm starting to get write conflict issue whenever i edit a particular record. I was not getting this error in 2010 version.
Access database has 1 query used to filter out selected record for editing and 1 form (no subform). On some records i was able to update, but on some records i'm getting a write conflict. I'm using a run command DoCmd.RunCommand acCmdSaveRecord to save each record that is updated. Do you have an idea why changes are saved on some records and why a write conflict on some. There's no other users editing the record.
Thanks
Whenever I have issues with msaccess that I cannot explain, I do the following:
1-) Create a new msaccess empty file;
2-) Import all tables / queries / forms, objects to this new msaccess file
It normally solves this kind of issue.
I had this issue with some of my clients in the past; Most of the times it is resolved by applying the following instruction:
Select File > Options.
In the Access Options dialog box, select Current Database.
Under Application Options, select the Compact on Close check box.
Select OK.
Close and reopen the database for the option to take effect.
More details are here: https://support.microsoft.com/en-us/office/compact-and-repair-a-database-6ee60f16-aed0-40ac-bf22-85fa9f4005b2
You might have a bit field in your table that allows NULL, making it a triple-state field. Whenever you attempt to save your record, if it is null, it can raise this "Write Conflict" error.
The solution would be either to explicitly assign a value to it before saving, or use a default. Alternatively, if you have a bound control to it (like a CheckBox), set its Triple State property to False.

Fields added to SQL Backend lock the records in MS Access

We have a database that is using a SQL Server backend with a MS Access and Filemaker frontend. It has been working fine. I added two fields to one of the tables today, one datetime field and one bit field.
When the table was updated in Access they can no longer edit a field in the records. A Write Conflict error comes up
"The record has been changed by another user since you started editing it. If you save the record, you will overwrite the changes the other user made. Copy the changes to the clipboard will let you look at the values the other user entered and then paste you changes back in if you decide to make chages.
The save Record option is grayed out.
Any ideas what the problem could be?
Is the bit column nullable? MS Access can show those symptoms when dealing with a nullable bit column.

Update multiple records in SQL from Access without error "The data has been changed"

I am working with an ODBC connection between Access 2013 front end and a SQL Server 2008R2 back end. I have both “Datasheet” and “Continuous” forms that display multiple records which are sorted on an “order” field (integer).
The record source of these forms is a query (an Access local query right now, but I don’t think that switching it to a SQL Server view will solve my problem). Users use these forms to set/update the order of their records for reporting purposes.
My problem: the forms are updateable, but keep throwing the error
The data has been changed. Re-edit the record.
I know this error can be solved by setting the respective form to re-query after every update, but that is equally frustrating to the user because then their records are constantly re-sorting on them as they are trying to set the order (and the form "blips" after every entry).
(I have the field "Timestamp" in all my tables. I know a similar error is raised if one does not have this field in a SQL BE database linked to an Access FE.)
What is the best way to allow the users to update the order of all their records without the form resorting on them? (I would like to keep the “sort” in the query on the “order” field so when they open the form next time it displays the records in the order they specified.) I’ve read about using unbound forms with a stored procedure to update the SQL data, but this seems to be useful for one record at a time. Could I use a temp local table, and then do a batch update with a stored procedure? If that is the best answer, I also ask for some example code to get me headed in the right direction. :)
A simple answer! Add Me.Refresh to the AfterUpdate event on the form or subform. Then when a user does want to update the form based on their new order, they can click a "refresh" button that is set to Me.Requery or Me.Parent.Requery depending on the form or subform, respectively.

VBA Access Need to obtain autonumber before saving new record

I've got a database with a SQLServer back end which was migrated from Access and an Access front end. After the migration one problem I keep running into is that autonumbers are not generated until after the record is saved (kind of obvious but Access didn't seem to care). I have a form that opens to create a new record in a table, but elements of that form require the value of the autonumber (Identity) field of that new record to calculate things. I want to somehow obtain this number right as the form loads instead of having to save it and reopen it just to obtain this number. What's the best way to go about this? Thanks in advance.
Access databases return + generate the autonumber when the record is dirty. In the case of SQL server + Access you cannot use nor does the form display the autonumber UNTIL record save time.
The simple solution is to thus force a save in the forms data, and then any following/existing code you have will continue to work.
So your code can look like this:
If Me.NewRecord = True Then
Me.Dirty = False
End If
The above will work as long as SOME editing has occurred. Note that if NO editing has occurred the above will NOT generate the autonumber ID (however even in non SQL server databases, when no editing has occurred then autonumber is not available anyway).
The above works for a bound form. If you have reocrdset code, then you change typial code like this:
Set rstRecords = CurrentDb.OpenRecordset("tblmain")
rstRecords.AddNew
In above your VBA code can/could grab autonumber. However the above code for sql sever will have to force a save.
In fact code that will work for both ACE or SQL server is thus:
will become:
Dim rstRecords As DAO.Recordset
Dim lngNext As Long
Set rstRecords = CurrentDb.OpenRecordset("tblmain", dbOpenDynaset, dbSeeChanges))
rstRecords.AddNew
' code here does whatever
rstRecords.Update
rstRecords.Bookmark = rstRecords.LastModified
lngNext = rstRecords!ID
rstRecords.Close
So the simple “issue” is you need to write out the record to force SQL server to generate the auotnumber. Once you done this record save, then your forms and most VBA code should run “as is”. You do not need to "resort" to additional code such as select ##identity UNLESS you using SQL insertcommands as opposed to say above forms or recordset code.
What you are referring to is the AutoNumber column in Access which is an Identity field in SQL.
The only way to accomplish this functionality in SQL is to insert the record when your form is opening and then use the ##Identity in SQL to retrieve the most recent value.
When your Access application was combined together with the database, Access was essentially doing this for you. You can check the following link for more details.
http://bytes.com/topic/sql-server/answers/143378-identity-sql-vs-autonumber-access
If its on a bound form, give a save button which will save record to database of Master table.It will have nothing except the identity field's value. Then enable or show subform which needs ID from this master table/form.
Looks ##identity code may not be possible for you since its bound form. To convince users why they need to save before adding any data to form, I name the button "show details" or "Add data to details" , this saves the record using docmd on the form, and makes visible the details section.
DoCmd.RunCommand acCmdSaveRecord
'OR a different code snippet, Just update Dirty status of form
If Me.Dirty = True then
Me.Dirty = False
End If
'You may write code to show/visible detail section/subform