Run Time Error '2501' - vba

I'm having the Run Time Error '2501' on a Form. Does anyone have ideas of why this happens and how to fix it?
The Report is called through this code:
Private Sub Command92_Click()
DoCmd.SetWarnings Off
''# get current reqid and version number
DoCmd.OpenQuery "NewReqVersion", acViewNormal
Dim NewReqID As Integer
Dim NewVerID As Integer
NewReqID = Me.Text58
NewVerID = Me.Version + 1
[Forms]![UWReviewForm].[StatusID] = 99
On Error Resume Next
**DoCmd.Save acForm, "UWReviewForm"**
DoCmd.OpenForm "NewReqVersionForm", acNormal
DoCmd.GoToRecord acDataForm, "NewReqVersionForm", acLast
End Sub
Private Sub StatusID_Change()
Me.LastModifiedTimeStamp = Now()
End Sub

The error 2501 can occur on calling OpenForm if there is a problem with the database. Can you please try following the steps in the link below under the headings "Recovering from corruption" and "Symptom: Cannot open a form or report"?
MS Access - Recovering from corruption

In my case, I had a table linked to an external data source, which was another Access database. I had experimented with splitting that database application into its database component and UI component, and had decided to revert the change. Somewhere along the line, this corrupted to the link between my original Access application and the external Access data source. I could verify this by attempting to open the linked table in my original Access application, and it gave an error.
My solution was to delete the linked table from my Access application and import it once again. After this, my problem resolved. The forms which could not open before were based on queries which included this external data source.

Related

Disconnecting users and removing .laccdb file - Office 365

