"Could not update; currently locked" Error caused by VBA? - ms-access

I have inherited and MS Access application. There are many issues that could be changed within the application but unfortunately it was rushed to production and major redesign changes cannot be made.
I have split and given FEs to 6 different users. for the most part there does not seem to be many issues with this, however some users are getting the error "Could not update; currently locked" intermittently when they create a new record on one of the forms and enter into a subform to insert information into it. The main form contains a customers details and the subform contains information on the product that they have.
Optimistic locking being used throughout the application and this doesn't seem to be occurring because two users are updating a single record, as they will basically never being looking at the same records.
I think that the issue may be code that is being used to generate that customer ID. The form where the error occurs is (1_1)Customer Subform. There is a subform within that subform called (1_1_1)Product Subform. These are linked by a PID, which is present in each subform. When the mouse is clicked within the product subform, the application hangs for a few seconds before giving the error "Could not update; currently locked".
There is a button on the form that does the following:
Private Sub NewCustomerRecord_Click()
DoCmd.Close acForm, "1Main_IPCOR"
DoCmd.OpenForm "1Main_IPCOR", acNormal, "", "", , acNormal
Forms![1Main_IPCOR]![1IPCOR_Sform]![(1_1)Customer Subform].SetFocus
DoCmd.GoToRecord , , acNewRec
Forms![1Main_IPCOR]![1IPCOR_Sform]![CREATED_DATE] = Now()
Forms![1Main_IPCOR]![1IPCOR_Sform]![P_ID] = NewP_ID()
Forms![1Main_IPCOR]![1IPCOR_Sform].Form.Refresh
Exit_New_record_Click:
Exit Sub
Err_New_record_Click:
MsgBox Err.Description
Resume Exit_New_record_Click
End Sub
Private Function NewP_ID() As String
Dim strTemplate As String
strTemplate = Replace("CUSXX", "XX", Right(Year(Date), 2))
NewP_ID = Nz(DMax(Expr:="Right([P_ID], 5)", _
Domain:="[(1_1)_Customer]", _
Criteria:="[P_ID] Like '" & strTemplate & "*'"), "0")
NewP_ID = strTemplate & Format(Val(NewP_ID) + 1, "00000")
'Exit function now after successful incrementing or after error message
Exit_P_ID:
Exit Function
'If an error occurred, display a message, then go to Exit statement
P_ID_Err:
MsgBox "Error " & Err & ": " & Error$
Resume Exit_P_ID
End Function
What the function is doing is closing the main form and reopening it with a new ID so a new customer can be added.
Is there anything within the method that may be causing the record to be locked, and never unlocked?
Thanks!

Related

New record only, when subform has records

