2012-01-24 45 views
0

我正在嘗試從訪問數據庫執行多次插入到遠程SQL Server。到目前爲止,我沒有運氣。當我嘗試在工作區和事務中進行編碼時,我收到數據不匹配錯誤,但功能insert單獨工作得很好。Microsoft Access - 插入遠程數據庫時發生多次事務

這裏是我的代碼:交易之一已被分離的INSERT語句,並把dbAccess.Execute各一個註釋掉

Private Sub cmdInsSqlSrvr_Click() 
On Error GoTo ErrHandler 

    Dim dbAccess As DAO.Database 
    Dim strTableName As String 
    Dim strSQL As String 
    Dim strSqlServerDB As String 
    Dim strTableName2 As String 
    Dim cInTrans As Boolean 
    Dim wsp As DAO.Workspace 


    strTableName = "po_header_sql" 
    strTableName2 = "po_line_Sql" 

    '<configuration specific to SQL Server ODBC driver> 
    strSqlServerDB = "ODBC;DRIVER={SQL Server};" & _ 
        "Server=;" & _ 
        "DATABASE=;" & _ 
        "Uid=;" & _ 
        "Pwd=;" 


    'Start Transaction One 
    'Set dbAccess = DBEngine(0)(0) 

' strSQL = "INSERT INTO [" & strSqlServerDB & "].TABLE3 SELECT * FROM " & strTableName & ";" 
    'dbAccess.Execute strSQL, dbFailOnError 
    'InitConnect = True 

    'MsgBox (dbAccess.RecordsAffected & " records have been moved from " & strTableName & " to remote DB") 
    'Command9.SetFocus 
    'cmdInsSqlSrvr.Enabled = False 
    'cmdInsertTbl.Enabled = True 

' End Transaction One 

'Begin Transaction Two 

    Set wsp = DBEngine(0)(0) 
    wsp.BeginTrans 
    Set dbAccess = wsp(0) 
    cInTrans = True 

    strSQL = "INSERT INTO [" & strSqlServerDB & "].TABLE4 SELECT * FROM " & strTableName2 & ";" 
    dbAccess.Execute strSQL, dbFailOnError 
    InitConnect = True 

    MsgBox (dbAccess.RecordsAffected & " records have been moved from " & strTableName & " to remote DB") 
    wsp.CommitTrans 
    cInTrans = False 
    Command9.SetFocus 
    cmdInsSqlSrvr.Enabled = False 
    cmdInsertTbl.Enabled = True 

'End Transaction Two 

ExitProcedure: 
    On Error Resume Next 
    Set dbAccess = Nothing 
Exit Sub 

ErrHandler: 
    InitConnect = False 
    MsgBox Err.Description, vbExclamation, "Moving data to Sql Server failed: Error " & Err.Number 
    Resume ExitProcedure 

End Sub 
+0

,我不認爲你可以運行「插入」這樣的語句訪問,將插入到遠程數據庫,只到本地Access數據庫,但它不會識別表名。 –

回答

0

固定它。還大量清理了代碼。代碼如下:

Access版本是否使用的是
Private Sub cmdInsSqlSrvr_Click() 
On Error GoTo ErrHandler 

    Dim dbAccess As DAO.Database 
    Dim strTableName As String 
    Dim strSQL As String 
    Dim strSqlServerDB As String 
    Dim strTableName2 As String 

    strTableName = "po_header_sql" 
    strTableName2 = "po_line_Sql" 

    '<configuration specific to SQL Server ODBC driver> 
    strSqlServerDB = "ODBC;DRIVER={SQL Server};" & _ 
        "Server=<server ip>;" & _ 
        "DATABASE=<database name>;" & _ 
        "Uid=<database uid>;" & _ 
        "Pwd=<database password>;" 

    Set dbAccess = DBEngine(0)(0) 

    strSQL = "INSERT INTO [" & strSqlServerDB & "].TABLE3 SELECT * FROM " & strTableName & ";" 
    dbAccess.Execute strSQL, dbFailOnError 

    MsgBox (dbAccess.RecordsAffected & " records have been moved from " & strTableName & " to remote DB") 

    strSQL = "INSERT INTO [" & strSqlServerDB & "].TABLE4 SELECT * FROM " & strTableName2 & ";" 
    dbAccess.Execute strSQL, dbFailOnError 
    InitConnect = True 

    MsgBox (dbAccess.RecordsAffected & " records have been moved from " & strTableName2 & " to remote DB") 
    Command9.SetFocus 
    cmdInsSqlSrvr.Enabled = False 
    cmdInsertTbl.Enabled = True 


ExitProcedure: 
    On Error Resume Next 
    Set dbAccess = Nothing 
Exit Sub 

ErrHandler: 
    InitConnect = False 
    MsgBox Err.Description, vbExclamation, "Moving data to Sql Server failed: Error " & Err.Number 
    Resume ExitProcedure 

End Sub