北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |
比如我想将ss表中字段【姓名】中包含“王”字的姓名更新为ll,使用下面的代码:
Private Sub Command5_Click()
Dim strsql As String
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
strsql = "select * from ss where 姓名 like '*王*'"
Debug.Print strsql
Set rst = getrstx(strsql)
DoCmd.SetWarnings False
rst.MoveFirst
Do Until rst.EOF
rst!姓名 = "ll"
rst.Update
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
MsgBox "OK"
End Sub
在运行时总是出错,错误提示的意思是说得到的记录集是空的,不知道什么情况?难道ADO方式返回记录集,对应的SQL语句不能使用模糊查询?
其中用于返回记录集的 getrstx函数代码如下:
Public Function getrstx(ByVal strsql As String) As ADODB.Recordset
On Error GoTo err_getrs
Dim rs As ADODB.Recordset
Dim cnn As New ADODB.Connection
Set rs = New ADODB.Recordset
Set cnn = CurrentProject.Connection
rs.Open strsql, cnn, adOpenKeyset, adLockOptimistic
Set getrstx = rs
exit_err_getrs:
Set rs = Nothing
Set cnn = Nothing
Exit Function
err_getrs:
MsgBox (Err.Description)
Resume exit_err_getrs
End Function