2010-05-17 110 views
1

我想更新我的代碼以包含單元格的內容S3。眼下細胞具有「測試,測試,測試」和查詢的相關部分的價值是這樣的:將單元格的內容添加到VBA查詢中

SELECT * FROM dbo.Total WHERE ID = " & TextBox1.Text & " And Source IN (What do I put in here?) 

你的幫助是非常讚賞

+0

什麼是資源? – wcm 2010-05-17 14:23:04

+0

這是我總表中的一列。不重要。我只想知道()裏面需要什麼。 – Daniel 2010-05-17 14:29:05

回答

1

怎麼這樣呢?

Dim someSQL As String 
someSQL = "SELECT * FROM dbo.Total WHERE ID = " & TextBox1.Text & _ 
      " And Source IN (" & Sheet1.Cells(3, 19) & ")" 

根據需要替換「Sheet1」的相應工作表名稱。

1

您將需要分析出,修剪,逃跑並報各3個值在細胞獲得:

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 
相關問題