2016-02-01 106 views
0

我試過以下代碼, 1)Button3_Click()...對於添加新記錄,沒有錯誤消息,但沒有記錄添加到數據庫 2) Button2_Click()...對於更新記錄,顯示「無法從程序集'WindowsApplication1,版本= 1.0.0.0,Culture = neutral,PublicKeyToken = null'加載類型'ADODB.FieldsToInternalFieldsMarshaler'」。VB 2012:使用ADO記錄集修改記錄MSACCESS

有人可以幫忙。

Public Class Form1 



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    'update 1st record 
    Dim rs As New ADODB.Recordset 

    Dim cn As New ADODB.Connection() 
    cn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Test.mdb;" 
    cn.Open() 

    rs.Open("SELECT * FROM Users", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockBatchOptimistic) 


    rs.MoveFirst() 
    rs.Fields("EmpName").Value = "AAAAA" 
    rs.Fields("EmpId").Value = "222" 
    rs.UpdateBatch() 

    rs.Close() 
    cn.Close() 
End Sub 

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
    'add new Record 

    Dim rs As New ADODB.Recordset 

    Dim cn As New ADODB.Connection() 
    cn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Test.mdb;" 
    cn.Open() 

    rs.Open("SELECT * FROM Users", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockBatchOptimistic) 

    Dim fieldsArray(1) As Object 
    fieldsArray(0) = "EmpName" 
    fieldsArray(1) = "EmpId" 
    Dim values(1) As Object 
    values(0) = "4" 
    values(1) = "as" 

    rs.AddNew(fieldsArray, values) 
    rs.UpdateBatch() 

    rs.Close() 
    cn.Close() 
End Sub 

末級

回答

0

見我的代碼,我測試和它的工作:表1有兩個字段。我添加了一個有兩個值的行。

Dim cnn As New ADODB.Connection 
    Set cnn = CurrentProject.Connection 
    Dim rs As New ADODB.Recordset 
    Set rs = New ADODB.Recordset 
    rs.Open "Table1", cnn, adOpenKeyset, adLockOptimistic 

    rs.AddNew 
    rs.Fields(1) = "a" 
    rs.Fields(2) = "b" 
    rs.UpdateBatch 
+0

我覺得這是VBScript中,需要此VB2012 –

+0

不,它是VBA,ADODB相同始終。 –