格式化字符函數

原始位置﹕http://dev.csdn.net/author/fanpingli/1742be883708457999b0ad569b82c878.html

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章