位置:首頁(yè) > 軟件操作教程 > 辦公軟件 > Excel > 問(wèn)題詳情

如何使用Recordset對(duì)象的方法操作數(shù)據(jù)庫(kù)

提問(wèn)人:周麗平發(fā)布時(shí)間:2021-08-23

1、啟動(dòng)Excel,打開(kāi)Visual Basic編輯器,在工程資源管理器中插入一個(gè)模塊,在模塊的“代碼”窗口中輸入如下程序代碼:

Sub EditRecord()

Dim conn As New ADODB.Connection

Dim rs As New ADODB.Recordset

Dim fld As ADODB.Field

Dim arrFld As Variant,arrValue As Variant

conn.Provider="Microsoft.ACE.OLEDB.12.0"

conn.ConnectionString="data source"="&_ThisWorkbook.Path&"\羅斯文2007.accdb"

conn.Mode=adModeReadWrite

conn.Open

rs.CursorLocation=adUseClient

rs.Open"運(yùn)貨商",conn,adOpenDynamic,adLockOptimistic

Debug.Print"添加前記錄數(shù):"&rs.RecordCount

arrFld=Array("公司","地址","城市","省/市/自治區(qū)",_"郵政編碼","國(guó)家/地區(qū)")

arrValue=Array("UPS","福田車(chē)公廟工業(yè)區(qū)泰然九路",_"深圳","廣東","518040","中國(guó)")

rs.AddNew arrFld,arrValue

Debug.Print"添加前記錄數(shù):"&rs.RecordCount

End Sub

2、按F5鍵運(yùn)行該過(guò)程,此時(shí)在“立即窗口”中將顯示記錄添加結(jié)果,如圖1所示。打開(kāi)Access,查看“運(yùn)貨商”數(shù)據(jù)表,在該數(shù)據(jù)表中添加了新的字段,如圖2所示。

image.png

圖1 “立即窗口”的輸出結(jié)果

image.png

圖2 “運(yùn)貨商”數(shù)據(jù)表實(shí)際效果

繼續(xù)查找其他問(wèn)題的答案?

回復(fù)(0)
返回頂部