我正在尝试遍历Access(2007-2010)中的一些查询,并且第一个查询每次都从表中获取不同的字段。如何在SQL语句的select子句中放置一个“参数”,以便每次运行查询时它都会获取正确的字段?如果我在其周围输入[]来输入参数,它就无法识别字段名称 - 它只会在每条记录中输入该名称。
以下是我想要描述的简化说明:
SELECT
Base.Trial,
Base.Timestep,
Base.Rate3,
Base.CashIndex
FROM
Mthly20141231Base AS Base
ORDER BY
Base.Trial,
Base.Timestep
每次VBA循环回到第一个查询,我希望它运行相同的东西但不是Base.Rate3我想要Base.Rate12
我不想在VBA中键入SQL代码。
感谢您的帮助!
UPDATE-这是替代方案的代码,因为Access不支持我想要它做的事情:
Sub LoopTesting()
Dim db As dao.Database
Dim RS_Inputs As Recordset
Dim increment As Long
Dim increment_prev As Long
Dim QueryString As String
Set db = CurrentDb
'This table has my increments I want to loop through
Set RS_Inputs = db.OpenRecordset("Test Periods")
RS_Inputs.MoveFirst
increment_prev = RS_Inputs("Timestep Increments")
Do Until RS_Inputs.EOF
increment = RS_Inputs("Timestep Increments")
QueryString = Replace(GetQueryStr("RatesbyTimestep"), increment_prev, increment)
db.QueryDefs("RatesbyTimestep").SQL = QueryString
increment_prev = increment
Call DeleteTable
db.Execute "Step 3 - MakeTable"
Call UpdatePrice(increment)
RS_Inputs.MoveNext
Loop
'reset query back to first increment
QueryString = Replace(GetQueryStr("RatesbyTimestep"), 360, 3)
db.QueryDefs("RatesbyTimestep").SQL = QueryString
End Sub
答案 0 :(得分:0)
我一直在制作PTQ(Pass Thru Queries),他们不会在创建时编辑列名与任何表,而是在运行时编辑。我可以
ptq_xxx as "Select ID, @arg1, DTS From myTable Where DTS >= @arg2."
sSQL = CurrentDb.QueryDefs("ptq_xxx").sql
sSQL = Replace(sSQL, "@arg1", myNewFieldName)
sSQL = Replace(sSQL, "@arg2", myDateValue)
Dim rst As ADODB.Recordset, nRecAffected As Long
Set rst = myConn.Execute(sSQL, nRecAffected)
像魅力一样。