Why I can't open a form after a CreateQueryDef instruction?

53 views Asked by At

I have an Access 2016 database which use a form to select a time interval of 1 or more days. A button let me to get the begin and end dates of the interval and do the follow 2 things:

a) build a query that, based on the dates, extracts a dataset from a table

b) open a pop-up form that show the dataset extracted by the query. There is no code on OpenForm event.

The magic is that everything works like a charm until I disable the Shift Bypass Key with the command

CurrentDb.Properties("AllowBypassKey") = False

After that the query still works well, but when the code try to open the form, 95% of the times, I get the error '2501 The OpenForm action was canceled', even if it worked well with Access 2013.

The code is quite simple, but after 3 days of hard work I still don't understand what is wrong. The only thing I got is that if I don't execute the CreateQueryDef instruction the error goes away and the form opens regoularly (even if it does not show the right dataset). Therefore both the routine works alone, but they conflict if they run one after the other.

Below the code behind the button:

Private Sub Cmd_Meteo_Click()
On Error GoTo Err

Dim strFrmName As String                            
Dim datBegin As Date                               
Dim datEnd As Date                                 

'Set the time interval
datBegin = Me.Txt_BeginTreatment                    'Set the begin of the interval
datEnd = Me.Txt_Data                                'Set tha end of the interval

'Build the query with meteo data
Call GetMetoData(Me.Txt_Region, Me.Cmb_MeteoStation, datBegin, datEnd, False)

'Set the form name
strFrmName = "Frm_DatiMeteoControllo"                   

'Check if the form is already open
If CurrentProject.AllForms(strFrmName).IsLoaded Then        'If the form is already open
    DoCmd.Close acForm, strFrmName                          'Close the form
End If

DoCmd.OpenForm strFrmName         'This line rise the 2501 error!

Exit_sub:
    Exit Sub

Err:
    MsgBox Err.Number & " " & Err.Description
    Resume Exit_sub
End Sub

and the subroutine that build the query:

Public Sub GetMetoData(strRegion As String, intIdSM As Integer, datBegin As Date, datEnd As Date, bolTot As Boolean)
On Error GoTo Err

Dim db As DAO.Database
Dim strDbName As String
Dim qdf As DAO.QueryDef
Dim strSqlMeteo As String
Dim strLinkName As String
Dim strQryName As String

Set db = CurrentDb                                  'Set the db
strDbName = Application.CurrentProject.Name         'Get the db name
strTblName = GetMeteoTableName(strRegion, intIdSM) 'Get the name of the data table
strLinkName = "Tbl_DatiMeteo"                       'Set the name of the linked table
strQryName = "TmpQry_DatiMeteoControllo"            'Set th name of the query

'SQL statement for the query
strSqlMeteo = "SELECT " & strLinkName & ".Data, ([" & strLinkName & "].[Precipitazione]) AS PrecTot, " & _
                strLinkName & ".Tmin, " & strLinkName & ".Tmean, " & strLinkName & ".Tmax" & vbCrLf & _
                "FROM " & strLinkName & vbCrLf & _
                "WHERE (((" & strLinkName & ".Data) Between #" & Format(datBegin, "mm/dd/yyyy") & "# And #" & Format(datEnd, "mm/dd/yyyy") & "#));"

'Delete the previous query
If QueryEsiste(strDbName, strQryName) Then      'If the query already exist...
    DoCmd.DeleteObject acQuery, strQryName      'delete the query.
End If

'Make the new query
Set qdf = db.CreateQueryDef(strQryName, strSqlMeteo)

Exit_sub:
    qdf.Close
    Set qdf = Nothing

    db.Close
    Set db = Nothing

    Exit Sub

Err:
    MsgBox Error$
    Resume Exit_sub
End Sub

Does anyone has a hint or faced the same problem?

1

There are 1 answers

2
Gustav On

There should be no reason to delete the query:

If QueryEsiste(strDbName, strQryName) Then 
    ' Modify the previous query.
    Set qdf = db.QueryDef(strQryName)
    qdf.SQL = strSqlMeteo
Else
    ' Create the new query.
    Set qdf = db.CreateQueryDef(strQryName, strSqlMeteo)
End If