2012-05-20 82 views
3

在MS Access VBA(2007)中,我編寫了下面的函數來將DAO記錄集轉換爲斷開連接的內存ADO記錄集。問題是我在DAO dbDecimal字段上有數據類型轉換問題。當我試圖將數據從DAO記錄集插入新創建的ADO記錄集時,問題就出現了。當我到是類型DAO dbDecimal(ADO adNumeric)我得到以下錯誤列:將DAO記錄集轉換爲斷開連接的ADO記錄集dbDecimal問題

Error -2147217887 (80040e21): 
Multiple-step operation generated errors. Check each status value. 

我看了一下,錯誤發生在每次到達此列的時間。此列中包含的數據是簡單的數字,例如25,44,60等。

正如您在下面看到的,我對我的NumericScale和Precision進行了硬編碼,但這似乎沒有任何幫助。

Public Function ConvertDAORStoADORS(ByRef r1 As DAO.Recordset) As ADODb.Recordset 

    If Not r1 Is Nothing Then 

     Dim ra As ADODb.Recordset 
     Set ra = New ADODb.Recordset 

     Dim f1 As DAO.Field, fa As ADODb.Field 

     For Each f1 In r1.Fields 
      Select Case f1.Type 
       Case dbText 
        ra.Fields.Append f1.Name, adVarWChar, f1.Size, adFldIsNullable 
       Case dbMemo 
        ra.Fields.Append f1.Name, adLongVarWChar, 10000, adFldIsNullable 

       'Here's the problematic one 
       Case dbDecimal 
        ra.Fields.Append f1.Name, adNumeric, , adFldIsNullable 
        Set fa = ra.Fields(f1.Name) 
        fa.NumericScale = 19 
        fa.Precision = 4 

       Case 9, dbLongBinary, dbAttachment, dbComplexByte, dbComplexInteger, dbComplexLong, dbComplexText, dbComplexSingle, dbComplexDouble, dbComplexGUID, dbComplexDecimal 
        'Unsupported types 
       Case Else 
        Debug.Print f1.Name & " " & f1.Type 
        ra.Fields.Append f1.Name, GetADOFieldType(f1.Type), , adFldIsNullable 

      End Select 
     Next f1 
     ra.LockType = adLockPessimistic 
     ra.Open 
     'On Error Resume Next 
     If Not (r1.EOF And r1.BOF) Then 
      r1.MoveFirst 
      Do Until r1.EOF = True 
       ra.AddNew 
       For Each f1 In r1.Fields 
        'Error -2147217887 (80040e21) Multiple-step operation generated errors. Check each status value. 
        'Error only occurs on dbDecimal/adNumeric fields 
        ra(f1.Name).value = r1(f1.Name).value 
       Next f1 
       ra.Update 
       r1.MoveNext 
      Loop 
     End If 

     Set ConvertDAORStoADORS = ra 
    End If 

End Function 

Private Function GetADOFieldType(daofieldtype As Integer) As Long 
    Select Case daofieldtype 
     'Fixed width adWChar does not exist 
     Case dbText: GetADOFieldType = adVarWChar 
     Case dbMemo: GetADOFieldType = adLongVarWChar 
     Case dbByte: GetADOFieldType = adUnsignedTinyInt 
     Case dbInteger: GetADOFieldType = adSmallInt 
     Case dbLong: GetADOFieldType = adInteger 
     Case dbSingle: GetADOFieldType = adSingle 
     Case dbDouble: GetADOFieldType = adDouble 
     Case dbGUID: GetADOFieldType = adGUID 
     Case dbDecimal: GetADOFieldType = adNumeric 
     Case dbDate: GetADOFieldType = adDate 
     Case dbCurrency: GetADOFieldType = adCurrency 
     Case dbBoolean: GetADOFieldType = adBoolean 
     Case dbLongBinary: GetADOFieldType = adLongVarBinary 
     Case dbBinary: GetADOFieldType = adVarBinary 
     Case Else: GetADOFieldType = adVarWChar 
    End Select 
End Function 

我從鏈接到MS SQL Server 2008中的字段實際上是一個SQL Server十進制(19,4)數據類型的ODBC鏈接表導出我的DAO記錄。

任何想法如何解決這個問題?

回答

1

這適用於我,但我不確定爲什麼你不只是從表中創建ADODB記錄集並斷開連接。

Case dbDecimal 
     ra.Fields.Append f1.Name, adDecimal, , adFldIsNullable 
     Set fa = ra.Fields(f1.Name) 
     fa.NumericScale = 19 
     fa.Precision = 4 

而且,爲什麼不

Dim ra As New ADODb.Recordset 
    ''Set ra = New ADODb.Recordset 
+0

它同時從DAO記錄到新創建的ADO記錄集移動數據的工作對我來說太,但後來它的錯誤。它總是在我的小數點列上出錯。 – HK1

+0

我的意思是,當我使用adDecimal作爲上述代碼時,它不會在小數點列上出錯,而​​不是adNumeric,這就是您的代碼中所包含的內容。 – Fionnuala