Solved

Microsoft Access Table Locked error

Posted on 2014-09-25
7
327 Views
Last Modified: 2014-09-25
I just came back to the experts as I cannot find a solution to this problem. It should be simple but not for me.
I have a fairly simple database and my vbs code which worked in the past is locked out of this table.
I am just trying to edit two fields in the table, a date field and a distance field which is long integer.
In frustration I made a completely new database with just this one table and created the table as new and it still cannot work. I tried two different methods, as can be seen in the code and get the error which I have also shown in the code. Nothing is changed in the table.

Private Sub NewService_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
Dim b As Integer, d As Integer, DummyDate As Integer
Dim NewData As String, bilum As String
Dim db As Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Dim myBestShot As Date
Set rs = db.OpenRecordset("BReq")

b = 16
    NewData = "Check" & CStr(b)
    If Form_Maintenance.Controls(NewData).Value = -1 Then
        bilum = "Label" & CStr(b)
        rs.Edit
        DummyDate = DLookup("TimeReq", "BReq", "ReqID =" & b)
        myBestShot = DateAdd("d", DummyDate, Me.DateReq)
       
        On Error GoTo handler
        CurrentDb.Execute ("UPDATE BReq SET DueDate = " & myBestShot _
        & " WHERE ReqID =" & b), dbFailOnError
        Exit Sub
handler:
        MsgBox Err & " " & Err.Description
        ' Error here is 3218 Could not update; currently locked.
       
        rs.Fields("DueDate") = DateAdd("d", DummyDate, Me.DateReq)
        rs!DueDate = DateAdd("d", DummyDate, Me.DateReq)
        'processes these lines but changes nothing in the table
        d = DLookup("DistanceReq", "BReq", "ReqID =" & b)
        rs.Fields("DueOdom") = Me.CurrOdo + d
        rs.Update
    End If
rs.Close
db.Close
End Sub
Corolla-4.accdb
0
Comment
Question by:bravotango
  • 3
  • 3
7 Comments
 
LVL 84
ID: 40343499
Why are you doing this with unbound forms? It looks like a very simple matter, and therefore I can't see any reason to use unbound forms for this.

That said:

You can't issue an UPDATE statement unless you've set the Recordset to an actual Record ... you're just using BReq as your recordset, which means there is no current record ...

If you really, really must do this, then when you need to add a new record you use INSERT syntax:

Currentdb.Execute "INSERT INTO BReq(DueDate) VALUES(#" & myBestShow & "#)"

In fact, you don't need all that code, for the most part. I'd think you could do it with this:

Dim DummyDate As Date
Dim myBestShot As Date
Dim b As Integer
b = 16
DummyDate = DLookup("TimeReq", "BReq", "ReqID =" & b)
myBestShot = DateAdd("d", DummyDate, Me.DateReq)
Currentdb.Execute "INSERT INTO BReq(DueDate) VALUES(#" & myBestShow & "#)"
       
There are several other things wrong with your code, but I'm assuming you've copied it over from the other db and made changes to accommodate this structure.
0
 

Author Comment

by:bravotango
ID: 40343629
Hi Scott and thanks for your suggestions. Firstly, the reason I am using unbound form is that the user has to interact with two tables and as a result it seemed simpler to do all the coding in vba to accommodate this.

I did have this completed and working once before and then lost these files when I had to upgrade to a new hard drive (no backups!!!).

There is only 24 records in the database and I only need to edit selected fields of random records.

I thought the rs.Edit and corresponding lines might have done the job but it proceeds without altering anything.

Adding new records is not really a solution.
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 350 total points
ID: 40343654
I didn't say you needed to ADD records, but your question was unclear, and I was just covering all bases ...


If you want to update the record, then all you need is this:

Dim DummyDate As Date
Dim myBestShot As Date
Dim b As Integer
b = 16
DummyDate = DLookup("TimeReq", "BReq", "ReqID =" & b)
myBestShot = DateAdd("d", DummyDate, Me.DateReq)
CurrentDb.Execute ("UPDATE BReq SET DueDate = " & myBestShot _
        & " WHERE ReqID =" & b), dbFailOnError

You do NOT need to open a Recordset to do this - the EXECUTE statement will update the data directly.

However, given the code structure you've got, I cannot determine exactly what you want to do. Looks like you try to run code AFTER your error, which is generally not a good idea. In most cases, your "rs" variable would be out of scope or no longer available, so you'd have other failures as well.

If you could give a clear idea of what you're trying to do, perhaps we could restructure the code to perform correctly.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:bravotango
ID: 40343759
You are correct Scott. I was adding Recordset to see if that might work but cutting all that extra baggage out and now it works and there is no lock out error. However the result is strange, I get a new date like 30/12/1899 instead of expected 15/03/2015 so that is a new problem.

I like the idea of using a bound form better now and using queries to update the table seems a better option as this is just a fairly simple vehicle maintenance table.
0
 
LVL 84
ID: 40343851
I like the idea of using a bound form better now and using queries to update the table seems a better option as this is just a fairly simple vehicle maintenance table.
If you are going to switch to using BOUND forms, then you should not use queries to update the tables. Let Access handle that - it does that much better than you can :)

If you're going to continue using UNBOUND forms, then of course you'll have to handle the data using code and SQL.
0
 
LVL 34

Assisted Solution

by:PatHartman
PatHartman earned 150 total points
ID: 40343934
user has to interact with two tables
Then you have two choices.  Use which ever one makes the most sense for the data:
1. Join the two tables in a query and bind the form to the query rather than to a table.
2. Use a form for the "parent" table/query and a subform for the "child" table/query.
0
 

Author Closing Comment

by:bravotango
ID: 40345181
Thanks for all your help. Will definitely return here for all my problems.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now