|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
"SELECT * FROM 凭证库 WHERE 日期 BETWEEN #?# AND #?# AND 凭证号=?"
是否见过这样的,在JS区看过SQLITE学到的
用正则轻松搞定
Sql = sqlRegStatement("SELECT * FROM 凭证库 WHERE 日期 BETWEEN #?# AND #?# AND 凭证号=?", Array(monthFirstDay(Sh.Cells(3, 4).Value), monthLastDay(Sh.Cells(3, 4).Value), Sh.Cells(3, 6).Value))
Public Function sqlRegStatement(Sql, arr)
Set reg = CreateObject("VBScript.RegExp")
reg.Pattern = "\?"
i = 0
Do While reg.test(Sql)
Sql = reg.Replace(Sql, arr(i))
i = i + 1
Loop
sqlRegStatement = Sql
End Function
如果变量有很多一样的,也可以用这种方式
Sql = sqlStatement("SELECT * FROM 凭证库 WHERE 日期 BETWEEN #?monthFirstDay# AND #?monthLastDay# AND 凭证号=?voucherNum", Array("monthFirstDay",monthFirstDay(Sh.Cells(3, 4).Value), "monthLastDay",monthLastDay(Sh.Cells(3, 4).Value),"voucherNum", Sh.Cells(3, 6).Value))
Function sqlStatement(Sql,arr)
For i = 0 To UBound(arr) Step 2
Sql = Replace(Sql, "?" & arr(i), arr(i + 1))
Next
sqlStatement = Sql
end Function |
|