|
5#
楼主 |
发表于 2007-9-8 10:39:15
|
只看该作者
Option Compare Database
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 ErrTrap
Q = SqlVariable
'FORMat the string
Select Case varType(SqlVariable)
Case vbNull, vbEmpty
Q = "NULL"
Case vbString
Q = "'" & Replace(SqlVariable, "'", "''") & "'"
'date variable
Case vbDate
'FORMat and enclose in pounds signs for Access
Q = "#" & Format$(SqlVariable, "general date") & "#"
'otherwise treat as numeric
Case Else
On Error Resume Next
Q = CStr(SqlVariable)
If err.Number <> 0 Then Q = SqlVariable
End Select
Exit Function
ErrTrap:
On Error GoTo 0
End Function |
|