MS Access OpenForm was cancelled - error 2501 - database

I've been asked to make some modification to Forms in an Access database used by a group I work with. I had to add a data entry form similar to one that exists so I copied one of the existing forms and made some modifications. When I click the button to open the form to test it I get this error:
Run-time error '2501'
The OpenForm action was cancelled.
Here is the code that is called to cause this error:
Private Sub cmdCPE_EntryForm_Click()
On Error GoTo Err_cmdCPE_EntryForm_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "CPE_EntryForm"
DoCmd.OpenForm stDocName ', , , stLinkCriteria
Exit_cmdCPE_EntryForm_Click:
Exit Sub
Err_cmdCPE_EntryForm_Click:
MsgBox Err.Description
Resume Exit_cmdCPE_EntryForm_Click
End Sub
Thx in advance for any assitance.

the form you're trying to open is in design view. Close design view

I got this error when my form was referring to a query that no longer existed.
Once I updated the form to point to an existing query, my code worked fine.

My form open error 2501 "Form Open Cancelled" error in MSAccess was due to the form's data source table not existing.

Runtime Error 2501 can occur on calling the OpenForm method if there is a problem with the database.
Try following the steps in this link under the headings "Recovering from corruption" and "Symptom: Cannot open a form or report"?
http://allenbrowne.com/ser-47.html
Could also be a problem with the new form's Open or Load events, might be worth posting that code as well for review.

I had exactly the same error when I wanted to open a form containing a sub form whose data was located in another folder. By decreasing the path of the dorsal base this has repaired the error. For example, the first path was C:\Dev\Application\Vers1\Prog\Debug\myAppDB.accdb, so I decreased the path's length in : C:\Dev\Debug\myAppDB.accdb and this solved the problem. The same error occurred with a network path, and I fixed it with the same method.
By assuming that you have :
a command button named "cmdOpenForm" and
a form called: "myForm"
I have also add this code on the On Click Event of the command button :
Private Sub cmdOpenForm_Click()
On Error GoTo Err_cmdOpenForm_Click
If CurrentProject.AllForms("myForm").IsLoaded = True Then
DoCmd.Close acForm, "myForm"
Else
DoCmd.OpenForm "myForm", acNormal, , , , acWindowNormal, Me.Form.Name
End If
Exit_cmdOpenForm_Click:
Exit Sub
Err_cmdOpenForm_Click:
MsgBox "Error number : " & Err.Number & vbCrLf & "Error: " & Err.Description, vbCritical
Resume Exit_cmdOpenForm_Click
End Sub

Your RecordScorce is not recognized. Type your recordScource In Code, then in Menu Tool\Reference Change place of your references.Then Debug your project And then save it.

Try closing the forms that you want to open then rerun the code. Worked for me.

Related

Get Msgbox 3mins later after clicking a commandbutton1 vba

I am really stuck with one of my project. I need to get msgbox appearing 3mins later once clicked on commandbutton1.
I have a button which copies the text "I will be back in 3minutes" now I need to have a pop msg after 3minutes reminding user with popmsg "your time is up"
I tested 10 seconds interval, and it worked.
Private Sub CommandButton1_Click()
'any previous stuff
Application.OnTime Now + TimeValue("00:00:10"), "RemindMe"
End Sub
The following subroutine is in a module.
Sub RemindMe()
MsgBox "your time is up!"
End Sub
Meanwhile, the Excel window doesn't get frozen, you can still operate on the worksheet.

Set Control Property after opening a form on a specific record

Im new to Access and need some help. D:
So I have a form in datasheet view which displays basic information of all the records from a query. By pressing a text field at the end of each record it opens another form in edit mode with the details of that specific record.
But after it opens I want to disable or hide some fields and controls including a button which clears all the fields in the form so that users can't press it accidentally and erase all Information in the form.
I've tried this code:
Private Sub Text23_Click()
DoCmd.OpenForm "FrmEntregas", acNormal, "", "[EntregasID]=" & Nz(CodeContextObject.EntregasID, 0), acEdit, acDialog
'Forms("FrmEntregas").btnReiniciar.Visible = False
'Forms!FrmEntregas!btnReiniciar.Visible = False
'Forms("FrmEntregas").Form.Controls(btnReiniciar).Visible = False
End Sub
The first line works great, the problem comes when refering to the form's button. I've tried refering to the button in three different ways but none of them work. It displays this message:
"Runtime Error 2450: Microsoft Access cannot find the referenced form 'FrmEntregas'."
If someone could point me in the right direction I'd be more than thankful!
Don't open in Dialog mode, the code after DoCmd works only when the form is closed. Change the Modal property of the form to Yes instead.
Private Sub Text23_Click()
DoCmd.OpenForm "FrmEntregas", acNormal, "", "[EntregasID]=" & Nz(CodeContextObject.EntregasID, 0), acEdit
Forms("FrmEntregas").btnReiniciar.Visible = False
End Sub

Open the latest file

I have an Access form with the drawing number D-A1ER-1378-1601-0 listed which is also stored in a file folder.
I use the code below to open the pdf drawing, which works fine.
Public Sub OpenDWG()
Dim strFile As String
Dim PathPDF As String
On Error GoTo Failure
PathPDF = DLookup("[FilePath]", "[SettingsDrawingFilePathTbl]", "ID = 4")
strFile = PathPDF & "\" & Screen.ActiveControl & ".pdf"
If Len(Dir(strFile)) Then
FollowHyperlink strFile
Else
MsgBox "No Document found for this Drawing Number, check Engineering Drawing Search File path in the Settings Tab and / drawing download files"
End If
Exit Sub
Failure:
MsgBox Err.Description
Err.Clear
End Sub
How do I adjust the strfile name
strFile = PathPDF & "\" & Screen.ActiveControl & ".pdf"
to get the form to open only the most recent file when a new version of the drawing is dropped into the folder. ie D-A1ER-1378-1601-0(2) will be the newest revision.
I would like to add a comment, but I don't have enough points ot comment.
I think that you can use the Dir function to get all files beginning with the same characters, or with wild cards, etc. I'll have to look into how exactly to do this. You could populate an array with these, and use code to scan the array to determine the latest file. Or you could populate a temporary table amd then use the table contents as the course to a combo box to have the user select the desired file, sorted with the latest one on top.
If I get a chance, I'll conjure up some sample code an post it.

