如何在 MS Access 中转到下一条记录

发布于 2024-11-24 12:15:00 字数 784 浏览 1 评论 0原文

我是 MS Access 2007 编程的新手.. 我想从数据库中一一获取信息。但是我正在使用的代码没有执行该信息。如果我在第一条记录上,那么它会直接转到我不想要的最后一条记录。我想把它带到下一个记录.. 这是我的代码:

Private Sub MoveNextBttn_Click()
    Dim db As Database
    Set db = CurrentDb
    Dim str As String
    str = "SELECT * FROM Table_Emp_Info"
    Dim rst As Recordset
    Set rst = db.OpenRecordset(str)
    Dim xxx As Integer
    xxx = 1

    'If the recordset is empty, exit.
    If rst.EOF Then
       Exit Sub
    End If

    Do Until rst.EOF
        Emp_ID_Text.Value = rst.Fields("EmpID")
        Rowsource_Designation.Value = rst.Fields("Designation")
        RowSource_Dept.Value = rst.Fields("Dept")
        DOJ_Text.Value = rst.Fields("Date_Of_Joining")
        rst.MoveNext
         'xxx = xxx + 1
    Loop

    rst.Close

End Sub

I am new in MS access 2007 programming..
I want to get information from database one by one.But the code which i am using is not executing that. If I am on first record then it directly goes to last record which i dont want. I want to take it to the next record..
Here is my code:

Private Sub MoveNextBttn_Click()
    Dim db As Database
    Set db = CurrentDb
    Dim str As String
    str = "SELECT * FROM Table_Emp_Info"
    Dim rst As Recordset
    Set rst = db.OpenRecordset(str)
    Dim xxx As Integer
    xxx = 1

    'If the recordset is empty, exit.
    If rst.EOF Then
       Exit Sub
    End If

    Do Until rst.EOF
        Emp_ID_Text.Value = rst.Fields("EmpID")
        Rowsource_Designation.Value = rst.Fields("Designation")
        RowSource_Dept.Value = rst.Fields("Dept")
        DOJ_Text.Value = rst.Fields("Date_Of_Joining")
        rst.MoveNext
         'xxx = xxx + 1
    Loop

    rst.Close

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

原野 2024-12-01 12:15:00

您需要使用绑定表单而不是未绑定表单。

听起来您正在尝试重新发明 Access 中内置的功能。要将表单从未绑定表单更改为绑定表单,请执行以下操作:

  1. 在设计视图中打开表单。
  2. 按 F4 键调出表单属性表。
  3. 单击“数据”选项卡。
  4. 在 RecordSource 属性中输入 SELECT * FROM Table_Emp_Info(您也可以简单地输入 Table_Emp_Info 将表单直接绑定到查询的基础表)。
  5. 单击“格式”选项卡,并确保“导航按钮”设置为“是”(默认值)。
  6. 摆脱您编写的所有不必要的代码。

现在,您只需使用表单左下角的 5 个按钮即可浏览您的记录。从左到右,这些按钮将带您到第一个、上一个、下一个、最后一个或新记录。

有关详细信息,请搜索“ms access 绑定表单”。

You need to use a bound form instead of an unbound form.

