如何访问Access中的选定行? [英] How do I access the selected rows in Access?

查看:138
本文介绍了如何访问Access中的选定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张包含数据表的表格.我想让用户可以选择多个行,单击一个按钮,然后运行一些sql查询并在这些行上执行一些工作.

I have a form which includes a data sheet. I would like to make it possible for a user to select multiple rows, click on a button and have some sql query run and perform some work on those rows.

通过我的VBA代码,我看到如何使用CurrentRecord属性访问最后选择的记录.但是我看不到如何知道在多重选择中选择了哪些行. (我希望我很清楚...)

Looking through my VBA code, I see how I can access the last selected record using the CurrentRecord property. Yet I don't see how I can know which rows were selected in a multiple selection. (I hope I'm clear...)

执行此操作的标准方法是什么?网上访问VBA文档有些晦涩...

What's the standard way of doing this? Access VBA documentation is somewhat obscure on the net...

谢谢!

推荐答案

下面是执行此操作的代码,但有一个问题.

Here is the code to do it, but there is a catch.

Private Sub Command1_Click()
     Dim i As Long
     Dim RS As Recordset
     Dim F As Form

     Set F = Me.sf.Form
     Set RS = F.RecordsetClone

     If F.SelHeight = 0 Then Exit Sub

     ' Move to the first selected record.
     RS.Move F.SelTop - 1

     For i = 1 To F.SelHeight
       MsgBox RS![myfield]
       RS.MoveNext
     Next i

End Sub

这是要抓住的地方: 如果将代码添加到按钮,则用户单击该按钮后,所选内容将立即在网格中丢失(selheight将为零).因此,您需要捕获该信息,并使用计时器或表单上的其他事件将其保存到模块级别的变量中.

Here's the catch: If the code is added to a button, as soon as the user clicks that button, the selection is lost in the grid (selheight will be zero). So you need to capture that info and save it to a module level variable either with a timer or other events on the form.

这里是一篇文章,详细介绍了如何解决渔获物.
http://www.mvps.org/access/forms/frm0033.htm

Here is an article describing how to work around the catch in some detail.
http://www.mvps.org/access/forms/frm0033.htm

捕获2:仅适用于连续的选择.他们无法选择网格中的多行非顺序行.

Catch 2: This only works with contiguous selections. They can't select mutliple non-sequential rows in the grid.

更新:
可能会有更好的事件来捕获此错误,但是这是一个使用我测试过的form.timerinterval属性的有效实现方式(至少在Access 2k3中,但2k7应该可以正常工作)

Update:
There might be a better event to trap this, but here is a working implementation using the form.timerinterval property that i have tested (at least in Access 2k3, but 2k7 should work just fine)

此代码在SUBFORM中,使用该属性获取主表单中的selheight值.

This code goes in the SUBFORM, use the property to get the selheight value in the master form.

Public m_save_selheight As Integer

Public Property Get save_selheight() As Integer
    save_selheight = m_save_selheight
End Property

Private Sub Form_Open(Cancel As Integer)
    Me.TimerInterval = 500
End Sub

Private Sub Form_Timer()
    m_save_selheight = Me.selheight
End Sub

这篇关于如何访问Access中的选定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