VBA Call to SQL Stored Procedure Causing Other Pages to hang - vba

I've got a simple piece of code that executes a SQL Server backup.
With sCmd
.ActiveConnection = sCN
.CommandText = "dbo.csp_ad_hoc_single_full_backup"
.CommandType = adCmdStoredProc
.Parameters.Refresh
.Parameters("#database_name").Value = strDB
.Parameters("#backup_dir").Value = preBackup_directory
.Parameters("#backup_dir_space").Value = objDiskSpace
.Execute , , adAsyncExecute
End With
returnvalue = sCmd.parameters(0)
This code executes when the user hits the page, the return value then displays a different text per each possible result of the procedure.
The problem is that I cannot hit any other pages while this procedure runs. I've tried using 'adAsyncExecute', but that does not seem to work. The page that displays the results and runs this code does not load until the entire backup completes. I wonder if perhaps this is why other pages hang.
Any ideas?

As per enderland's comment, VBA will not continue until the .Execute process completes.
My solution is to simply write the parameters sql table instead of immediately trying to execute the procedure. The web page will display the table write as sucessful. A job will then pick up those parameters and execute the code as it would have upon page load originally. The user will be notified of the job's completion via email using msdb.dbo.sp_send_dbmail a the end of the job.

Related

Excel VBA cannot determine when SQL Server stored procedure has finished executing

I am executing a stored procedure in SQL Server from Excel VBA. The stored procedure takes several minutes to run, but the VBA code originally continued to run and displays a "done" message, even when the stored procedure was still executing.
To remedy this problem, I added a loop to check if the connection to SQL Server is executing something, but for some reason, I am getting stuck in the loop and never reaching the end message.
Here is what I have written:
Dim cn As Object
Dim startTime As Variant, endTime As Date
Set cn = CreateObject("ADODB.Connection")
cn.Open = ...connection string stuff to open...
cn.Execute "stored_procedure", , adAsyncExecute
Do While (cn.State And adStateExecuting) = adStateExecuting
DoEvents
Loop
cn.Close
Set cn = Nothing
MsgBox "Execution completed"
For some reason, when I print the connection state inside of the "Do while" loop, it only says 1 (1 = connection is open, instead of 4 = executing). Yet the loop is never exited!! Am I missing something here?

Run query from VB in MS Access

I'm trying to set the sql code for a query and then run the query from VB. The problem is that when I change the sql dynamically, the VB opens the query but does not refresh it. It still shows results from old sql. If I check the sql, it has changed, and if I then run the query (! button), it runs with the new sql.
I'm doing:
Set qdf = CurrentDb.QueryDefs("temp_query")
qdf.SQL = SQL_query_string
MsgBox (qdf.SQL)
DoCmd.OpenQuery ("temp_query")
With SQL_query_string containing the new, dynamically generated sql. The message box shows me that I indeed have the new string. And, as I said, I can check it in the query itself, and it has changed, but DoCmd.OpenQuery("temp_query") seems to just give the query the focus, not actually running it. What command runs it with the fresh sql?
If the query is already open, you need to close it and then reopen, interestingly enough, you probably do not even need to check if it is open, you can just close and then run the code.
DoCmd.Close acQuery, "temp_query"
Set qdf = CurrentDb.QueryDefs("temp_query")
qdf.SQL = SQL_query_string
'MsgBox (qdf.SQL)
DoCmd.OpenQuery "temp_query"
Make sure you have not used Set Warnings or On Error resume next, because they will mask errors.

Classic ASP, ADO, recordset not populating from Stored Procedure

In a Classic ASP script, I have a sql statement that executes a stored procedure:
sql="exec my_stored_proc"
I then attempt to fill an ADO Recordset object with the results and loop through the recordset.
The stored procedure itself is populating a virtual table and the last line of the procedure returns the results:
select tableName, subTable, FieldLabel, total, FieldOrder from #AdmissionsTable
When executed directly from SQL management studio, I get results, but when executed from the ASP page, this line:
do while not rs.eof
results in the error "Operation is not allowed when the object is closed."
I know it's not because I'm trying to execute a stored proc, because I tested it against another stored proc, and was able to get results into the rs object.
Is it because I'm using a virtual table? Or something else?
Extra code:
After the sql string is assigned I open the connection, set the rs object, and execute the query:
conn.Open strConnection
Set rs = server.createobject("ADODB.Recordset")
rs.open sql, conn
Then comes the error line:
do while not rs.eof
Further update:
Right after the rs.open line, I added some debug code to response.write "RS Populated," and this line executes. But then the error still appears directly after that, so I'm guessing this means that somehow the RS is getting closed right away. Not sure why.
Still further update:
I've at least isolated the problem. The err object returns "Query timeout" as the error, and rs.ActiveConnection.CommandTimeout returns "30" as my timeout value.
The query is large and takes at least 60 seconds to run, so I tried setting rs.ActiveConnection.CommandTimeout = 120, but it still fails and - most maddeningly - it still returns "30" as the timeout value.
If rs.ActiveConnection.CommandTimeout = 120 didn't work to increase the timeout, how can I get that value up?
Problem solved. I had to set the CommandTimeout property at the connection level:
conn.CommandTimeout=120
Now it works.

ASP Classic INSERT script does not create a new record

I am having this really bizarre issue where I have an asp page that tries to action multiple insert scripts one after the other. In this case it is 2 inserts. One of the inserts will run fine and work, the other however, seems to run (ie no errors are thrown to the screen) but no record is created. If I dump the SQL to screen and run in SQL Management Studio, it inserts fine.
Would really appreciate some help if someone has experienced this before.
Code block:
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open strDBConn
Set CreateGRNHeaderEntryCMD = Server.CreateObject("ADODB.Command")
CreateGRNHeaderEntrySQL = "INSERT INTO whGRN(grn_Ref, grn_Created, grn_CreatedBy, grn_Status)"_
& " VALUES('GRN0857-13', GetDate(), 'Scanner','NEW')"
CreateGRNHeaderEntryCMD.CommandText = CreateGRNHeaderEntrySQL
Set CreateGRNHeaderEntryCMD.ActiveConnection = Conn
Set CreateGRNHeaderEntryRS = CreateGRNHeaderEntryCMD.Execute
Found out what was causing the issue, there were a Trigger setup against the database that was performing a rollback.

Run MSAccess pass through query thats runs SQL stored proc asynchronously

As title says trying to run pass through query asynchrnously.
I have tried
db.Execute "QrySSRSOneParameter", dbRunAsync
but this doesnt work.
So I found this code that passes the SQL statement through.
I run the following code but a get a
Could not find stored procedure 'sptest'.
It does exist.
Set ws = DBEngine.CreateWorkspace("ODBCWorkspace", "LESTERASSOCIATE\Malcolm", "access", dbUseODBC)
Set myconn = ws.OpenConnection("TestConnection", dbRunAsync, False, connstring)
Set myqry = myconn.CreateQueryDef("", "EXECUTE sptest")
myconn.Execute "EXECUTE sptest", dbRunAsync
Set myconn = Nothing
Set ws = Nothing
just looking at this code briefly and one thing struck me.
you're setting your connection then creating a query def... then not using the query def
shouldn't the execute line read
myqry.execute(dbRunAsync)