Visual basic 6 issue with combobox

I have created a forum in vb6 and made connection with a database in access,everything went perfect.
my problem is in my form there is 2 combobox one to select Number and other to get me other numbers (watch the video to understand )
anyway the first combo is working and the second combo is working too but after selecting different number from the first combo i don't get anything in the second combo.anyway i know i just miss something in the code something very stupid
i have uploaded a video so you can see my problem, thanks in advance.
Private Sub Form_Load()
liaison
Do Until rspatient.EOF
Me.npa.AddItem rspatient.Fields(0)
rspatient.MoveNext
Loop
End Sub
Private Sub npa_Click()
rspatient.MoveFirst
Dim cr As String
cr = "npation ='" & npa & "'"
rspatient.Find cr
nom = rspatient.Fields(1)
prenom = rspatient.Fields(3)
rshospita.MoveFirst
nh.Clear
While rshospita.EOF = False
If UCase(rshospita.Fields(14)) = UCase(npa) Then
nh.AddItem rshospita.Fields(0)
End If
rshospita.MoveNext
Wend
End Sub
video for more detail :
https://www.youtube.com/watch?v=Tidm18_tvp0
The simplest possible reason for your problem is that you don't have any hospital records associated with your second patient. Check that first.
However, your code is also a bit convoluted. A simpler way to do what you want is to use Filter instead of Find. Filter restricts your recordset to only those records which match the filter, so you can simply iterate the filtered recordset the same way you do the whole one (rspatient). Something like this:
Private Sub npa_Click()
With rshospita
.Filter = ".Fields(14) = '" & npa & "'"
.MoveFirst
nh.Clear
Do Unitl .EOF
nh.AddItem .Fields(0)
.MoveNext
End With
End Sub

MS Access - open a form taking a field value from a previous form

I have a form in an MS Access database which lists all the landowners consulted with for a new electricity line. At the end of each row is a button which opens another form, showing the details of all consultation, offers made etc.
I am trying to use vb in MS Access to take the contactID and automatically put it in a field in the details form, so that landowner's consultation details will pop up automatically. I am not a vb programmer at all (I have a comp sci degree mostly in Java and I'm currently working as a GIS analyst but it's a small company so I've been asked to get an Access database working).
I want to say
[detailsForm]![contactID] = [landownerlist]![ID]
in a way that vb and access will be happy with. Then I can see if I'm on the right track and if it will actually work! What I have above does not actually work. It won't compile.
From Kaliana
If you wish to open a form to a new record and to set the ID there, you can use Openargs, an argument of Openform:
DoCmd.OpenForm "FormName",,,,acFormAdd,,Me.ID
The opened form would also need some code:
If Me.Openargs<>vbNullstring Then
Me.Id = Me.Openargs
End If
It is also possible to find:
Forms!LandownersList.Recordset.FindFirst "ID=" & Me.ID
or fill in a value:
Forms!LandownersList!Id = Me.ID
on the form being opened from the calling form.
You may want to look into the code that is behind these buttons. If you are using a docmd.openform you can set the 4th Setting to a where clause on openning the next form.
DoCmd.OpenForm "OpenFormName", acNormal, , "[contactID] = " _
& [detailsForm]![contactID] , acFormEdit, acWindowNormal
This assumes contact ID is numeric and doesn't require any quotes.
Using open args is the generally accepted solution, as alluded to by others. This just falls under the category of "For you edification":) One of the problems with using open args is that unless you are careful with your comments it's easy to forget what they were supposed to mean. Were you passing more than one? Which is which? How did I do it here? How did I do it there etc. For my own money, I standardized to this (below) so I can always pass more than one argument without fear, and when I review my code a year from now, I can still see what's what without a huge hassle:
Option Explicit
'Example use: DoCmd.OpenForm "Example", OpenArgs:="Some Filter|True"
Public Enum eForm1Args
eFilter = 0
eIsSpecial = 1
End Enum
Private m_strArgs() As String
Public Property Get Args(ByVal eForm1Args As eForm1Args) As String
Args = m_strArgs(eForm1Args)
End Property
Private Sub Form_Open(Cancel As Integer)
m_strArgs = Split(Nz(Me.OpenArgs, vbNullString), "|")
If LenB(Me.Args(eFilter)) Then Me.Filter = Me.Args(eFilter)
End Sub
Private Sub Command1_Click()
If LCase$(Me.Args(eIsSpecial)) = "true" Then
'Do something special
End If
End Sub
As previously posted OpenArgs is great for this. One trick I have learned is that it is easy to pass in multiple parameters if required as a delimited string (comma for example), the target form can then access these values using the Split() function thus:
StringArrayVariable()= Split(me.OpenArgs,",")
Me.textbox= StringArrayVariable(0)
Me.textbox1= StringArrayVariable(1)
etc.
This is air code so check out the helpfile for Split().
It is also possible to pass objects in OpenArgs as well, it requires some manual memory pointer manipulation and I don't have the code to hand but I'm sure a Google search will find some examples. This technique can cause some random crashes though. Be Warned!

Resources