我想更新我的代码以包含单元格的内容,S3。现在,单元格的值为“Test,Testing,Tested”,查询的相关部分如下所示:
SELECT * FROM dbo.Total WHERE ID = " & TextBox1.Text & " And Source IN (What do I put in here?)
非常感谢您的帮助
答案 0 :(得分:1)
这样的事情怎么样?
Dim someSQL As String
someSQL = "SELECT * FROM dbo.Total WHERE ID = " & TextBox1.Text & _
" And Source IN (" & Sheet1.Cells(3, 19) & ")"
根据需要替换“Sheet1”的相应工作表名称。
答案 1 :(得分:1)
您需要解析,修剪,转义并引用单元格中的每个值,以获得:
SELECT * FROM dbo.Total WHERE ID = 'bob''s' And Source IN ('Test','Testing','Tested')
使用
Dim someSQL As String
someSQL = "SELECT * FROM dbo.Total WHERE ID = '" & escape(TextBox1.Text) & "' And Source IN (" & splitCell("s3") & ")"
Function splitCell(addr As String) As String
Dim v() As String
Dim i As Long
v = Split(Range(addr).Value, ",")
For i = 0 To UBound(v)
v(i) = "'" & escape(trim$(v(i))) & "'"
Next
splitCell = Join(v, ",")
If (Len(splitCell) = 0) Then splitCell = "''"
End Function
Function escape(sIn As String) As String
//you should use a prepared statement instead
escape = Replace$(sIn, "'", "''")
End Function