存储过程成功备份数据库但VBA代码报告错误

时间:2016-11-27 00:05:13

标签: sql-server stored-procedures access-vba

我已经编写了一个存储过程来备份我的SQL Server数据库,但是当我从Access VBA调用它时,即使备份成功,它也会被错误处理程序选中。存储过程是:

ALTER PROCEDURE [dbo].[spBackupDatabase]
    @full bit,
    @bakFile nvarchar(500) OUTPUT
AS
BEGIN

    SET NOCOUNT ON;

    DECLARE @filepath nvarchar(250)
    DECLARE @filename nvarchar(100)
    DECLARE @backupCommand nvarchar(500)

    /* Get @filepath and @filename and use these to set @bakFile - this bit works fine */

    SET @backupCommand = 
        N'BACKUP ' + CASE @full WHEN 1 THEN N'DATABASE' ELSE N'LOG' END + N' [myDatabase] 
            TO  DISK = N''' + @bakFile + N'''
            WITH NOFORMAT, 
            NOINIT,  
            NAME = N''myDatabase -' + CASE @full WHEN 1 THEN N'Full Database' ELSE N'Database Log' END + N' Backup'', 
            SKIP, 
            NOREWIND, 
            NOUNLOAD,  
            STATS = 10'

    EXEC sp_executesql @backupCommand

END

存储过程正在运行并正确执行备份,但是当我从Access中的VBA代码调用它时,由于某种原因它认为它导致了错误,并且代码会下降到我的错误处理程序。 VBA代码是:

Private Sub cmdBackup_Click()
On Error GoTo PROC_ERR
    Dim conDB As New ADODB.Connection
    Dim cmdBackup As New ADODB.Command
    Dim prmRetVal As ADODB.Parameter
    Dim prmFull As ADODB.Parameter
    Dim prmBakFile As ADODB.Parameter

    conDB.ConnectionString = gConnectionString
    conDB.Open

    cmdBackup.ActiveConnection = conDB
    cmdBackup.CommandType = adCmdStoredProc
    cmdBackup.CommandText = "spBackupDatabase"
    Set prmRetVal = cmdBackup.CreateParameter("retVal", adInteger, adParamReturnValue)
    cmdBackup.Parameters.Append prmRetVal
    Set prmFull = cmdBackup.CreateParameter("Full", adBoolean, adParamInput)
    cmdBackup.Parameters.Append prmFull
    Set prmBakFile = cmdBackup.CreateParameter("BakFile", adBoolean, adParamOutput)
    cmdBackup.Parameters.Append prmBakFile

    Select Case MsgBox("Perform FULL backup (select 'No' for an incremental backup)", vbQuestion + vbYesNoCancel, "Confirm backup type...")
        Case vbYes
            prmFull = True
        Case vbNo
            prmFull = False
        Case vbCancel
            GoTo PROC_EXIT
    End Select
    cmdBackup.Execute , , adExecuteNoRecords
    If prmRetVal.Value = 1 Then
        MsgBox "Application parameter 'BackupPath' has not been set. Please contact developer for assistance.", vbExclamation, "Unable to perform backup"
    Else
        MsgBox "Backup successfully created at " & prmBakFile, vbInformation + vbOKOnly, "Done..."
    End If

PROC_EXIT:
    On Error Resume Next
    Set prmRetVal = Nothing
    Set prmFull = Nothing
    Set prmBakFile = Nothing
    Set cmdBackup = Nothing
    conDB.Close
    Set conDB = Nothing
Exit Sub

PROC_ERR:
    Call ShowError("Form_sfmMaintenance", "cmdBackup_Click", Err.Number, Err.Description)
    Resume PROC_EXIT

End Sub

代码在点击PROC_ERR时会一直显示到cmdBackup.Execute , , adExecuteNoRecords,并报告错误编号-2147217913以及" BACKUP LOG已成功处理2页的错误说明...& #34;

任何想法如何阻止来自服务器的此成功消息被我的VBA代码解释为错误,同时仍然发现真正的错误(例如,如果备份位置无效)?

2 个答案:

答案 0 :(得分:1)

我不确定是否有更好的方法可以首先避免错误,但最简单的方法是将Call ShowError包装在if语句中,该语句检查该错误号,也可能是包含“成功”的描述。在我的手机上,如If Not (Err.Number = -2147217913 And Err.Description.Contains("successfully")) Then

编辑:要处理返回代码中的上一个点:

PROC_ERR:
If (Err.Number = -2147217913 And Err.Description.Contains("successfully")) Then
    'This error is received on success. Continue.
    Resume Next
Else
    Call ShowError("Form_sfmMaintenance", "cmdBackup_Click", Err.Number, Err.Description)
    Resume PROC_EXIT
End If

答案 1 :(得分:0)

谢谢@Andre和@SMM。错误是数据类型不匹配的真正错误。我更正了prmBakFile的数据类型,它现在就像一个梦想。