如何在 MS Access 中转到下一条记录
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要使用绑定表单而不是未绑定表单。
听起来您正在尝试重新发明 Access 中内置的功能。要将表单从未绑定表单更改为绑定表单,请执行以下操作:
Table_Emp_Info
将表单直接绑定到查询的基础表)。现在,您只需使用表单左下角的 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:
SELECT * FROM Table_Emp_Info
in the RecordSource property (you could also simply enterTable_Emp_Info
to bind the form directly to the query's underlying table).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".
您的代码看起来没问题,但您在每次迭代中将
Table_Emp_Info
中的值写入相同的位置:这使得位置保存循环结束时最后一行的值。这是你的意图,还是你想要一些不同的东西?
编辑
根据您的评论判断,您想要单步执行
Table_Emp_Info
的元组。在这种情况下,您不需要循环 - 单击应该执行 rst.MoveNext 并更新显示数据的字段。类似这样的东西可能会起作用,但有一个免责声明:我对 Access 模型的工作原理几乎一无所知。
Your code looks ok, but you are writing the values from
Table_Emp_Info
to the same locations in every iteration: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 therst.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.
在我看来,您应该使用单个 SQL UPDATE 语句来执行更新,而不是遍历表单和记录集并逐条记录地复制数据。
但是,您没有为我提供足够的信息来提供示例 SQL,因为我无法知道您的目标表单与源数据的关系。
您可能正在创建新记录并从记录集中复制数据,在这种情况下您将使用 SQL INSERT 语句而不是 UPDATE,但这里没有足够的信息。
顺便说一句,
作为一个保护子句,当记录集不返回任何内容时禁止执行循环,DAO 中的常用方法是检查记录集的 .Recordcount 属性是否为 0:
您实际上不想退出,因为您还没有如果您这样做,请不要关闭您的记录集。
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:
You actually don't want to EXIT, because you haven't closed your recordset if you do.
尝试在 do 循环之前执行此操作:
Try doing this before your do loop: