程序运行一会之后出现无当前记录错误,运行之后刚开始是可以的
Public Class Form1vb.NET 无当前记录
'--------------------------------------------
'以下定义了一些DAO对象
'和一些SQL打开数据库字符串
'设计了一个OpenDB()函数
'用DAO对象打开了数据库
'--------------------------------------------
Dim Mydb As dao.Database
'定义一个DAO.Database数据库对象
Dim Myws As dao.Workspace
'定义一个工作空间DAO.Database
Dim Mysql As String
Dim Myrec As dao.Recordset
'定义一个数据项
Dim de As dao.DBEngine = New dao.DBEngine()
'定义一个数据引擎
Public Function OpenDb() As Short
'定义打开数据的函数
Dim dbstr As String
'存放数据库的目录
dbstr = "F:\VB.NET信息系统\学生名册管理系统\Student.mdb"
Myws = de.Workspaces(0)
'建立一个工作空间
Mydb = Myws.OpenDatabase(dbstr, False, False)
'打开数据存放于Mydb
dbstr = "select * from 学生名册"
'设置打开方式
Myrec = Mydb.OpenRecordset(dbstr, dao.RecordsetTypeEnum.dbOpenDynaset)
End Function
Public Sub New()
'构造子程序
MyBase.New()
'对基础类库进行初始化
'此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
Dim myResult As Short
myResult = OpenDb()
'调用openDb()打开数据库
End Sub
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
'获取数据中的数据
TxtNumber.Text = Myrec.Fields(0).Value.ToString
TxtName.Text = Myrec.Fields(1).Value.ToString
TxtDate.Text = Myrec.Fields(2).Value.ToString.Substring(0, 9)
TxtTel.Text = Myrec.Fields(3).Value.ToString
End Sub
Private Sub BtNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtNext.Click
'当单机下一条按钮时的操作
If Myrec.EOF = True Then
Beep()
Else
Myrec.MoveNext()
If Myrec.EOF = True Then
Exit Sub
End If
TxtNumber.Text = Myrec.Fields(0).Value.ToString
TxtName.Text = Myrec.Fields(1).Value.ToString
TxtDate.Text = Myrec.Fields(2).Value.ToString.Substring(0, 9)
TxtTel.Text = Myrec.Fields(3).Value.ToString
End If
End Sub
Private Sub BtPre_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtPre.Click
'当单机下一条按钮时的操作
If Myrec.BOF = True Then
Beep()
Else
Myrec.MovePrevious()
If Myrec.BOF = True Then
Exit Sub
End If
TxtNumber.Text = Myrec.Fields(0).Value.ToString
TxtName.Text = Myrec.Fields(1).Value.ToString
TxtDate.Text = Myrec.Fields(2).Value.ToString.Substring(0, 9)
TxtTel.Text = Myrec.Fields(3).Value.ToString
End If
End Sub
Private Sub BtFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtFirst.Click
Myrec.MoveFirst()
TxtNumber.Text = Myrec.Fields(0).Value.ToString
TxtName.Text = Myrec.Fields(1).Value.ToString
TxtDate.Text = Myrec.Fields(2).Value.ToString.Substring(0, 9)
TxtTel.Text = Myrec.Fields(3).Value.ToString
End Sub
Private Sub BtLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtLast.Click
Myrec.MoveLast()
TxtNumber.Text = Myrec.Fields(0).Value.ToString
TxtName.Text = Myrec.Fields(1).Value.ToString
TxtDate.Text = Myrec.Fields(2).Value.ToString.Substring(0, 9)
TxtTel.Text = Myrec.Fields(3).Value.ToString
End Sub
Private Sub BtAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtAdd.Click
Dim dbstr As String
dbstr = "select * from 学生名册 where 学号='" & TxtNumber.Text & "'"
'建立查询表达式
Myrec = Mydb.OpenRecordset(dbstr, dao.RecordsetTypeEnum.dbOpenDynaset)
If Myrec.EOF Then
Myrec.AddNew()
'增加新记录
Myrec.Fields(0).Value = TxtNumber.Text
Myrec.Fields(1).Value = TxtName.Text
Myrec.Fields(2).Value = TxtDate.Text
Myrec.Fields(3).Value = TxtTel.Text
Myrec.Update()
TxtNumber.Text = ""
TxtName.Text = ""
TxtDate.Text = ""
TxtTel.Text = ""
Else
'判断是否存在当前记录,如果存在就执行下面操作
MsgBox("这个学号已经存在了, 如果需要更改这个人的信息, 请选择[编辑]按钮。", Microsoft.VisualBasic.MsgBoxStyle.OkOnly, "信息框")
TxtNumber.Text = ""
TxtName.Text = ""
TxtDate.Text = ""
TxtTel.Text = ""
End If
dbstr = "select * from 学生名册"
Myrec = Mydb.OpenRecordset(dbstr, dao.RecordsetTypeEnum.dbOpenDynaset)
End Sub
Private Sub BtEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtEdit.Click
Dim dbstr As String
dbstr = "select * from 学生名册 where 学号='" & TxtNumber.Text & "'"
'建立查询表达式
Myrec = Mydb.OpenRecordset(dbstr, dao.RecordsetTypeEnum.dbOpenDynaset)
If Myrec.EOF Then
MsgBox("这个学号不存在, 如果需要增加这个人的信息, 请选择[增加]按钮。", Microsoft.VisualBasic.MsgBoxStyle.OkOnly, "信息框")
End If
End Sub
End Class