主题:往数据库内添加数据时报错。
下面是我对数据库内添加数据的按钮的源码,我的便携式的想法是:在往数据库里添加内容时,先在“username”列中检测是否有相同数据,如有,做出提示,如没有,添加数据后报成功提示。
现在的情况是:在检测相同数据的那一段源码报错(这一段源码是后加的,去掉这一段,则添加用户正常,只不过在用户名这一栏中会有相同的用户名。)
Private Sub Command1_Click()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\Data.mdb;Mode=ReadWrite|Share Deny None;Persist Security Info=False;Jet OLEDB:Database Password=123"
[color=008080] Dim Rs As New ADODB.Recordset
sql = "select * from admin where username='" & Text2.Text & "' "
[color=FF0000] Rs.Open sql, conn, adOpenStatic, adLockOptimistic ’报错行[/color]
If Not Rs.EOF Then
MsgBox "存在这个用户"
Else [/color]
cn.Execute "INSERT INTO admin([Username],[Division],[Password]) VALUES ('" & Text2.Text & "','" & Text1.Text & "','123456')"
MsgBox "添加用户成功!", , "提示"
cn.Close
Set cn = Nothing
Adodc1.Refresh
DataGrid1.Refresh
Text1.Text = ""
Text2.Text = ""
[color=008080]End If[/color]
End Sub
现在的情况是:在检测相同数据的那一段源码报错(这一段源码是后加的,去掉这一段,则添加用户正常,只不过在用户名这一栏中会有相同的用户名。)
Private Sub Command1_Click()
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
cn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\Data.mdb;Mode=ReadWrite|Share Deny None;Persist Security Info=False;Jet OLEDB:Database Password=123"
[color=008080] Dim Rs As New ADODB.Recordset
sql = "select * from admin where username='" & Text2.Text & "' "
[color=FF0000] Rs.Open sql, conn, adOpenStatic, adLockOptimistic ’报错行[/color]
If Not Rs.EOF Then
MsgBox "存在这个用户"
Else [/color]
cn.Execute "INSERT INTO admin([Username],[Division],[Password]) VALUES ('" & Text2.Text & "','" & Text1.Text & "','123456')"
MsgBox "添加用户成功!", , "提示"
cn.Close
Set cn = Nothing
Adodc1.Refresh
DataGrid1.Refresh
Text1.Text = ""
Text2.Text = ""
[color=008080]End If[/color]
End Sub