Link to home
Start Free TrialLog in
Avatar of bravotango
bravotango

asked on

Microsoft Access Table Locked error

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
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

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.
Avatar of bravotango
bravotango

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all your help. Will definitely return here for all my problems.