在Access 2010中更新记录集

时间:2013-12-09 14:38:44

标签: sql vb.net ms-access-2010

我需要在VB中进行简单的更新查询,我可以更新表格中最后一条记录中的某些字段。

enter image description here

我有一组带有一组组合框的表单,当我按下Edit按钮时,我应该可以从组合框中选择不同的值,当我按下Save时,它应该更新最后一个在我的表格中记录我的组合框中的最新选项。请指教。

我有以下代码,但它只是垃圾,它不会在我的表格中编辑任何内容。

Private Sub Command51_Click()
Dim RS As DAO.Recordset
Dim RecID As Integer
Set RS = CurrentDb.OpenRecordset("Data", dbOpenDynaset)
RecID = DLast("[ID]")
RS.MoveFirst
Do Until RS.EOF
If RS("ID") = RecID Then
    RS.Edit
    RS("WLAN") = Me.Text34
    RS("Controller Version") = Me.Text38
    RS("AP Model") = Me.Text36
    RS("Security") = Me.Text39
    RS("Wired Network") = Me.Text37
    RS("Installation Type") = Me.Text40
    RS("Quoted Device") = Me.Text41
    RS.Update
    RS.Close

     End With

     MsgBox "Device info were edited and stored.", vbExclamation

End Sub

这是表结构[已编辑]

enter image description here

1 个答案:

答案 0 :(得分:0)

根据您告诉我们的内容,我会将默认值更改为Form_Current事件上表单上每个控件的当前值。此外,在“表单设计”模式下,将所有控件设置为“已锁定”。当用户点击“编辑”按钮时,我只需移动到新记录。当然,除非您锁定了所有控件,否则用户将能够编辑当前记录。请注意,我没有测试过此代码。

Private Sub Form_Current()
   On Error Resume Next   'not all controls have the "DefaultValue" property, such as labels'
   Dim ctl as Control
   For Each ctl in Me.Controls
       ctl.Locked = Not Me.NewRecord
       ctl.DefaultValue = ctl.Value
   Next ctl
End Sub

Private Sub cmdEdit_Click()
   Access.Application.DoCmd.RunCommand acCmdRecordsGoToNew
End Sub

Private Sub cmdSave_Click()
   Me.Dirty = False
End Sub