将一个表中的多个记录传递到SQL语句中

时间:2013-08-06 19:33:10

标签: sql vba ms-access pass-through

我试图从访问中的表中获取许多值(总是会更改),并将它们置于SQL语句的限制之内。

我有Dlookup(),但据我们所知它只拉了1个值。

如何循环使用以获取所有值?

相关代码:

  1. Dim Value as string
  2. Value = DLookup("Values", "value")
  3. sSQL = ......... WHERE AL.LIMIT '" & value & "'" &_

1 个答案:

答案 0 :(得分:0)

我不知道你要做什么,你的问题就像泥巴一样清晰。但是,我要去捅它。

尝试这样的事情:

Dim db as Database
Dim rec as Recordset

Db = CurrentDB
'This assumes you're pulling one specific record from a table with many records
rec = db.OpenRecordSet("Select * from YourTableName WHERE Something = SomethingElse")

Do While rec.EOF = False
  MyFirstValue = rec(0)
  MySecondValue = rec(1)
  MyThirdValue = rec(2)
  ...ect
Loop

sSQL = "Select * from Whatever WHERE FirstParam = '" & MyFirstValue & "' AND SecondParam = '" & MySecondValue & "' AND ThirdParam = '" & MyThirdValue & "'"

这需要大量编辑,但如果我理解正确,那应该是一个很好的起点。