Five month ago, I got help with this problem:
Auto next number count to specific selection
I needed this auto count for daily reports at constuction sites.
Now my Problem is, the code
Dim NextNumber As Long
NextNumber = Nz(DMax("[raumBTBNR]", "[tbl_RaeumstellenErfassung]", "[raeumKostenstelleIDRef] = " & Me!KostenstelleAuswahl.Value & ""), 0) + 1
Me!Nummer = NextNumber
is placed at SelectingCostCentre_AfterUpdate ( Its working correctly )
The consequence:
If an employee wants to write the daily report:
He opens the program, selects the matching construction site he is at and the daily report number counts +1. So far so good...
If he is now leaving the program, or is clicking at another form without
adding employees at the activity form, Access saved the record in spite of this.
When he now wants to enter the record again, he has to type in his construction site again, but the auto report number isn't correct anymore, because he counts +1 again. This relates to an DoCmd.GoToRecord , , acNewRec at Form_Open
Form looks like this
What i want access to do is:
When opening the form, check if the last record (of the specific constr. site) at table
"Constructionsite entry" has entries at the table "activity log"
If True
DoCmd.GoToRecord , , acNewRec
Else
Open Last Record of the matching constuction site
EDIT:
or something like:
If no data entries at table "activity log" available
Recordset current delete
Apparently my VBA skills are very basic .. if any.
I looked at function DLookup, but don't know how to handle syntax with this type of problem.
EDIT2
Got a first step of a Solution:
If DCount("*", "tbl_RaeumstellEnerfassung", _
"RaeumKostenstelleIDRef=" & Me!KostenstelleAuswahl & " " & _
"AND raeumDatum =" & Format(Me!raeumDatum, _
"\#yyyy-mm-dd\#")) > 1 Then
If MsgBox("Die Räumstelle " & Me![KostenstelleAuswahl].Column(1) & " ist am " & Me![raeumDatum] & " schon eingetragen worden!" & vbCrLf & "Trotzdem fortfahren?", vbQuestion + vbYesNo) = vbNo Then
DoCmd.GoToRecord , , acLast
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True
Else
Cancel = False
End If
End If
He checks if a constructionsitenumber and a date is already entered.
What I would like to do is having a third MsgBox button as Dlookup or DLast, which shows me the last data related to this construction site.
DLookup("[raeumID]", "tbl_RaeumstellenErfassung", "[KostenstellenIDRef] ="Forms![frm_RaeumstellenErfassung]![KostenstellenAuswahl]")
apperently this isnt working .. more precisly I dont get this to work with a 3d button

How to keep formula in form field when add new record using form in access

I have a form to entry new data to table in Access 2010.
I want Request ID field to be Max(Req_Number)+1 from current table, whenever user add new record and want it to be filled automatically, also Req_Number is the key. When use Data Mode all fields are blank so I wonder how to lock one filed with a function when entry data to a table using form in access?
The RequestID text box should be Locked and Disabled. Add code to the form's Before Insert event as follows:
Private Sub Form_BeforeInsert(Cancel As Integer)
On Error GoTo PROC_ERR
RequestID = Nz(DMax("Req_Number", "myTable"), 0) + 1
PROC_EXIT:
On Error Resume Next
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " - " & Err.Description
Resume PROC_EXIT
End Sub

How to record unbound control to a database

I have a form control (address) that uses Dlookup to call info from a table "database" but the form is bound to table "Tracker". The dlookup is based on another control on the same form - "Name" I need the form to record this dlookup control, along with other controls that are bound to "tracker" as new recordto the table "tracker."
My failed attempts:
Using the default value property to assign the recalled data from the dlookup to another text box which would be bound to "tracker" This simply does not work for some reason. Perhaps I am missing something that tells this control "Address" to update upon selecting the correct "name?"
Code:
Private Sub SubmitReferral_Click()
On Error GoTo Err_SubmitReferral_Click
DoCmd.GoToRecord , , acNewRec
Exit_SubmitReferral_Click:
Exit Sub
Err_SubmitReferral_Click:
MsgBox Err.Description
Resume Exit_SubmitReferral_Click
End Sub
I also tried this - to assign the data - but the data from the dlookup in control "Address1" is not transferring/copying to control "Address2"
Private Sub Combo276_OnUpdate()
OnUpdate ([Address2].Value = [Address1].Value)
End Sub
Help or suggestions?
PS - I have tried to Edit per request to be as specific as possible, and to follow proper board etiquette.
Still unsure of your field names, etc., but the following is an example you can modify. Change 'tblEmployee' to 'database'.
I must state that if you are just starting out with developing in Access (or VBA) that you should never use names that are reserved words, or that can be misleading. Your table named 'database' is ok if named 'tblDatabase'.
Option Compare Database
option Explicit
Private Sub cmdInsert_Click()
Dim strSQL As String
Dim i As Integer
Debug.Print "cmdInsert; "
i = MsgBox("Do you want to add 1 row for Employee ID: " & Me.EmpID & " to table 'tracker'?", vbYesNo, "Confirm Add")
If i = vbNo Then
Exit Sub
End If
DoCmd.SetWarnings True
strSQL = "INSERT INTO tracker ( FirstName, LastName, Add1, City, St, Zip ) " & _
"SELECT tblEmployee.FirstName, tblEmployee.LastName, tblEmployee.Add1, tblEmployee.City, tblEmployee.St, tblEmployee.Zip " & _
"FROM tblEmployee " & _
"WHERE (((tblEmployee.EmpID)=" & Me.EmpID & "));"
DoCmd.RunSQL strSQL
End Sub
Thanks for the help - I solved my concern by hiding the fields that contain the dlookup, and putting code behind a button that copies the information to fields that are bound and therefore will record to the table "tracker"

MS Access Code issue for Reminders

I'm trying to setup reminders based on tasks added from a Table called "Tasks" Here is the code I'm using but something isn't right as it keeps giving me issues with the following line:
intStore = DCount("[TaskName]", "[Status]", "[DueDate] <=Now() AND [Complete] =0")
When the code runs I get the error:
Microsoft Access database engine cannot find the input table or query
for 'Status' Make sure it exists and is spelled correctly.
In my table I have fields for Task Name, Status, and Due Date so I'm not exactly sure why this is coming up.
Below is the entire line of code:
Private Sub Form_Load()
'On Load of the switchboard check Jobs table for any uncompleted jobs
Dim intStore As Integer
intStore = DCount("[Priority]", "[Tasks]", "[DueDate] <=Now() AND [PercentComplete] <=0")
If intStore = 0 Then
Exit Sub
Else
If MsgBox("There are " & intStore & " uncompleted jobs" & _
vbCrLf & vbCrLf & "Would you like to see these now?", _
vbYesNo, "You Have Uncomplete Jobs...") = vbYes Then
DoCmd.Minimize
DoCmd.OpenForm "Tasks", acNormal
Else
Exit Sub
End If
End If
End Sub
You can only perform a DCount on one field (primary key is best if you are just doing a general count on the table). You have entered "[Status]" where Access is expecting a table or query name to be used as the source of the [TaskName] field.
See here for more information.
Judging by your other code example I expect that your code needs to be:
intStore = DCount("[TaskName]", "[Tasks]", "[DueDate] <=Now() AND [Complete] =0")

Deleting record on Continuous form in MS Access using acCmdDeleteRecord

I have continuous form with just 2 fields and a button.
Design View
'
Form View
Below is the code for Delete Button.
Private Sub cmdDelete_Click()
DoCmd.RunCommand acCmdDeleteRecord
Debug.Print "IDWeb - " & Me.IDWeb
'Here I Execute a Web API to Delete Data based on Me.IDWeb
End Sub
The Problem - Web API does not get the Me.IDWeb Value . SOme times the IDWeb is captured correctly, sometimes not.
Edit1 : I tried the code below. But still the problem exists.
I guess the users can be on another record when they deleted the current record. Hence the issue. But the record is currently deleted in MS Access. Only the problem is Me.IDWeb is NOT captured correctly, and hence my web API is getting failed.
Private Sub cmdDelete_Click()
Me.WebType.SetFocus
If Me.IDWeb = "" Or Me.IDWeb = vbNull Or Me.IDWeb = vbNullString Then
MsgBox "No Record Selected"
Else
DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord
Debug.Print "IDWeb - " & Me.IDWeb
'Here I execute my Web API to Delete based on Me.IDWeb
End If
End Sub
The solution was to first get Me.IDWeb and store it in a variable,
then run acCmdDeleteRecord afterwards.
Personally, I don't use DoCmd. You are trying to get the IdWeb of a deleted record.
Assuming your IDWeb is a number (if not use the appropriate data type), the following should work
Dim lngIdWeb as long
lngIdWeb = nz(Me.IdWeb, 0)
If lngIdWeb > 0 Then
Me.RecordsetClone.FindFirst "IdWeb = " & lngIdWeb
If Not Me.RecordsetClone.NoMatch Then
Me.RecordsetClone.Delete
End If
Debug.Print "IdWeb - " & lngIdWeb
End If