Unanswered: runtime error 2501 openform action was canceled
Hi, How to fix that?
runtime error 2501 openform action was canceled
Option Compare Database
Private Sub Generate_Click()
' Declare variables
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("q_produkti_print")
' Loop through the selected items in the list box and build a text string
If Me!list_names.ItemsSelected.Count > 0 Then
For Each varItem In Me!list_names.ItemsSelected
strCriteria = strCriteria & "produkti_vav.ID = " & Chr(34) _
& Me!list_names.ItemData(varItem) & Chr(34) & "OR "
strCriteria = Left(strCriteria, Len(strCriteria) - 3)
strCriteria = "produkti_vav.ID Like '*'"
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM produkti_vav " & _
"WHERE " & strCriteria & ";"
' Apply the new SQL statement to the query
qdf.SQL = strSQL
' Open the query
' Empty the memory
Set db = Nothing
Set qdf = Nothing
This line: DoCmd.OpenForm "f_produkti_print" opens the form.
The cancel occurs after this line.
Either the user is canceling it, or there is something in the Open Event of the form that is setting Cancel = True.
I would also look at the SQL string you are assigning to the query - it is possible the SQL statement is invalid and when the form opens, it tries the query, which fails and subsequently cancels the form from opening.
After you assign the SQL to the query, try to open the query.
If the query opens fine, close it and try to manually open the form.