简体   繁体   中英

Executing SQL query from VBA - run time error

I try to execute the simplest and shortest query from VBA on SQL-Server using ADO. What am I doing wrong?

Sub VBA_to_SQL()
    Dim cnn As ADODB.Connection
    Dim ConnectionString As String
    Set cnn = New ADODB.Connection

    cnn.ConnectionString = "driver={SQL Server};server=MyServerName;Database=MyDataBaseName;Trusted_Connection=yes;"
    cnn.Open

    If cnn.State = adStateOpen Then
       Set rs = cnn.Execute("begin transaction insert_value; delete from dbo.MyTable;COMMIT TRANSACTION insert_value;")
        cnn.Close
        MsgBox "Got through!"
    Else
        MsgBox "Sorry. No way today."
    End If
End Sub

I keep getting an error on the line Set rs = cnn.Execute("begin transaction insert_value; delete from dbo.MyTable;;COMMIT TRANSACTION insert_value;") The error message is Run-time error '-2147217908 (80040e31)'

If I comment the error-causing line I manage to get through to the server. So the connection is established, however the query is not executed.

Update This code worked well for several days and all of a sudden stopped. Too bad!

I have found the worm and crunched it. Running this on SQL Server helped. Thank you all for care.

ALTER DATABASE [MyDataBaseName] 
SET RECOVERY SIMPLE;
CHECKPOINT;

DBCC SHRINKFILE (MyDataBaseName_log,0);

ALTER DATABASE [MyDataBaseName]
MODIFY FILE (NAME=MyDataBaseName_log,SIZE=8000MB,MAXSIZE=UNLIMITED,FILEGROWTH=1000MB);
ALTER DATABASE [MyDataBaseName] 
SET RECOVERY FULL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM