strsql="select [name] from Student where [name]='" & trim(text1.text ) & "' " rs.open strsql,cn,3,3 if rs.eof and rs.bof then rs.addnew rs.fields("name").value=trim(text1.text) rs.update end if
这好像是ASP形式的.在VB中可以这样写吗?
来晚了,就是ljc_zy(彷徨)的方法。不过 strsql="select [name] from Student where [name]='" & trim(text1.text ) & "' " 这句还是改为: strsql="select * from Student where [name]='" & trim(text1.text ) & "' " 比较好。 因为添加记录时不会只添加一个姓名字段,应该还有其它的字段要添加吧。
可以是可以!不过添加一些msgbox来提示会更好些吧!这样不会出现误操作了!
对,希望用msgbox来提示?strsql="select * from Student where [name]='" & trim(text1.text ) & "' " rs.open strsql,cn,3,3 if rs.eof and rs.bof then rs.addnew rs.fields("name").value=trim(text1.text) rs.update end if 在上面的代源中,能不能提示如何与库建立连接?
Dim Conn As New ADODB.Connection Dim Rs As New ADODB.Recordset Dim strConn As StringstrConn = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" + 你的数据库Conn.Open strConnRs.Open ="select * from Student where [name]='" & trim(text1.text ) & "'", Conn, adOpenDynamic
Rs.Open ="select * from Student where [name]='" & trim(text1.text ) & "'", Conn, adOpenDynamic 这句怎么有错误呢?
Dim sq As String sq = "select * from dbo_hyxx where dbo_id=" & "'" & Text27.text & "'" Me.Data6.Refresh If Data6.Recordset.NoMatch Then Data6.Recordset.AddNew Data6.Recordset("dbo_id") = Text27.text Data6.Recordset.Update Data6.Recordset.MoveLast Else MsgBox "已经存在", vbExclamation, "提示" End If这段代源不知道为什么执行的结果总是"已经存在",请高手指点.
我晕,怎么可能解决不了???????我就不信,一个这么简单的问题,至于解决不了吗?你字符串连接也没有问题~SQL语句应该也不会有问题啊~ Dim sq As String dim rs as adodb.recordset dim conn as adodb.connection dim conn_TWO as adodb.connection sq = "select ID from dbo_hyxx where dbo_id=" & "'" & Text27.text & "'" Set rs = New ADODB.Recordset Set conn = New ADODB.Connection conn.Open ConnectString rs.Open sq, conn, adOpenStatic if rs.recordcount <1 then set conn_TWO =new adodb.connection conn_TWO.open connectstring conn_TWO.execute "insert into 表(字段1,字段2,ID),values('值1','值2','" & text27 & "')" conn.close else msgbox "已经存在" rs.close conn.close end if rs.close conn.close 这样在不可以,我就把电脑吃了~气死我了~
谢谢,我的数据库是access,是通过DATA控件来连接库的. 请问这样如何实现?
Data1.DatabaseName = 你的数据库名(包含路径) Data1.RecordSource = "select * from 表名 where 条件" '就用前面别人给你的那个 Data1.RefreshIf Data1.Recordset.RecordCount = 0 Then Data1.Recordset.AddNew ... ... Data1.Recordset.Update Else MsgBox "记录已经存在!", vbCritical End If
Public Function ExecuteSQL(sql As String) As ADODB.Recordset Dim cn As ADODB.Connection Dim rst As ADODB.Recordset Dim msg As String On Error GoTo executesql_error Set cn = New ADODB.Connection cn.Open "Driver={SQL Server};Server=你的服务器名;Uid=sa;Pwd=;database=你的数据库" Set rst = New ADODB.Recordset rst.Open Trim$(sql), cn, adOpenKeyset, adLockOptimistic Set ExecuteSQL = rst executesql_exit: Set rst = Nothing Set cn = Nothing Exit Function executesql_error: msg = "错误原因:" & Err.Description Resume executesql_exit End FunctionPrivate Sub cmd_true_Click() If Trim(txt_name.Text) = "" Then MsgBox "请输入用户名~!~", vbOKOnly + vbExclamation, "注意" txt_name.SetFocus Exit Sub Else If Len(Trim(txt_name.Text)) > 10 Then MsgBox "对不起,密码有效位仅为10位,请想好在输入,谢谢合作~!~", vbOKOnly + vbExclamation, "注意" txt_name.SetFocus txt_name.Text = "" Exit Sub End If sql = "select*from Cuser" Set rs = ExecuteSQL(sql) While rs.EOF = False If Trim(rs.Fields(0)) = Trim(txt_name.Text) Then MsgBox "此用户已存在,请重新命名~!~", vbOKOnly + vbExclamation, "注意" txt_name.SetFocus txt_name.Text = "" txt_pw.Text = "" txt_conpw.Text = "" Exit Sub Else rs.MoveNext End If Wend End If If Trim(txt_pw.Text) <> Trim(txt_conpw.Text) Then MsgBox "对不起,您两次输入的密码不一样,请重新输入~!~", vbOKOnly + vbExclamation, "注意" txt_pw.SetFocus txt_pw.Text = "" txt_conpw.Text = "" Else If Trim(txt_pw.Text) = "" Then MsgBox "对不起,密码不能为空~!~", vbOKOnly + vbExclamation, "注意" txt_pw.SetFocus Else If Len(txt_pw.Text) > 10 Then MsgBox "对不起,密码有效位仅为10位,请想好在输入,谢谢合作~!~", vbOKOnly + vbExclamation, "注意" txt_pw.SetFocus txt_pw.Text = "" txt_conpw.Text = "" Exit Sub Else rs.AddNew rs.Fields(0) = Trim(txt_name.Text) rs.Fields(1) = Trim(txt_pw.Text) If Check1.Value = 1 Then rs.Fields(2).Value = "1" Else rs.Fields(2).Value = "0" End If rs.Update txt_name.Text = "" txt_pw.Text = "" txt_conpw.Text = "" rs.Close txt_name.SetFocus frm_umanagement.Adodc1.Refresh Set frm_umanagement.DataList1.DataSource = frm_umanagement.Adodc1 frm_umanagement.DataList1.DataField = "id" Set frm_umanagement.DataList1.RowSource = frm_umanagement.Adodc1 frm_umanagement.DataList1.ListField = "id" frm_umanagement.DataList1.Refresh 'frm_umanagement.cmd_delete.Enabled = False End If End If End If Check1.Value = 0 End Sub这是一个已经调试成功的代码 ,给你做参考吧
strsql="select [name] from Student where [name]='" & trim(text1.text ) & "' "
rs.open strsql,cn,3,3
if rs.eof and rs.bof then
rs.addnew
rs.fields("name").value=trim(text1.text)
rs.update
end if
strsql="select [name] from Student where [name]='" & trim(text1.text ) & "' " 这句还是改为:
strsql="select * from Student where [name]='" & trim(text1.text ) & "' "
比较好。
因为添加记录时不会只添加一个姓名字段,应该还有其它的字段要添加吧。
rs.open strsql,cn,3,3
if rs.eof and rs.bof then
rs.addnew
rs.fields("name").value=trim(text1.text)
rs.update
end if
在上面的代源中,能不能提示如何与库建立连接?
Dim Rs As New ADODB.Recordset
Dim strConn As StringstrConn = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" + 你的数据库Conn.Open strConnRs.Open ="select * from Student where [name]='" & trim(text1.text ) & "'", Conn, adOpenDynamic
这句怎么有错误呢?
sq = "select * from dbo_hyxx where dbo_id=" & "'" & Text27.text & "'"
Me.Data6.Refresh
If Data6.Recordset.NoMatch Then
Data6.Recordset.AddNew
Data6.Recordset("dbo_id") = Text27.text
Data6.Recordset.Update
Data6.Recordset.MoveLast
Else
MsgBox "已经存在", vbExclamation, "提示"
End If这段代源不知道为什么执行的结果总是"已经存在",请高手指点.
Dim sq As String
dim rs as adodb.recordset
dim conn as adodb.connection
dim conn_TWO as adodb.connection
sq = "select ID from dbo_hyxx where dbo_id=" & "'" & Text27.text & "'"
Set rs = New ADODB.Recordset
Set conn = New ADODB.Connection
conn.Open ConnectString
rs.Open sq, conn, adOpenStatic
if rs.recordcount <1 then
set conn_TWO =new adodb.connection
conn_TWO.open connectstring
conn_TWO.execute "insert into 表(字段1,字段2,ID),values('值1','值2','" & text27 & "')"
conn.close
else
msgbox "已经存在"
rs.close
conn.close
end if
rs.close
conn.close 这样在不可以,我就把电脑吃了~气死我了~
请问这样如何实现?
Data1.RecordSource = "select * from 表名 where 条件" '就用前面别人给你的那个
Data1.RefreshIf Data1.Recordset.RecordCount = 0 Then
Data1.Recordset.AddNew
...
...
Data1.Recordset.Update
Else
MsgBox "记录已经存在!", vbCritical
End If
Dim cn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim msg As String
On Error GoTo executesql_error
Set cn = New ADODB.Connection
cn.Open "Driver={SQL Server};Server=你的服务器名;Uid=sa;Pwd=;database=你的数据库"
Set rst = New ADODB.Recordset
rst.Open Trim$(sql), cn, adOpenKeyset, adLockOptimistic
Set ExecuteSQL = rst
executesql_exit:
Set rst = Nothing
Set cn = Nothing
Exit Function
executesql_error:
msg = "错误原因:" & Err.Description
Resume executesql_exit
End FunctionPrivate Sub cmd_true_Click()
If Trim(txt_name.Text) = "" Then
MsgBox "请输入用户名~!~", vbOKOnly + vbExclamation, "注意"
txt_name.SetFocus
Exit Sub
Else
If Len(Trim(txt_name.Text)) > 10 Then
MsgBox "对不起,密码有效位仅为10位,请想好在输入,谢谢合作~!~", vbOKOnly + vbExclamation, "注意"
txt_name.SetFocus
txt_name.Text = ""
Exit Sub
End If
sql = "select*from Cuser"
Set rs = ExecuteSQL(sql)
While rs.EOF = False
If Trim(rs.Fields(0)) = Trim(txt_name.Text) Then
MsgBox "此用户已存在,请重新命名~!~", vbOKOnly + vbExclamation, "注意"
txt_name.SetFocus
txt_name.Text = ""
txt_pw.Text = ""
txt_conpw.Text = ""
Exit Sub
Else
rs.MoveNext
End If
Wend
End If
If Trim(txt_pw.Text) <> Trim(txt_conpw.Text) Then
MsgBox "对不起,您两次输入的密码不一样,请重新输入~!~", vbOKOnly + vbExclamation, "注意"
txt_pw.SetFocus
txt_pw.Text = ""
txt_conpw.Text = ""
Else
If Trim(txt_pw.Text) = "" Then
MsgBox "对不起,密码不能为空~!~", vbOKOnly + vbExclamation, "注意"
txt_pw.SetFocus
Else
If Len(txt_pw.Text) > 10 Then
MsgBox "对不起,密码有效位仅为10位,请想好在输入,谢谢合作~!~", vbOKOnly + vbExclamation, "注意"
txt_pw.SetFocus
txt_pw.Text = ""
txt_conpw.Text = ""
Exit Sub
Else
rs.AddNew
rs.Fields(0) = Trim(txt_name.Text)
rs.Fields(1) = Trim(txt_pw.Text)
If Check1.Value = 1 Then
rs.Fields(2).Value = "1"
Else
rs.Fields(2).Value = "0"
End If
rs.Update
txt_name.Text = ""
txt_pw.Text = ""
txt_conpw.Text = ""
rs.Close
txt_name.SetFocus
frm_umanagement.Adodc1.Refresh
Set frm_umanagement.DataList1.DataSource = frm_umanagement.Adodc1
frm_umanagement.DataList1.DataField = "id"
Set frm_umanagement.DataList1.RowSource = frm_umanagement.Adodc1
frm_umanagement.DataList1.ListField = "id"
frm_umanagement.DataList1.Refresh
'frm_umanagement.cmd_delete.Enabled = False
End If
End If
End If
Check1.Value = 0
End Sub这是一个已经调试成功的代码 ,给你做参考吧