在VB程序中格式化SQL字符串

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

VB程序中格式化SQL字符串 

在写SQL语句时,需要对不同类型的数据分别加上#号,""号等来表示,用以下函数,就可以实现操作的简化.不管是什么类型,只需用这个Q函数转化一下,不需动手加格式化符号,就OK了.实在是方便.本人一直在用它,实在是方便.

Function Q(ByVal SQLVariable As Variant) As String<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

'-----------------------------------------

'        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

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: vb
想对作者说点什么? 我来说一句

VB6.0代码格式化

2014年09月05日 79KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