• <strong id="yd969"><track id="yd969"></track></strong>

    <li id="yd969"></li>
  • <rp id="yd969"><object id="yd969"></object></rp>
  • office交流網--QQ交流群號

    Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

    Word交流群:218156588             PPT交流群:324131555

    Access 函數簡化串接sql字符串,減少符號導致的書寫錯誤

    2021-05-16 08:00:00
    fans.net
    原創
    14417

    在編寫vba代碼時,經常會因為連接字符串的符號太多,沒有組成一對導致錯誤。

    這里版主粉絲寫了一個函數方便串接SQL字符串,可以讓串接變得更直觀,特別是有單引號的情況下,類似C#中的String.Format方法


    函數用途:可以方便串接SQL字符串
    
        Public Function ArrayFormat(expression As String, ParamArray formatException()) As String
            Dim strFind As String, strReplace As String, strTemp As String
            Dim i As Integer
            strTemp = expression
            For i = 0 To UBound(formatException)
                strFind = "{" & i & "}" : strReplace = formatException(i)
                strTemp = Replace(strTemp, strFind, strReplace)
            Next
            ArrayFormat = strTemp
        End Function
    
    
    Demo1:
    
    
     Sub Demo1()
        Dim str As String
        str = "他們分別來自:{0}、{1}、{2}、{3}、{4}、{5}"
        Debug.Print ArrayFormat(str, "北京", "上海", "廣州", "山東", "福建", "海南")
     End Sub
    
    
    輸出:他們分別來自:北京、上海、廣州、山東、福建、海南
    
    
    Demo2:
    
     Sub Demo2()
        Dim str As String
        str = "他們分別來自:{0}、{1}、{2}、{3}、{4}、{5}"
        Debug.Print ArrayFormat(str, "北京", "上海", "廣州")
     End Sub
    
    輸出:他們分別來自:北京、上海、{2}、{3}、{4}、{5}。
    
    


    如下面的示例,其中占位符:{}  ,里面的數字為index


      分享