I have .adcde frontend + .accdb backend database located on local shared drive, used by 10 users at the moment. They are using shortcuts to access frontend file. I have struggled to make regular updates as users constantly left DB open so I implemented idea from MSDN website ( https://learn.microsoft.com/en-us/office/troubleshoot/access/shut-down-custom-application-remotely )
Solution works well on my machine, however, when utilized in user environment, it seems to leave .laccdb locks on both frontend and backend (which I deduct should be closed in moment when last connection to frontend closes)
Any hints? Do I understand this structure incorrectly?
Private Sub Form_Open(Cancel As Integer)
boolCountDown = False
DoCmd.Maximize
DoCmd.Restore
Me.Visible = False
End Sub
Private Sub Form_Timer()
On Error GoTo Err_Form_Timer
Dim strFileName As String
strFileName = Dir(filelocation/chkfile.ozx)
If boolCountDown = False Then
If strFileName <> "chkfile.ozx" Then
boolCountDown = True
intCountDownMinutes = 2
End If
Else
intCountDownMinutes = intCountDownMinutes - 1
'DoCmd.OpenForm "aShutDownWarning"
Me.Visible = True
Me.SetFocus
Forms!aShutDownWarning!txtWarning.Caption = "This application will be shut down in approximately " & intCountDownMinutes & " minute(s) due to maintenance works. Please save all work."
If intCountDownMinutes < 1 Then
Application.Quit acQuitSaveAll
End If
End If
end sub
That should work but I guess, since the laccdb files are still there, that one or more user are actually not logged out.
If they logg out from their account or from their computer the Access files can still be open but not running, so your code is not really running.
I solved a similar situation by adding usernames to a table when they start the program and delete the same username when program are closed. That way I could see users that was not logged out by a similar code.
If you're able to delete the .laccdb files manually then no one is still logged in and some user's Access session was simply unable to clean up after itself when it terminated. This might be caused by a user not having delete access to the folder where the .laccdb file is. Otherwise, someone is still logged in. You can use the Jet UserRoster to see who is logged in. If you want to make your own viewer see https://learn.microsoft.com/en-us/office/troubleshoot/access/determine-who-is-logged-on-to-database or see this utility at https://www.utteraccess.com/topics/1897146

how to use environ function to avoid other from using my Access DB

I manage an Access DB (accdb) and it contains some information about my company that I don´t want others to access it out of my company´s server.
I thought to use Environ (5)=computername or Environ (12)=path to retrieve some references such as LEN(environ(path)). With this function, I could, for instance, make sure that the accdb file only works if LEN(environ(path))/2+15=55 (the lenght at my company´s server divided by 2 plus 15 = 80/2+15=55 = algorhytm).
So, on opening the db, it should prompt for a number/code. If the user inserts 55 and the filepath = 80, it will open. If filepath=100 (filepath out of my company´s server), must be prompted 100/2+15=65 to open the db.
Unfortunelly, I don't know how to programe it neither I know how to block the use of SHIFT (that breakes the VBA code on opening) because I'm a rookie.
So, if you please, can you help me to solve these huge problems (1. algorhytm using Environ, 2. avoid using SHIFT on opening).
Thanks in advance.
Bruno
Add this code to your startup form. When the form opens it will check for the username and computername, and if both match the form will open.
Private Sub Form_Open(Cancel As Integer)
If Not (Environ("username") = "santosh" And Environ("computername") = "ABC-CAP1-093") Then
Cancel = True
Application.Quit
End If
End Sub
Avoid using shift key - I have already answered see this link

Access VBA unable to refresh a form

I have a form (frmDropDownEdit) that has a filtered table as the data. A "New" button is created that opens another form (frmDropDownNew) and the user can enter new data. When complete the new form is closed and the user is back to the original form. The code for frmDropDownNew correctly add the info to the table, then the code refreshes the frmDropDownEdit form but it does not refresh. If I click the refresh button in the ribbon, it also does not refresh. But refresh all does work.
How can I have my code refresh the data in frmDropDownEdit. I also put code me.refresh on the OnGotFocus event but that does not even run.
Here is my source code
Private Sub Command5_Click()
'Add Button
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblDropDown")
rst.AddNew
rst!DdCategory = Me.txtCategory.Value
rst!DdDescription = UCase(Me.txtDescription.Value)
rst.Update
rst.Close
DoCmd.Close
Forms!frmDropDownEdit.Refresh
End Sub
On my MS Access 2010 ×64 single user PC, Forms![AnyForm] .Refresh never worked in VBA, independently where it is placed in any database's code. Form_Current() doesn't run either as it should after data are modified (verified by putting a Stop therein). Moreover, records with modified data are neither marked dirty nor refreshed before the vba code has finished. Procedures which should run without delay when data are modified don't run, even when placed into the modified fields' events.
Thus, one has to use a work-around. Many people recommend to use .Requery instead of .Refresh and then to return by vba code to the desired record, but this requires a field with a primary key.
My solution for tables without primary key is the following:
'…
' ESSENTIAL: this code must be run from ANOTHER module !
' (it runs without error in MyForm's own code [eg. in Form_Activate(),
' but then MyForm is NOT immediately refreshed as desired,)
' one still has to repeat these steps by hand afterwards…
DoCmd.GoToRecord acForm, "MyForm", acNext
DoCmd.GoToRecord acForm, "MyForm", acPrevious
' NB: Forms![MyForm].Refresh doesn't work
' at this place in "MyOtherModule" either.
'…
As mentioned in above code comments: this code must be run from another module ("MyOtherModule") - in my case, a form-independent procedure called upon closing a pop-up form opened from the first form, which interactively modifies data. These data should be updated/refreshed immediately when closing the pop-up form, reflecting all changes and their consequences (for example, automatic filling-in/deleting of other data and/or en/disabling controls or making them [in]visible, depending on the modified fields' values).

VBA ADO: Could Not Use <Filename>; file already in use

It's been a long time since I've had to do any development in Access, so hoping I can get some help. I have a split Front End/Back End solution that I've built. The Back End resides on a server, the front end gets copied down to user's desktops (and they use runtime Access 2013). I'm using Access with VBA and ADO connections/recordsets in order to do all record actions (Select, inserts, updates mostly).
Two intermittent issues have cropped up and I'm at a loss - this is one of them. From time to time, some users will get the error "Could not use "(back end Filename)"; file already in use." (where (back end filename) is my back end db name & location". When users get this message, they close out, re-open and try the same data entry and it works without a hitch. Here's the code:
Private Sub SetProblemCode()
On Error GoTo ErrorHandler
strSQL = "SELECT Problem_Code_ID, Problem_Code, Problem_Description FROM Problem_Code ORDER BY Problem_Description"
con.Open strConString
rstProblemCode.CursorLocation = adUseClient
rstProblemCode.Open strSQL, con, adOpenForwardOnly, adLockReadOnly
cboProblemCode.RowSourceType = "Table/Query"
Set cboProblemCode.Recordset = rstProblemCode
rstProblemCode.Close
con.Close
Exit Sub
ErrorHandler:
CriticalError Err.Description, Err.Number, Me.Name, "SetProblemCode"
End Sub
The rst and con objects are defined at the global level, a practice I've used in other solutions before but I'm questioning if that's some of the problem. I'm also questioning the cursor location, type and lock type I'm using, although it seems correct - I'm not altering data, just copying a recordset to the Access combo box.
I'm hesitant to make sweeping changes when it seems like the user closes out and tries again and it works just fine. Any thoughts?

How to open macros from other databases - VBA, MS Access 2003

I've been tasked with creating an Access 2003 application that will act as a focal point for all other databases (6 in total).
These 6 databases will each contain a macro used to build data bound for a table called DispenseExport.
Originally each of these databases had their own version of this table but now we have to have them all write to the one application - which I've affectionately codednamed Omega.
Omega will contain the DispenseExport table that all the others will write to. It will also be the database that calls the macros from the others to write to it.
So - what is the best way to accomplish this?
I already have a sample sub to call a macro from another database (and works) but the problem here is that it opens the database as any normal user would - Omega will sit on a server and needs to bypass this - by possibly using the SHIFT-KEY access method, if best?
How do I do that programmatically in VBA, assuming it's the best way to do so?
Here's what I have so far:
Option Compare Database
Option Explicit
'Sub to call a macro from another database
Public Sub CallMacro()
On Error GoTo ErrHandler:
Debug.Print "Opening Macro"
'/x tells the database to run the macro as soon as it is opened
Call Shell("msaccess.exe C:\path-to-database\database.mdb /x mcrTestCall", 0)
Debug.Print "Completed Macro"
ErrHandler:
If Err.Number <> 0 Then
'Create a message box with the error number and description
MsgBox ("Sorry but error number " & Err.Number & " occurred; " & vbCrLf & Err.Description)
End If
End Sub
UPDATE
Still haven't found the answer but believe that I'm closing in. I need to find out how to emulate HOLDING DOWN SHIFT - any ideas?
Thanks
This will avoid shell call (Access 2007, also Access 2003?):
'
' variables:
' strDbName: database filename
' strMacro: macro name
'
Sub CallMacro()
'
Dim strDbName, strMacro
Dim objApp
'
strDbName = "C:\path-to-database\database.mdb"
strMacro = "mcrTestCall"
'
Set objApp = CreateObject("Access.Application")
'
objApp.OpenCurrentDatabase strDbName
'
' Run Macro:
'
objApp.DoCmd.RunMacro strMacro
'
objApp.Quit
Set objApp = Nothing
'
End Sub
To skip startup form or AutoExec, there is no simple solution, but we have a workaround in http://access.mvps.org/access/api/api0068.htm, by simulating the Shift key, using API.
There is a cleaner way to do this without opening the remote database. Build a query within that database that does what you want, then call it from the other database without ADO, DAO or opening it. Below is a link explaining how:
Remote Queries
Since links go away, the gist of it is this:
1. Build the query you want to call to return what you need in the other database
2. In the remote calling database, open a query without selecting a table (I use Query Design)
3. Right click on the query background and select 'Properties'
4. Replace the Database setting of (Current) with the full path to the database with the query
5. Build a query to call the original query as the datasource
6. Output the data by a "SELECT INTO" or DoCmd.TransferSpreadsheet, etc