Access 2010 VBA循环未将数据行添加到表中

时间:2018-09-22 00:48:51

标签: access-vba ms-access-2010

这个数据库将使我丧命...我正在尝试以某种形式获取结果并将数据复制到表中。我创建了一个按钮来希望这样做,但是它所做的只是打开没有数据的表。我需要在表中查看的数据可以是成百上千的记录。这是我到目前为止的内容:

`Private Sub Command42_Click()

Dim db As Database
Dim rec As Recordset

Set db = CurrentDb
db.Execute "Delete * FROM Import70_TBL"
Set rec = CurrentDb.OpenRecordset("Import70_tbl")
Do Until rec.EOF
    rec.AddNew
    rec("CCAMPUS") = Me.CCAMPUS
    rec("FUNCAFF") = Me.FUNCAFF
    rec("BUILDING") = Me.BUILDING
    rec("ROOM_NO") = Me.ROOM_NO
    rec("BLDG_NAME") = Me.BLDG_NAME
    rec("ROOMCD") = Me.ROOMCD
    rec("ASF") = Me.ASF
    rec("STATIONS") = Me.STATIONS
    rec("FAC_DEPT") = Me.FAC_DEPT
    rec("PGM_CODE") = Me.PGM_CODE
    rec("CCPEC") = Me.CCPEC
    rec("CCLSIZE") = Me.CCLSIZE
    rec("CRESIZE") = Me.CRESIZE
    rec("NSFDISC") = Me.NSFDISC
    rec("AREA_UNITS") = Me.AREA_UNITS
    rec("BUILDING_ZIP_CODE") = Me.BUILDING_ZIP_CODE
    rec.Update
Loop
rec.Close

DoCmd.OpenTable "Import70_tbl"

End Sub`

很显然,我做错了事,但我无法弄清楚。预先感谢任何可以帮助我的人。

1 个答案:

答案 0 :(得分:0)

您需要使用以下格式的 RecordsetClone

Private Sub Command42_Click()

    Dim db  As DAO.Database
    Dim res As DAO.Recordset 
    Dim rsc As DAO.Recordset 
    Dim fld As DAO.Field

    Set db = CurrentDb

    db.Execute "Delete * From Import70_tbl"
    Set rec = db.OpenRecordset("Import70_tbl")
    Set rcs = Me.RecordsetClone

    Do Until rcs.EOF
        rec.AddNew
            For Each fld in rcs.Fields
                res.Fields(fld.Name).Value = fld.Value
            Next
        rec.Update
        rcs.MoveNext
    Loop
    rec.Close
    rcs.Close        

    DoCmd.OpenTable "Import70_tbl"

End Sub