2017年5月17日 星期三

[VB6]Dictionary, 結合SQL語法 做新增,修改

Public Function SQL_String(ByRef  Dict1 As Dictionary, ByVal StrTable As String, ByVal IntType As Integer, Optional StrWhere As String) As String                                      
    Dim i          As Long
    Dim S1      As String
 
    Select Case IntType
    Case 1      '新增        
        S1 = "Insert Into" & StrTable & "("         '組SQL新增字串   
 
        '0 到 dict1 物件最大值   UBound: 傳回指定陣列某維最大可使用的陣列索引。
        For i = 0 To UBound(Dict1.Keys)            
                S1 = S1 & Dict1.Keys(i) & IIf(i <> UBound(Dict1.Keys), ",", "")   '最後一項不補逗點
        Next i  
     
        S1 = S1 & ") Values("                               '組SQL新增字串
     
        For i = 0 To UBound(Dict1.Keys)             '0 到 dict1 物件最大可使用陣列索引值
                '最後一項不補逗點
                 S1 = S1 & " ' " & Dict1(Dict1.Keys(i)) & " ' " & IIf(i <> UBound(Dict1.Keys), ",", "")
        Next i

        S1 = S1 & ")"
     
        SQL_String = S1
     
    Case 2  '修改
        S1 = "Update " & StrTable & " Set "
     
        For i = 0 To UBound(Dict1.Keys)
                S1 = S1 & Dict1.Keys(i) & "='" & Dict1(Dict1.Keys(i)) & " ' " & _
                      IIf(i <>UBound(dict1.Keys), ", ", "")
        Next i

        If StrWhere <> "" Then S1 = S1 & " Where " & StrWhere

        SQL_String = S1
    End Select
End Function

沒有留言:

張貼留言