|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 山中老人 于 2013-10-21 09:20 编辑
仿照系统函数:Dlookup,Dsum,Dcount,Dmin
写了一个函数:DUpdate
请高手指点,能否优化!
Private Const SQL分隔符 = ","
Public Function DUpdate(字段串 As String, 数值串 As String, 记录集 As String,Optional WHERE As String="") As String '返回SQL语句
DUpdate = ""
If 字段串 = "" Or 数值串 = "" Or 记录集 = "" Then Exit Function
On Error GoTo err
Dim 字段() As String, 数值() As String, STR As String
字段 = Split(字段串, SQL分隔符)
数值 = Split(数值串, SQL分隔符)
STR = ""
For i = LBound(字段, 1) To UBound(字段, 1)
If i > UBound(数值, 1) Then Exit For '字段串与数值串 个数不一致。
STR = "," & 字段(i) & "=" & 数值(i)
Next i
STR = "SET " & Mid(STR, 2) ' SET 子句
STR = "UPDATE " & 记录集 & " " & STR
If WHERE <> "" Then STR = STR & " WHERE " & WHERE '有WHERE语句!
Debug.Print STR 'SQL语句!
DoCmd.SetWarnings False '关闭警告提示
DoCmd.RunSQL STR
DoCmd.SetWarnings True '打开警告提示
DUpdate = STR
Exit Function
err:
MsgBox Error$
End Function
|
|