Simplify your string connection statement with a function

Source: Internet
Author: User

' 2004/07/27 Zhu Yanzhi (Goodidea) myad@yeah.net

' Do you really hate using & a bunch of connection variables?
Do you have to use the "" and "" "frequently?
' It's like this:
' strfilter = strfilter & ' and [& Strfldname & '] like ' "& strvalue &" ""
'
' strSQL = ' insert INTO Tblpubstyle (Class,prop,value,type,[set],memo,flag) VALUES (' "& _
' CTL. ControlType & "', '" & _
' PRP. Name & "', '" & _
' PRP. Value & "', '" & _
' PRP. Type & "'," & _
' 2 & ', ' & _
' "," & _
' false ') "
'
' strcnn = Provider=sqloledb; ' & _
' "Data source=" & strServerName & ";" & _
' "Initial catalog=" & strDbName & ";" & _
"User id=" & Struid & ";" & _
' "Password=" & Strpwd & ";
'
' With it, gcombinationstring, this very simple function, makes verbose code more concise, enhances the readability of the statement
' The following is the typical form of its use:
' strfilter = gcombinationstring ("and [$] like" "$" ", Strfilter,strfldname,strvalue)
' Strcnn= gcombinationstring ("Provider=sqloledb;data source=$1;initial catalog=$2; User id=$3; password=$4; ", _
' strServerName, strDbName, Struid, strpwd
' The number of parameters is not limited, parameter type is not limited
' strSQL = gcombinationstring (insert into Tblpubstyle (Class,prop,value,type,[set],memo,flag) & _
' VALUES (' $ ', ' $ ', ' $ ', ' $ ', $5,$6) ', _
' CTL. ControlType, PRP. Name, PRP. Value, PRP. Type, 2, False)
'

Const Strcharpre = "$"
Dim Varitem as Variant
Dim I as Integer

i = 0
For each varitem in Para
i = i + 1
StrText = VBA. Replace (StrText, Strcharpre & I, Varitem)
StrText = VBA. Replace (StrText, Strcharpre & "\", Strcharpre)
Next
gcombinationstring = StrText
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.