Access VBA: Unable to add new column without Error 3211 - vba

I'm trying to add a new column/field using VBA into my newly created table and newly created database, i.e. there is absolutely nothing in the table or database. My code is as follows:
Sub test()
CurrentDb.Execute ("ALTER TABLE Table1 ADD COLUMN Column2 Text;")
End Sub
However, this results in the following error message:
Run-time error '3211':
The database engine could not lock table 'Table1' because it is already in use by another person or process.
I'm the only user accessing the table. Running other snippets of VBA code works fine, but this particular piece of code throws this error.
I've tried deleting the database and creating a new one; shutting down all instances of Access; and restarting my computer, but this error still occurs.

This error will be raised if you have the table open.
So, before calling your function, make sure table1 is closed.

Related

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.

Run query connected to another database in VBA Access

I am trying to run saved query inside VBA Access. That query is connected to another database and looks like that:
SELECT * FROM TABLE IN 'C:\USERS\Another_database.accdb'
This query is saved as "My_query" inside first Access database. VBA code looks like this:
Function My_function()
Set rst = CurrentDb.OpenRecordset("My_query", dbOpenDynaset)
End Function
When I try to run it i got an error:
Run-time error '3219'
Any ideas why? It works for normal queries (without IN 'C:\USERS\Another_database.accdb' part.
The most efficient way to get data from another Access database on an ongoing basis is with Linked Tables.
Click External Data on the ribbon, then New Data Source → From Database → Access
Browse to the source database, and make sure you choose Link to the data source by creating a linked table
Select one or more tables that you want to link
[
The linked tables will be created and you will be able to query the linked tables as if they were local to the current database.
More Information:
Office Support : Import or link to data in another Access database
Office Support: Manage Linked Tables
Maybe you need to define a Recordset first or try to remove the quotation marks you have in the example? The following example works fine:
Dim rs as Recordset
Set rs = CurrentDb.OpenRecordset("SELECT Field FROM Table IN 'here goes your path'")
Now you have an Array rs() in which you can loop.

Error 3167 - "Record is deleted" when running an append query. ACCESS 2010

I'm attempting to run an append query to a linked table (access backend) and I'm getting Error 3167, Record is deleted. I can't seem to find the offending record. But if I change the table to a local table and run the append query, it runs with no problems at all. I've attempted a compact and repair but it's not helped at all.
Any ideas?
For some reason it was popping up with "Record is deleted" when it was trying to append a primary key that was already there. It never had that issue before. So I just added to the where criteria:
"table.id NOT IN (SELECT ID from table)"
Runs perfectly now. Have no idea why it started though.
Thank you for everyone's input.

VBA - Jet SQL - False error message

Here's the code:
SQL = " DROP TABLE [H:\Archive23\SPX.accdb].[SPX_MissingTimes] "
MsgBox SQL
DoCmd.RunSQL SQL
This code ran without an error message for a week, then, after some changes in unrelated code following it, it started generating this message:
Microsoft Visual Basic
Run-time error '3295':
Syntax error in DROP TABLE or DROP INDEX.
This error message is generated AFTER later code has run, as proven by the fact that a later MsgBox call executes before the error message.
I call it a "false error message" because the code still works, deleting the table every time.
There are no other DROP statements involved in any code.
I have no idea where to start looking. Any ideas will be appreciated.
GSerg, thanks. Yes, it is a second, different MsgBox coded after DoCmd.Execute that pops up before the error message. This second msgbox displays the SQL statement for a make-table query which replaces the deleted table, and which is executed without incident. I probably should have mentioned: I am using Windows 10 and Access 2016 in a standalone installation (without the rest of Office). I have had no other strange problems. I have a suspicion this is related somehow to Access inner threading logic. Or maybe it's an Access "ghost" (it wouldn't be my first), which can be eliminated by creating a new blank database and importing everything form the afflicted one so start clean.

Microsoft access DoCmd.GoToRecord

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]).