一:全名
EOF: End of File。
BOF: Before of File
EOF 指示当前记录位置位于 Recordset 对象的最后一个记录之后,用于判断记录知否到末尾
BOF 指示当前记录位置位于 Recordset 对象的第一个记录之前, 用于判断记录是否在最前面。
下图是BOF .EOF指的位置
BOF——>
记录1 |
记录2 |
...... |
三:含义
说明:
![](https://img-blog.csdn.net/20160518102857746?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
EOF()是一个函数,所以它有函数值,它的函数值有两个true和false。
EOF的含义是:读取文件的指针指到文件尾时,它的值就是true(-1),还没有读到文件尾时,它的值为false(0)。
BOF的含义: 如果当前记录位于第一个记录之前,BOF 属性将返回 True (-1),如果当前记录为第一个记录或位于其后则将返回 False (0)。
说明:
1. Eof(1)它的()里的1就是参数。括号里的参数是它判断的文件的文件号。
2. if EOF(1) then
解释:
判断如果EOF(1)的值为true则进行then后面的语句。
EOF | BOF | |
True | 当前行的位置是在最后一行的后面,无记录 | 当前行的位置是在第一行之前,无记录 |
false | 当前位置是在最后一行或其前面,有记录 | 当前位置是在第一行或其后,有记录 |
(1)EOF 或BOF值为true时,说明当前无记录
(2)EOF,BOF同时为true时,说明记录集recordset无记录
四:实例化
最近在敲学生信息管理时经常用到EOF\BOF,而且经常会在带EOF\BOF代码处出现错误 3021
原因:记录集recordset中根本没记录(执行该步前没添加或都已删除),其实错误提示写的已经很清楚了
解决办法:
以学生管理为例:
1 . 加载时没记录,可添加如下代码:
If mrc.BOF Then
str = (MsgBox("已没有可修改的记录了", vbOKOnly + vbExclamation, "提示"))
If str = vbOK Then
'处于不可编辑状态
Frame1.Enabled = False
firstCommand.Enabled = False
previousCommand.Enabled = False
nextCommand.Enabled = False
lastCommand.Enabled = False
txtSID.Enabled = False
txtName.Enabled = False
ComboSex.Enabled = False
txtBorndate.Enabled = False
ComboClassno.Enabled = False
txtTel.Enabled = False
txtRutedate.Enabled = False
txtAddress.Enabled = False
txtComment.Enabled = False
editCommand.Enabled = False
updateCommand.Enabled = False
cancelCommand.Enabled = False
deleteCommand.Enabled = False
txtSID.Text = ""
txtName.Text = ""
ComboSex.Text = ""
txtBorndate.Text = ""
ComboClassno.Text = ""
txtTel.Text = ""
txtRutedate.Text = ""
txtAddress.Text = ""
txtComment.Text = ""
Exit Sub
Else
'处于不可编辑状态
Frame1.Enabled = False
firstCommand.Enabled = False
previousCommand.Enabled = False
nextCommand.Enabled = False
lastCommand.Enabled = False
txtSID.Enabled = False
txtName.Enabled = False
ComboSex.Enabled = False
txtBorndate.Enabled = False
ComboClassno.Enabled = False
txtTel.Enabled = False
txtRutedate.Enabled = False
txtAddress.Enabled = False
txtComment.Enabled = False
editCommand.Enabled = False
updateCommand.Enabled = False
cancelCommand.Enabled = False
deleteCommand.Enabled = False
txtSID.Text = ""
txtName.Text = ""
ComboSex.Text = ""
txtBorndate.Text = ""
ComboClassno.Text = ""
txtTel.Text = ""
txtRutedate.Text = ""
txtAddress.Text = ""
txtComment.Text = ""
Exit Sub
End If
Else
mrc.MoveFirst
End If
If str = vbOK Then
'处于不可编辑状态
Frame1.Enabled = False
firstCommand.Enabled = False
previousCommand.Enabled = False
nextCommand.Enabled = False
lastCommand.Enabled = False
txtSID.Enabled = False
txtName.Enabled = False
ComboSex.Enabled = False
txtBorndate.Enabled = False
ComboClassno.Enabled = False
txtTel.Enabled = False
txtRutedate.Enabled = False
txtAddress.Enabled = False
txtComment.Enabled = False
editCommand.Enabled = False
updateCommand.Enabled = False
cancelCommand.Enabled = False
deleteCommand.Enabled = False
txtSID.Text = ""
txtName.Text = ""
ComboSex.Text = ""
txtBorndate.Text = ""
ComboClassno.Text = ""
txtTel.Text = ""
txtRutedate.Text = ""
txtAddress.Text = ""
txtComment.Text = ""
Exit Sub
Else
'处于不可编辑状态
Frame1.Enabled = False
firstCommand.Enabled = False
previousCommand.Enabled = False
nextCommand.Enabled = False
lastCommand.Enabled = False
txtSID.Enabled = False
txtName.Enabled = False
ComboSex.Enabled = False
txtBorndate.Enabled = False
ComboClassno.Enabled = False
txtTel.Enabled = False
txtRutedate.Enabled = False
txtAddress.Enabled = False
txtComment.Enabled = False
editCommand.Enabled = False
updateCommand.Enabled = False
cancelCommand.Enabled = False
deleteCommand.Enabled = False
txtSID.Text = ""
txtName.Text = ""
ComboSex.Text = ""
txtBorndate.Text = ""
ComboClassno.Text = ""
txtTel.Text = ""
txtRutedate.Text = ""
txtAddress.Text = ""
txtComment.Text = ""
Exit Sub
End If
Else
mrc.MoveFirst
End If
2.把记录中的记录全删除时,可编写代码:
If str2$ = vbOK Then
mrc.MoveNext '删除后显示下一条记录
If mrc.EOF Then '分两种情况
mrc.MovePrevious
mrc.MovePrevious
If mrc.BOF Then ’记录中有且只有一条记录,且将要删除
mrc.MoveNext
mrc.Delete
MsgBox "最后一条记录已经删除!", vbOKOnly + vbExclamation, "提示"
'处于不可编辑状态
Frame1.Enabled = False
firstCommand.Enabled = False
previousCommand.Enabled = False
nextCommand.Enabled = False
lastCommand.Enabled = False
txtSID.Enabled = False
txtName.Enabled = False
ComboSex.Enabled = False
txtBorndate.Enabled = False
ComboClassno.Enabled = False
txtTel.Enabled = False
txtRutedate.Enabled = False
txtAddress.Enabled = False
txtComment.Enabled = False
mrc.MoveNext '删除后显示下一条记录
If mrc.EOF Then '分两种情况
mrc.MovePrevious
mrc.MovePrevious
If mrc.BOF Then ’记录中有且只有一条记录,且将要删除
mrc.MoveNext
mrc.Delete
MsgBox "最后一条记录已经删除!", vbOKOnly + vbExclamation, "提示"
'处于不可编辑状态
Frame1.Enabled = False
firstCommand.Enabled = False
previousCommand.Enabled = False
nextCommand.Enabled = False
lastCommand.Enabled = False
txtSID.Enabled = False
txtName.Enabled = False
ComboSex.Enabled = False
txtBorndate.Enabled = False
ComboClassno.Enabled = False
txtTel.Enabled = False
txtRutedate.Enabled = False
txtAddress.Enabled = False
txtComment.Enabled = False
editCommand.Enabled = False
updateCommand.Enabled = False
cancelCommand.Enabled = False
deleteCommand.Enabled = False
txtSID.Text = ""
txtName.Text = ""
ComboSex.Text = ""
txtBorndate.Text = ""
ComboClassno.Text = ""
txtTel.Text = ""
txtRutedate.Text = ""
txtAddress.Text = ""
txtComment.Text = ""
Else '有多条记录,要删除的是最后一条,删除后显示第一条
mrc.MoveFirst
myBookmark = mrc.Bookmark
mrc.MoveLast
mrc.Delete
mrc.Bookmark = myBookmark
Call viewData
End If
Else ' 删除的记录不是最后一条,删除后显示下一条
myBookmark = mrc.Bookmark
mrc.MovePrevious
mrc.Delete
mrc.Bookmark = myBookmark
Call viewData
End If
Else '不删除记录
mrc.Bookmark = myBookmark '取消标记
Call viewData
End If