看到别人写过这样语句,用来更改数据库中的多条记录,我试了怎么不行呢?(主要星号之间的)
我的想法是用Command2_Click()来找到需要的记录,用Command3_Click()来更
改一条或全部记录
*************************
cn.Execute "update 表1 set a = '" & Trim(Text9.Text) & "', _
" & "b1_2='"& trim(b) &"'where id=189"
光标换行时说:
缺少:语句结束
另外 amp;是做什么用的?
****************************
全部代码如下:Option Explicit
Dim cn As New ADODB.Connection
Dim cm As New ADODB.Command
Dim rst As New ADODB.Recordset
Dim sql As String
Dim sql2 As String
Dim sql3 As String
Dim sql4 As StringPrivate Sub Form_Load()
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents\db1.mdb"
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents\db1.mdb"
rst.Open "select * from [表1]", cn, adOpenDynamic, adLockOptimistic
End SubPrivate Sub Command2_Click()
Set cm.ActiveConnection = cn
sql3 = "select * from 表1 where id=189"
cm.CommandText = sql3
Set rst = cm.Execute
Text9.Text = rst.Fields("a")
Text10.Text = rst.Fields("b1_2")
Text11.Text = rst.Fields("c")
Text12.Text = rst.Fields("d")
rst.Close
Set rst = Nothing
End SubPrivate Sub Command3_Click()
Dim a As Single
Dim b As String
Dim c As Date
Dim d As String
a = Text9.Text
b = Text10.Text
c = Text11.Text
d = Text12.Textcn.Execute "update 表1 set a = '" & Trim(Text9.Text) & "'," & "b1_2='"& trim(b) &"'where id=189" End Sub
我的想法是用Command2_Click()来找到需要的记录,用Command3_Click()来更
改一条或全部记录
*************************
cn.Execute "update 表1 set a = '" & Trim(Text9.Text) & "', _
" & "b1_2='"& trim(b) &"'where id=189"
光标换行时说:
缺少:语句结束
另外 amp;是做什么用的?
****************************
全部代码如下:Option Explicit
Dim cn As New ADODB.Connection
Dim cm As New ADODB.Command
Dim rst As New ADODB.Recordset
Dim sql As String
Dim sql2 As String
Dim sql3 As String
Dim sql4 As StringPrivate Sub Form_Load()
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents\db1.mdb"
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\My Documents\db1.mdb"
rst.Open "select * from [表1]", cn, adOpenDynamic, adLockOptimistic
End SubPrivate Sub Command2_Click()
Set cm.ActiveConnection = cn
sql3 = "select * from 表1 where id=189"
cm.CommandText = sql3
Set rst = cm.Execute
Text9.Text = rst.Fields("a")
Text10.Text = rst.Fields("b1_2")
Text11.Text = rst.Fields("c")
Text12.Text = rst.Fields("d")
rst.Close
Set rst = Nothing
End SubPrivate Sub Command3_Click()
Dim a As Single
Dim b As String
Dim c As Date
Dim d As String
a = Text9.Text
b = Text10.Text
c = Text11.Text
d = Text12.Textcn.Execute "update 表1 set a = '" & Trim(Text9.Text) & "'," & "b1_2='"& trim(b) &"'where id=189" End Sub
cn.Execute "update 表1 set a = '" & Trim(Text9.Text) & "', _
" & "b1_2='"& trim(b) &"'where id=189"
光标换行时出现警告:
编译错误
缺少:语句结束
另外 amp;是做什么用的?
希望大家能帮助我
"b1_2='" trim(b)" 'where id=189"
试试这样吧
但不知道哪儿错了.才来请教大家的,
先谢谢
daisy8675(莫依)
qezhu521(来,受尽苦难的人,来我的怀抱)
sql="update 表1 set a="+"'"+trim(text9.text)+"'", _
"b1_2="+"'"+trim(b)+"'"+"where id=189"
cn.execute(sql)
一个字符一个字符的去按你需要的组合,方法笨了点!
cn.Execute "update 表1 set a = '" +Trim(Text9.Text)+"'and " + "b1_2='" +trim(b)+" 'where id=189"
你再试试这样
qezhu521(来,受尽苦难的人,来我的怀抱) ,您说的不能编译,任然报错:
编译错误
缺少:语句结束
to: njhxc(上官锅盔)
按照您的写法没报错,也能运行,但是结果是将所有的记录都修改了,where条件
好像没起作用,而且只能修改字段a的,字段b1_2没改过来,不知道为什么?
我原来的只修改一个字段的语句可以按照where 的条件来修改,语句如下
cn.Execute "update 表1 set a = '" & Trim(Text9.Text) & "'where id=189" 这是为什么呢?你们碰到过这样的情况吗?
cn.Execute "update 表1 set a = '" & Trim(Text9.Text) & "'where id='" & 189 & "'"
cn.Execute "update 表1 set a = '" & Trim(Text9.Text) & "', _
" & "b1_2='"& trim(b) &"'where id=189"
出错的原因:
1.“&”后没有空格
2.&是网页的一个转义号,也就是"&"。你的程序应该是网下拷的吧
注意:
1.vb中的最好不要在有空格的地方分行显示。按你的所写改为:cn.Exexute "update 表1 set a='" & Trim(Text9.Text) & "'," & "b1_2='" & Trim(b) & "' where id=189"
也是正确的
给点分吧????????!!!!!!!!!!!!!!