<form id="hbx9t"></form>

<noframes id="hbx9t">

    <em id="hbx9t"><span id="hbx9t"></span></em>

        <noframes id="hbx9t"><address id="hbx9t"><th id="hbx9t"><progress id="hbx9t"></progress></th></address>
        office交流網--QQ交流群號

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

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

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

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

        在編寫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


          分享