在写SQL语句时,需要对不同类型的数据分别加上#号,""号等来表示,用以下函数,就可以实现操作的简化.不管是什么类型,只需用这个Q函数转化一下,不需动手加格式化符号,就OK了.实在是方便.本人一直在用它,实在是方便.
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
Function Q(ByVal SqlVariable As Variant) As String'-----------------------------------------' Notes: Useful in creating properly formatted SQL statements' Usage: sql="select * from table where name= " & Q(vntName)' 这个版本格式化适用于Access的变量,若支持其它数据库或许需要对其进行修改'-----------------------------------------On Error GoTo ErrTrapQ = SqlVariable'format the stringSelect Case VarType(SqlVariable)Case vbNull, vbEmptyQ = "NULL"Case vbStringQ = "'" & Replace(SqlVariable, "'", "''") & "'"'date variableCase vbDate'format and enclose in pounds signs for AccessQ = "#" & Format$(SqlVariable, "general date") & "#"'otherwise treat as numericCase ElseOn Error Resume NextQ = CStr(SqlVariable)If Err.Number 0 Then Q = SqlVariableEnd SelectExit FunctionErrTrap:On Error GoTo 0End Function(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)