---------SQL生成----------
任務是當用戶以逗號來分隔查詢的條件,的多條件查詢,使用IN(x,y),但是當在數據庫(ACCESS)對應的字段是字符型是就會出現錯誤,那是當然的!如果是IN('x','y')就沒有問題!
下面的代碼就是對字符串的操作,返回 'x','y'。
'-----接受要處理的字符串------- Function Tool_disTest(ByVal TextBefore As String) Dim TextAfter() As String Dim intNum, intTextLen, k, i As Integer intNum = 1 intTextLen = Len(TextBefore) '--------計算有多少的查詢條件-------- For i = 1 To intTextLen If Mid(TextBefore, i, 1) = "," Then intNum = intNum + 1 End If Next '----------------------------------- ReDim TextAfter(intNum) k = 1 '--------把每個條件放到數組裡--------- For i = 1 To intTextLen If Mid(TextBefore, i, 1) = "," Then k += 1 Else TextAfter(k) = TextAfter(k) & Mid(TextBefore, i, 1) End If Next '---------------------- Dim strTemp As String '--------為每一個條件加上''---------- For i = 1 To intNum strTemp = TextAfter(i) TextAfter(i) = "'" & strTemp & "'" Next '---------------------- strTemp = "" '------添加分隔條件用的","------ For i = 1 To intNum strTemp = strTemp & TextAfter(i) & "," Next '------------------ '------去除最後一個,------ strTemp = Mid(strTemp, 1, Len(strTemp) - 1) '------------------- Return strTemp End Function 最後在SQLServer中又試了試,發現SQLServer查詢的時候用IN(x,y)來查詢字符型是OK的,(SQLServer就是比ACCESS強點,呵呵) ---------end-------