Format SQL strings in VB program

Source: Internet
Author: User
Tags goto

When writing SQL statements, you need to add a # number to different types of data. "Number, etc., with the following functions, you can achieve the simplification of the operation. No matter what type, just use this Q function to transform, do not need to add formatting symbols, OK. It's really convenient. I've been using it, it's really convenient .

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.