小弟做了一个简单的学生管理系统,其中录入和查询都没有没有问题!!!
但修改却不行!!急死了!!
具体如下:每当我点击“确认修改”时,就会弹出一个系统错误:上面写着"违反了 primary key 约束 ‘pk_student_286302ec'.不能在对象‘student'插入重复键"
我用sql server 作的数据库,其中student是我建的一个表!!
谢谢各位大虾赐教!!
不胜感激!!!
但修改却不行!!急死了!!
具体如下:每当我点击“确认修改”时,就会弹出一个系统错误:上面写着"违反了 primary key 约束 ‘pk_student_286302ec'.不能在对象‘student'插入重复键"
我用sql server 作的数据库,其中student是我建的一个表!!
谢谢各位大虾赐教!!
不胜感激!!!
Dim SQL As String
On Error GoTo ErrMsg
SQL = MsgBox("你真的要修改当前数据吗,请慎重处理!", vbInformation _
+ vbOKCancel + vbDefaultButton2, "系统提示")
If SQL = vbCancel Then
Exit Sub
End If
'判断姓名
If Trim(txtName.Text) = "" Then
MsgBox "姓名不能为空,请核实!", vbOKOnly + vbExclamation, "系统提示"
txtName.SetFocus
Exit Sub
End If
'判断出生日期,以必须是15--30岁之间这例
If (Year(Now) - Year(dtBirthday.Value)) < 15 Or (Year(Now) - Year(dtBirthday.Value)) > 30 Then
MsgBox "输入的出生日期应保证在15--30岁间!", vbOKOnly + vbExclamation, "系统提示"
dtBirthday.SetFocus
Exit Sub
End If'若电子信箱输入,则判断是否是合法的
If Trim(txtEmail.Text) <> "" Then
'?--是任意一个字符,*--零个或任意个字符
'邮箱必须是如下样式的:[email protected]
If txtEmail.Text Like "?*@??*.???*" = False Then
MsgBox "输入的邮箱不是有效的信箱,请核实!", vbOKOnly + vbExclamation, "系统提示"
txtEmail.SetFocus
Exit Sub
End If
End If'若邮政编码为空不处理,若不是空的,则要判断是否是6位数字
If Trim(txtPostCode.Text) <> "" Then
If Len(Trim(txtPostCode.Text)) <> 6 Or Not (txtPostCode.Text Like "[0-9][0-9] [0-9][0-9][0-9][0-9]") Then
MsgBox "邮政编码输入不正确,必须是6位数字,请核实!", vbOKOnly + vbExclamation, "系统提示"
txtPostCode.SetFocus
Exit Sub
End If
End If
SQL = ""
SQL = "update student set id='" & txtID.Text & "',"
SQL = SQL & "Name='" & txtName.Text & "',"
SQL = SQL & "Sex='" & cobSex.Text & "',"
SQL = SQL & "Birthday='" & Format(dtBirthday.Value, "YYYY-MM-DD") & "',"
SQL = SQL & "Nation='" & cobNation.Text & "',"
SQL = SQL & "Speciality='" & cobSpeciality.Text & "',"
SQL = SQL & "Phone='" & txtPhone.Text & "',"
SQL = SQL & "StatusID='" & txtStatusID.Text & "',"
SQL = SQL & "Email='" & txtEmail.Text & "',"
SQL = SQL & "HouseName='" & txtHouseName.Text & "',"
SQL = SQL & "HousePhone='" & txtHousePhone.Text & "',"
SQL = SQL & "HouseAddress='" & txtHouseAddress.Text & "',"
SQL = SQL & "PostCode='" & txtPostCode.Text & "',"
SQL = SQL & "Memo='" & txtMemo.Text & "'"
adoCon.Execute (SQL)
ErrMsg:
If Err.Number <> 0 Then
MsgBox Err.Description, , "系统错误"
End If
End Sub
应该是这样的:update student set 字段=新值 where 主键字段=当前记录的主键字段的值
......SQL = SQL & "Memo='" & txtMemo.Text & "'"
SQL = SQL & " where ......" '这里是你需要增加的语句。
adoCon.Execute (SQL)......