It sounds like you are trying to reinvent functionality that is built into Access. To change your form from an unbound form to a bound form do the following:

  1. Open the form in design view.
  2. Press F4 to bring up the form property sheet.
  3. Click on the Data tab.
  4. Enter SELECT * FROM Table_Emp_Info in the RecordSource property (you could also simply enter Table_Emp_Info to bind the form directly to the query's underlying table).
  5. Click on the Format tab and make sure that Navigation Buttons is set to Yes (the default).
  6. Get rid of all that unnecessary code you wrote.

Now you can simply use the 5 buttons at the bottom left of your form to navigate your records. From left to right the buttons will take you to the First, Previous, Next, Last, or New record.

For more information search on "ms access bound form".

笑叹一世浮沉 2024-12-01 12:15:00

您的代码看起来没问题,但您在每次迭代中将 Table_Emp_Info 中的值写入相同的位置:

    Emp_ID_Text.Value = rst.Fields("EmpID")
    Rowsource_Designation.Value = rst.Fields("Designation")
    RowSource_Dept.Value = rst.Fields("Dept")
    DOJ_Text.Value = rst.Fields("Date_Of_Joining")
    rst.MoveNext

这使得位置保存循环结束时最后一行的值。这是你的意图,还是你想要一些不同的东西?

编辑

根据您的评论判断,您想要单步执行 Table_Emp_Info 的元组。在这种情况下,您不需要循环 - 单击应该执行 rst.MoveNext 并更新显示数据的字段。

类似这样的东西可能会起作用,但有一个免责声明:我对 Access 模型的工作原理几乎一无所知。

private rst As Recordset

Private Sub Form_Open(Cancel As Integer)
    set rst = CurrentDb.OpenRecordset("SELECT * FROM Table_Emp_Info")
    call UpdateForm()
End Sub

private sub UpdateForm()
    if not rst.EOF
        Emp_ID_Text.Value = rst.Fields("EmpID")
        Rowsource_Designation.Value = rst.Fields("Designation")
        RowSource_Dept.Value = rst.Fields("Dept")
        DOJ_Text.Value = rst.Fields("Date_Of_Joining")
    end if
end sub

private sub MoveNextBttn_Click()
    if not rst.EOF then
        rst.MoveNext
        call UpdateForm()
    end if
end sub

Your code looks ok, but you are writing the values from Table_Emp_Info to the same locations in every iteration:

    Emp_ID_Text.Value = rst.Fields("EmpID")
    Rowsource_Designation.Value = rst.Fields("Designation")
    RowSource_Dept.Value = rst.Fields("Dept")
    DOJ_Text.Value = rst.Fields("Date_Of_Joining")
    rst.MoveNext

This makes the locations holding the values of the last row at the end of the loop. Was this your intention, or did you want something different?

Edit

Judging by your comment, you want to step through the tuples of Table_Emp_Info. In that case, you do not want the loop - the click should do the rst.MoveNext and update the fields where the data is displayed.

Something like this could work, but a disclaimer is in place: I know close to nothing about how the Access model works.

private rst As Recordset

Private Sub Form_Open(Cancel As Integer)
    set rst = CurrentDb.OpenRecordset("SELECT * FROM Table_Emp_Info")
    call UpdateForm()
End Sub

private sub UpdateForm()
    if not rst.EOF
        Emp_ID_Text.Value = rst.Fields("EmpID")
        Rowsource_Designation.Value = rst.Fields("Designation")
        RowSource_Dept.Value = rst.Fields("Dept")
        DOJ_Text.Value = rst.Fields("Date_Of_Joining")
    end if
end sub

private sub MoveNextBttn_Click()
    if not rst.EOF then
        rst.MoveNext
        call UpdateForm()
    end if
end sub
怕倦 2024-12-01 12:15:00
Do while not rst.EOF
        Emp_ID_Text.Value = rst.Fields("EmpID")
        Rowsource_Designation.Value = rst.Fields("Designation")
        RowSource_Dept.Value = rst.Fields("Dept")
        DOJ_Text.Value = rst.Fields("Date_Of_Joining")
        rst.MoveNext
         'xxx = xxx + 1
    Loop
Do while not rst.EOF
        Emp_ID_Text.Value = rst.Fields("EmpID")
        Rowsource_Designation.Value = rst.Fields("Designation")
        RowSource_Dept.Value = rst.Fields("Dept")
        DOJ_Text.Value = rst.Fields("Date_Of_Joining")
        rst.MoveNext
         'xxx = xxx + 1
    Loop
凉月流沐 2024-12-01 12:15:00

在我看来,您应该使用单个 SQL UPDATE 语句来执行更新,而不是遍历表单和记录集并逐条记录地复制数据。

但是,您没有为我提供足够的信息来提供示例 SQL,因为我无法知道您的目标表单与源数据的关系。

您可能正在创建新记录并从记录集中复制数据,在这种情况下您将使用 SQL INSERT 语句而不是 UPDATE,但这里没有足够的信息。

顺便说一句,

作为一个保护子句,当记录集不返回任何内容时禁止执行循环,DAO 中的常用方法是检查记录集的 .Recordcount 属性是否为 0:

  'If the recordset is empty, skip the loop.
  If rst.RecordCount <> 0 Then
     Do Until rst.EOF
       Emp_ID_Text.Value = rst.Fields("EmpID")
       Rowsource_Designation.Value = rst.Fields("Designation")
       RowSource_Dept.Value = rst.Fields("Dept")
       DOJ_Text.Value = rst.Fields("Date_Of_Joining")
       rst.MoveNext
       'xxx = xxx + 1
     Loop
  End If
  rst.Close
  Set rst = Nothing ' you omitted this step

您实际上不想退出,因为您还没有如果您这样做,请不要关闭您的记录集。

It looks to me like you should be using a single SQL UPDATE statement to do the update, instead of walking through a form and a recordset and copying the data record-by-record.

However, you don't give enough information for me to provide sample SQL, since there's no way for me to know how your destination form relates to your source data.

It could be that you're creating new records and copying the data in from the recordset, in which case you'd use a SQL INSERT statement instead of an UPDATE, but there just isn't enough information to go on here.

ASIDE:

By the way, as a guard clause to prohibit executing the loop when the recordset returns nothing, the usual method in DAO is to check if the recordset's .Recordcount propert is 0:

  'If the recordset is empty, skip the loop.
  If rst.RecordCount <> 0 Then
     Do Until rst.EOF
       Emp_ID_Text.Value = rst.Fields("EmpID")
       Rowsource_Designation.Value = rst.Fields("Designation")
       RowSource_Dept.Value = rst.Fields("Dept")
       DOJ_Text.Value = rst.Fields("Date_Of_Joining")
       rst.MoveNext
       'xxx = xxx + 1
     Loop
  End If
  rst.Close
  Set rst = Nothing ' you omitted this step

You actually don't want to EXIT, because you haven't closed your recordset if you do.

梦毁影碎の 2024-12-01 12:15:00

尝试在 do 循环之前执行此操作:

rst.movelast

rst.movefirst

Try doing this before your do loop:

rst.movelast

rst.movefirst
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文