Hello,
Have duplicated your set up and cannot understand how this macro is working at all on your system. The way you have this written, you have defined oSQLstmt as a String but in the code body fill it with an Object (sqlTxtField). This creates an error anytime the macro is executed.
Here is the code I tested with and works:
Sub OpenAnswer
DIM oDatasource AS OBJECT
DIM oConnection AS OBJECT
DIM oStatement AS OBJECT
DIM oSQLstmt AS STRING
oDataSource = ThisComponent.Parent.CurrentController
IF NOT (oDataSource.isConnected()) Then
oDataSource.connect()
End IF
oConnection = oDataSource.ActiveConnection()
'retrieve SQL statement from form '
oForm = ThisComponent.Drawpage.Forms.getByName("sqlForm")
oSQLstmt = oForm.getByName("sqlTxtField").Text
'execute the sql statement and open the Answer form '
oStatement = oConnection.createStatement()
oStatement.execute(oSQLstmt)
ThisDatabaseDocument.FormDocuments.getbyname("Answers").open()
End Sub
Only two of the lines were changed; retrieval of oSQLstmt and the actual execution line of code.
Edit 2019-03-11:
Not certain where your problem lies as you have not posted a sample.
Do not see this happening on the one I created for testing:
Sample ------ TestFormCall.odb.
Records are returned on first try.
Edit 2019-03-13:
You should not be using labels for this type of information but rather a text box as in my example above. You can place default information in a text box. Have posted below your example with a slight change in the macro:
Dim mySQL AS String
mySQL = oSQLstmt.Label
'execute the sql statement '
oStatement = oConnection.createStatement()
' oStatement.execute(oSQLstmt.Label)'
oStatement.execute(mySQL)
That is to extract statement to a string variable before executing the SQL. Works now as my example posted previously does.
Sample ------ FixedExample.odb