一个添加数据的窗体,5个textbox(学号,姓名,VC成绩,VB成绩,SQL成绩),2个commandbutton(确定和取消)。如何实现当我输入完信息,点确定的时候,系统先去相应的表中查找,如果输入的学号已经存在,就报错,添加失败;如果不存在就将记录写入数据库。
现在的情况是,我定义了一个插入数据的函数,点确定后执行INSERT语句,也设置了报错,可每次测试输入重复数据时,都提示添加数据成功,但数据库中的记录没有变化,如果输入一个不重复的数据就可以添加进去。我郁闷很久了!以下是我写的查询函数的代码,请大侠指点,看我写得对不对
Public Function InsertSQL(ByVal InsertString As String) As Boolean
Call StudDataRcs.Open(InsertString, StudData, adOpenDynamic, adLockOptimistic, -1)
StudDataRcs.MoveFirst
Do While (Not StudDataRcs.EOF)
If StudDataRcs.Fields("studcode").Value = adddata.txtcode.Text Then
MsgBox "学号重复,请确认后再输!", vbCritical + vbOKOnly, "错误"
InsertSQL = False
Exit Function
End If
StudDataRcs.MoveNext
Loop
InsertSQL = True
End Function
9 个解决方案
#1
这是添加记录窗体的“确定”代码
Private Sub ok_Click()
Dim InsertString As String
On Error Resume Next
InsertString = "INSERT INTO stud_info(studcode,studname,studvc,studvb,studsql) VALUES ('" & txtcode.Text & "','" & txtname.Text & "','" & txtvc.Text & "','" & txtvb.Text & "','" & txtsql.Text & "')"
If InsertSQL(InsertString) = True Then
StudDataRcs.Update
If MsgBox("数据添加成功!" & vbCrLf & "是否继续添加?(Y/N)", vbInformation + vbYesNo, "提示") = vbYes Then
With adddata
.txtcode.Text = ""
.txtcode.SetFocus
.txtname.Text = ""
.txtvc.Text = ""
.txtvb.Text = ""
.txtsql.Text = ""
End With
Else
Unload Me
End If
End If
End Sub
Private Sub ok_Click()
Dim InsertString As String
On Error Resume Next
InsertString = "INSERT INTO stud_info(studcode,studname,studvc,studvb,studsql) VALUES ('" & txtcode.Text & "','" & txtname.Text & "','" & txtvc.Text & "','" & txtvb.Text & "','" & txtsql.Text & "')"
If InsertSQL(InsertString) = True Then
StudDataRcs.Update
If MsgBox("数据添加成功!" & vbCrLf & "是否继续添加?(Y/N)", vbInformation + vbYesNo, "提示") = vbYes Then
With adddata
.txtcode.Text = ""
.txtcode.SetFocus
.txtname.Text = ""
.txtvc.Text = ""
.txtvb.Text = ""
.txtsql.Text = ""
End With
Else
Unload Me
End If
End If
End Sub
#2
studdatarcs是什么东西
#3
是一个new ADODB.RecordSet
#4
那干吗要用call,后面的-1又是干什么用的。
#5
Public Function InsertSQL(ByVal InsertString As String) As Boolean
Call StudDataRcs.Open(InsertString, StudData, adOpenDynamic, adLockOptimistic, -1)
StudDataRcs.MoveFirst
Do While (Not StudDataRcs.EOF)
If trim(StudDataRcs.Fields("studcode").Value) = trim(adddata.txtcode.Text) Then
MsgBox "学号重复,请确认后再输!", vbCritical + vbOKOnly, "错误"
InsertSQL = False
Exit Function
End If
StudDataRcs.MoveNext
Loop
InsertSQL = True
End Function
Call StudDataRcs.Open(InsertString, StudData, adOpenDynamic, adLockOptimistic, -1)
StudDataRcs.MoveFirst
Do While (Not StudDataRcs.EOF)
If trim(StudDataRcs.Fields("studcode").Value) = trim(adddata.txtcode.Text) Then
MsgBox "学号重复,请确认后再输!", vbCritical + vbOKOnly, "错误"
InsertSQL = False
Exit Function
End If
StudDataRcs.MoveNext
Loop
InsertSQL = True
End Function
#6
如果不用call,系统会提示找不到 = 号,后边的-1是书上写的,我还没弄明白是干什么的,呵呵
#7
to: tztz520(午夜逛街)
还是不行啊,我输入了重复数据,还是提示添加成功
还是不行啊,我输入了重复数据,还是提示添加成功
#8
你可以把If StudDataRcs.Fields("studcode").Value = adddata.txtcode.Text Then改成
If Trim(StudDataRcs.Fields("studcode").Value) = Trim(adddata.txtcode.Text) Then试试
如是我做的话,我是这样做:
public Function check() as boolean
dim rs as new recordset
check=true
rs.open "select studecode from stud_info where studecode='" & adddata.txtcode.text & "'",conn,adOpenDynamic, adLockOptimistic
if not rs.eof then
check=false
msgbox "重复数据"
end if
rs.close
set rs = nothing
end function
Private Sub ok_Click()
Dim InsertString As String
yn=check()
if not yn then exit sub
On Error Resume Next
InsertString = "INSERT INTO stud_info(studcode,studname,studvc,studvb,studsql) VALUES ('" & txtcode.Text & "','" & txtname.Text & "','" & txtvc.Text & "','" & txtvb.Text & "','" & txtsql.Text & "')"
conn.execute(insertstring)
If MsgBox("数据添加成功!" & vbCrLf & "是否继续添加?(Y/N)", vbInformation + vbYesNo, "提示") = vbYes Then
With adddata
.txtcode.Text = ""
.txtcode.SetFocus
.txtname.Text = ""
.txtvc.Text = ""
.txtvb.Text = ""
.txtsql.Text = ""
End With
Else
Unload Me
End If
End If
End Sub
conn是new adodb.connection,rs是new adodb.recordset
If Trim(StudDataRcs.Fields("studcode").Value) = Trim(adddata.txtcode.Text) Then试试
如是我做的话,我是这样做:
public Function check() as boolean
dim rs as new recordset
check=true
rs.open "select studecode from stud_info where studecode='" & adddata.txtcode.text & "'",conn,adOpenDynamic, adLockOptimistic
if not rs.eof then
check=false
msgbox "重复数据"
end if
rs.close
set rs = nothing
end function
Private Sub ok_Click()
Dim InsertString As String
yn=check()
if not yn then exit sub
On Error Resume Next
InsertString = "INSERT INTO stud_info(studcode,studname,studvc,studvb,studsql) VALUES ('" & txtcode.Text & "','" & txtname.Text & "','" & txtvc.Text & "','" & txtvb.Text & "','" & txtsql.Text & "')"
conn.execute(insertstring)
If MsgBox("数据添加成功!" & vbCrLf & "是否继续添加?(Y/N)", vbInformation + vbYesNo, "提示") = vbYes Then
With adddata
.txtcode.Text = ""
.txtcode.SetFocus
.txtname.Text = ""
.txtvc.Text = ""
.txtvb.Text = ""
.txtsql.Text = ""
End With
Else
Unload Me
End If
End If
End Sub
conn是new adodb.connection,rs是new adodb.recordset
#9
这一句
StudDataRcs.Open "select studcode from stud_info where studcode='" & adddata.txtcode.Text & "'", StudData, adOpenDynamic, adLockOptimistic
提示:
实时错误'-2147217913 (80040e07)':
标准表达式中数据类型不匹配.
这是什么意思啊?
StudDataRcs.Open "select studcode from stud_info where studcode='" & adddata.txtcode.Text & "'", StudData, adOpenDynamic, adLockOptimistic
提示:
实时错误'-2147217913 (80040e07)':
标准表达式中数据类型不匹配.
这是什么意思啊?
#1
这是添加记录窗体的“确定”代码
Private Sub ok_Click()
Dim InsertString As String
On Error Resume Next
InsertString = "INSERT INTO stud_info(studcode,studname,studvc,studvb,studsql) VALUES ('" & txtcode.Text & "','" & txtname.Text & "','" & txtvc.Text & "','" & txtvb.Text & "','" & txtsql.Text & "')"
If InsertSQL(InsertString) = True Then
StudDataRcs.Update
If MsgBox("数据添加成功!" & vbCrLf & "是否继续添加?(Y/N)", vbInformation + vbYesNo, "提示") = vbYes Then
With adddata
.txtcode.Text = ""
.txtcode.SetFocus
.txtname.Text = ""
.txtvc.Text = ""
.txtvb.Text = ""
.txtsql.Text = ""
End With
Else
Unload Me
End If
End If
End Sub
Private Sub ok_Click()
Dim InsertString As String
On Error Resume Next
InsertString = "INSERT INTO stud_info(studcode,studname,studvc,studvb,studsql) VALUES ('" & txtcode.Text & "','" & txtname.Text & "','" & txtvc.Text & "','" & txtvb.Text & "','" & txtsql.Text & "')"
If InsertSQL(InsertString) = True Then
StudDataRcs.Update
If MsgBox("数据添加成功!" & vbCrLf & "是否继续添加?(Y/N)", vbInformation + vbYesNo, "提示") = vbYes Then
With adddata
.txtcode.Text = ""
.txtcode.SetFocus
.txtname.Text = ""
.txtvc.Text = ""
.txtvb.Text = ""
.txtsql.Text = ""
End With
Else
Unload Me
End If
End If
End Sub
#2
studdatarcs是什么东西
#3
是一个new ADODB.RecordSet
#4
那干吗要用call,后面的-1又是干什么用的。
#5
Public Function InsertSQL(ByVal InsertString As String) As Boolean
Call StudDataRcs.Open(InsertString, StudData, adOpenDynamic, adLockOptimistic, -1)
StudDataRcs.MoveFirst
Do While (Not StudDataRcs.EOF)
If trim(StudDataRcs.Fields("studcode").Value) = trim(adddata.txtcode.Text) Then
MsgBox "学号重复,请确认后再输!", vbCritical + vbOKOnly, "错误"
InsertSQL = False
Exit Function
End If
StudDataRcs.MoveNext
Loop
InsertSQL = True
End Function
Call StudDataRcs.Open(InsertString, StudData, adOpenDynamic, adLockOptimistic, -1)
StudDataRcs.MoveFirst
Do While (Not StudDataRcs.EOF)
If trim(StudDataRcs.Fields("studcode").Value) = trim(adddata.txtcode.Text) Then
MsgBox "学号重复,请确认后再输!", vbCritical + vbOKOnly, "错误"
InsertSQL = False
Exit Function
End If
StudDataRcs.MoveNext
Loop
InsertSQL = True
End Function
#6
如果不用call,系统会提示找不到 = 号,后边的-1是书上写的,我还没弄明白是干什么的,呵呵
#7
to: tztz520(午夜逛街)
还是不行啊,我输入了重复数据,还是提示添加成功
还是不行啊,我输入了重复数据,还是提示添加成功
#8
你可以把If StudDataRcs.Fields("studcode").Value = adddata.txtcode.Text Then改成
If Trim(StudDataRcs.Fields("studcode").Value) = Trim(adddata.txtcode.Text) Then试试
如是我做的话,我是这样做:
public Function check() as boolean
dim rs as new recordset
check=true
rs.open "select studecode from stud_info where studecode='" & adddata.txtcode.text & "'",conn,adOpenDynamic, adLockOptimistic
if not rs.eof then
check=false
msgbox "重复数据"
end if
rs.close
set rs = nothing
end function
Private Sub ok_Click()
Dim InsertString As String
yn=check()
if not yn then exit sub
On Error Resume Next
InsertString = "INSERT INTO stud_info(studcode,studname,studvc,studvb,studsql) VALUES ('" & txtcode.Text & "','" & txtname.Text & "','" & txtvc.Text & "','" & txtvb.Text & "','" & txtsql.Text & "')"
conn.execute(insertstring)
If MsgBox("数据添加成功!" & vbCrLf & "是否继续添加?(Y/N)", vbInformation + vbYesNo, "提示") = vbYes Then
With adddata
.txtcode.Text = ""
.txtcode.SetFocus
.txtname.Text = ""
.txtvc.Text = ""
.txtvb.Text = ""
.txtsql.Text = ""
End With
Else
Unload Me
End If
End If
End Sub
conn是new adodb.connection,rs是new adodb.recordset
If Trim(StudDataRcs.Fields("studcode").Value) = Trim(adddata.txtcode.Text) Then试试
如是我做的话,我是这样做:
public Function check() as boolean
dim rs as new recordset
check=true
rs.open "select studecode from stud_info where studecode='" & adddata.txtcode.text & "'",conn,adOpenDynamic, adLockOptimistic
if not rs.eof then
check=false
msgbox "重复数据"
end if
rs.close
set rs = nothing
end function
Private Sub ok_Click()
Dim InsertString As String
yn=check()
if not yn then exit sub
On Error Resume Next
InsertString = "INSERT INTO stud_info(studcode,studname,studvc,studvb,studsql) VALUES ('" & txtcode.Text & "','" & txtname.Text & "','" & txtvc.Text & "','" & txtvb.Text & "','" & txtsql.Text & "')"
conn.execute(insertstring)
If MsgBox("数据添加成功!" & vbCrLf & "是否继续添加?(Y/N)", vbInformation + vbYesNo, "提示") = vbYes Then
With adddata
.txtcode.Text = ""
.txtcode.SetFocus
.txtname.Text = ""
.txtvc.Text = ""
.txtvb.Text = ""
.txtsql.Text = ""
End With
Else
Unload Me
End If
End If
End Sub
conn是new adodb.connection,rs是new adodb.recordset
#9
这一句
StudDataRcs.Open "select studcode from stud_info where studcode='" & adddata.txtcode.Text & "'", StudData, adOpenDynamic, adLockOptimistic
提示:
实时错误'-2147217913 (80040e07)':
标准表达式中数据类型不匹配.
这是什么意思啊?
StudDataRcs.Open "select studcode from stud_info where studcode='" & adddata.txtcode.Text & "'", StudData, adOpenDynamic, adLockOptimistic
提示:
实时错误'-2147217913 (80040e07)':
标准表达式中数据类型不匹配.
这是什么意思啊?