【问题标题】:How to update a selection of SQL records from Access VB?如何从 Access VB 更新选择的 SQL 记录?
【发布时间】:2017-02-23 16:15:12
【问题描述】:

我正在寻求帮助,以创建对所选项目进行以下更新的最简单方法。

我在 SQL 中有一个名为 Item 的表,其中包含字段 ReservationID

我在 Access 中有一个项目列表,我想做的是使用 Access 表单中的记录选择器选择项目,然后使用 SQL 更新命令使用 ReservationID 更新这些项目。

我在某处读到只能选择相邻的记录选择器,这很好,但我真的不知道从哪里开始。

任何指点将不胜感激。谢谢。 ---23/02/2017--- 好的,我将其从函数更改为 Private Sub:

Private Sub cmdReserve_Click()
 Dim i As Long
 Dim frm As Form
 Dim rs As DAO.Recordset
 ' Get the form and its recordset.
 Set frm = Forms![F_SalesOrders_ItemsInStock]
 Set rs = frm.RecordsetClone
 ' Move to the first record in the recordset.
 rs.MoveFirst
 ' Move to the first selected record.
 rs.Move frm.SelTop - 1
 ' Enumerate the list of selected records 
 ' presenting the field contents in a message box.
 For i = 1 To frm.SelHeight
   MsgBox rs![ItemID]
   rs.MoveNext
 Next i
End Sub

但是当我选择记录并点击按钮时没有任何反应

【问题讨论】:

  • 您使用的是 vb.net(.Net 框架,可能是 Visual Studio)还是 VBA(在办公室内,可能是访问宏)?
  • 我使用 Access 2010 作为前端,使用 SQL Express 作为数据库

标签: sql vba ms-access


【解决方案1】:

考虑一个链接表并遍历记录集。

Function DisplaySelectedCompanyNames()
 Dim i As Long
 Dim frm As Form
 Dim rs As DAO.Recordset

 ' Get the form and its recordset.
 Set frm = Forms![Customers]
 Set rs = frm.RecordsetClone

 ' Move to the first record in the recordset.
 rs.MoveFirst

 ' Move to the first selected record.
 rs.Move frm.SelTop - 1

 ' Enumerate the list of selected records presenting
 ' the CompanyName field in a message box.
 For i = 1 To frm.SelHeight
   MsgBox rs![CompanyName]
   rs.MoveNext
 Next i

End Function

【讨论】:

  • 感谢您的回复 Fionnuala。不幸的是,我真的在这里扩展了我的能力,我并没有真正理解代码的目的。我尝试将修改后的版本分配给一个按钮,选择一些记录并单击该按钮,但没有效果。我假设它应该有 msgbox 给我一条消息?
  • 您是否更改了所有表单和列名以匹配您的情况?您收到错误消息了吗?
  • 重要的位是 seltop 和 selheight。
  • 我在表单上放了一个按钮,代码如下,但收到错误消息“过程声明与具有相同名称的事件或过程的描述不匹配”。仅供参考,表单名称是 F_SalesOrders_ItemsInStock,记录来源:v_ItemsInStock(这是一个 SQL 视图)
  • Function cmdReserve_Click() Dim i 只要 Dim frm As Form Dim rs As DAO.Recordset Set frm = Forms![F_SalesOrders_ItemsInStock] Set rs = frm.RecordsetClone rs.MoveFirst rs.Move frm.SelTop - 1 For i = 1 To frm.SelHeight MsgBox rs![ItemID] rs.MoveNext Next i End Function
猜你喜欢
  • 2013-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
相关资源
最近更新 更多