'引用microsoft activex data object2.5 library Private Sub DataGrid1_AfterColEdit(ByVal ColIndex As Integer) Dim cn As New ADODB.Connection Dim sql As String Dim i As Integer cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\tyq.mdb;Persist Security Info=False;" sql = "select * from 学生信息 where 学号=" & DataGrid1.Columns("学号").Value cn.Execute sql, i if i>0 then msgbox "已经有这个学号了,请重新录入学号" DataGrid1.Text="" DataGrid1.SetFocus end if End Sub
首先库表里要学号设为主字段,其次在光标离开学号列时用ado+sql语句判断学号是否重复
Private Sub DataGrid1_AfterColEdit(ByVal ColIndex As Integer)
Dim cn As New ADODB.Connection
Dim sql As String
Dim i As Integer
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\tyq.mdb;Persist Security Info=False;"
sql = "select * from 学生信息 where 学号=" & DataGrid1.Columns("学号").Value
cn.Execute sql, i
if i>0 then
msgbox "已经有这个学号了,请重新录入学号"
DataGrid1.Text=""
DataGrid1.SetFocus
end if
End Sub