程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Access數據庫 >> Access數據庫入門 >> 用函數簡化你地字符串連接語句

用函數簡化你地字符串連接語句

編輯:Access數據庫入門

'2004/07/27 朱彥志(goodidea) [email protected]

'你是否非常討厭 用 & 一大串的連接變量?
'你是否不得不經常使用""與"""" ?
'就好像這樣:
'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 & ";"
'
'用它,gCombinationString,這個十分簡單的函數,讓冗長的代碼更簡潔,增強語句可讀性
'以下是它的使用典型形式:
'strFilter = gCombinationString( "$1 and [$2] like ""$3""",strFilter,strFldname,strValue)
'strCnn= gCombinationString("Provider=sqloledb;Data Source=$1;Initial Catalog=$2;User Id=$3;Password=$4;",_
' strServerName ,strDbname ,strUid, strPwd)
'參數個數不限,參數類型不限
' strSql = gCombinationString("insert into tblpubStyle (Class,Prop,Value,Type,[Set],memo,flag) " & _
' " values ('$1','$2','$3','$4',$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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved