[英]Dynamic field name in Access query and function, can this be done?
in an access database i have created a function which compares 3 fields in my query:在访问数据库中,我创建了一个 function 比较我的查询中的 3 个字段:
Fields:字段:
CostMacDON成本麦克唐
CostKentFRY CostKentFRY
CostBurgKIN CostBurgKIN
TurnMacDON转麦克唐
TurnKentFRY TurnKentFRY
TurnBurgKIN特恩伯格金
CustMacDON卡斯特麦克唐
CustKentFRY CustKentFRY
CustBurgKIN卡斯特堡金
Public function Eval(MacD, KentF, BurgK)
'Note: the real evaluation is a bit more complex, this is just an oversymplified example
if MacD>KentF and MacD>BurgK
Eval="MD is the highest"
else
Eval="MD is NOT the Highest"
endif
end function结束 function
to call the function in a access query view i use在我使用的访问查询视图中调用 function
Evaluate Cost: Eval(CostMacDON, CostKentFRY, CostBurgKIN)
==== ---- ---- ----
Evaluate Turn: Eval(TurnMacDON, TurnKentFRY, TurnBurgKIN)
==== ---- ---- ----
Evaluate Cust: Eval(CustMacDON, CustKentFRY, CustBurgKIN)
==== ---- ---- ----
BUT...with all this repetition of Cost/Turn/Cust, I was hoping to simplify the queries by adapting the function.但是......由于成本/转弯/客户的所有这些重复,我希望通过调整 function 来简化查询。 After some research, it seems that I should restructure the tables which I really want to avoid.
经过一些研究,我似乎应该重组我真正想避免的表格。 (as this is just a very small part of the puzzle)
(因为这只是拼图的一小部分)
I want the users to be able to add other field, so I would also like to avoid complex sql statements.我希望用户能够添加其他字段,所以我也想避免复杂的 sql 语句。 SO.. I would like to call the function like this
所以..我想像这样调用 function
Evaluate Cost: Eval("Cost")评估成本:Eval("Cost")
Public function Eval(EvalType as variant)
Dim MacD as Variant (??)
Dim KentF as Variant (??)
Dim BurgK as Variant (??)
MacD= EvalType & "MacDON"
KentF= EvalType & "KentFRY"
BurgK= EvalType & "BurgKIN"
' this however gives me the names of the 3 fields, and I want to compare the content??! ' 但是,这给了我 3 个字段的名称,我想比较内容??!
if MacD>KentF and MacD>KentF
Eval="MD is the highest"
else
Eval="MD is NOT the Highest"
endif
end function结束 function
Does anybody have an idea if this can be done?有人知道这是否可以做到吗?
You would have to open a recordset or use DLookup() aggregate function to pull data from table into function.您必须打开一个记录集或使用 DLookup() 聚合 function 将数据从表中提取到 function 中。
Probably also need to pass into function ID of record.大概还需要传入 function 的 ID 记录。
Public Function Eval(EvalType As Variant, varID As Variant) As String
Example with DLookup(): DLookup() 示例:
MacD = DLookup(EvalType & "MacDON", "tablename", "ID=" & varID)
Nothing in your code shows any interaction with tables/recordsets?您的代码中没有任何内容显示与表/记录集的任何交互? If you are asking how to retrieve values from fields in a table where the field names are not fixed but can vary dynamically, then that could be done by passing the field name as a variable to eg an open recordset.
如果您询问如何从表中字段名称不固定但可以动态变化的字段中检索值,则可以通过将字段名称作为变量传递给例如打开的记录集来完成。
Eg you can do例如你可以做
MyField$ = "MacDCost"
MyValue = MyRecordSet(MyField)
And you can play with MyField$ to your heart's content.您可以尽情地玩 MyField$。 Eg:
例如:
Dim MyRSt as Recordset
Dim Shops(3) As String
Dim Type(3) As String
Dim ShopTeller as Long
Dim TypeTeller as Long
Dim MyValue as Double
Shops(0)="MacD"
Shops(1)="BurK"
Shops(2)="KenF"
MyType(0)="Cost"
MyType(1)="Turn"
MyType(2)="Cust"
Set MyRst=CurrentDB.OpenRecordset("MyTable")
MyRst.MoveFirst
TypeTeller = 0
Do
MyValue = 0
ShopTeller = 0
Do
myValue = MyValue + CDbl(MyRst(Shops(ShopTeller) & MyType(TypeTeller)))
ShopTeller = ShopTeller + 1
Loop Until ShopTeller = UBound(Shops)
Msgbox "Total " & MyType (TypeTeller) & "from all junk food outlets is :" & MyValue, vbExclamation & VbOKOnly
TypeTeller = TypeTeller + 1
Loop Until TypeTeller = UBound(MyType)
MyRst.Close
Set MyRst = Nothing
However, if you want the user to be able to add/remove 'shops' etc then you really cannot keep that info in the same table - you will need to, for instance, have a table which contains the Shops and a table which contains the other data (replacing the 2 arrays above) and then link them through an ID field and use JOINs to retrieve the values.但是,如果您希望用户能够添加/删除“商店”等,那么您真的不能将该信息保存在同一个表中 - 例如,您需要有一个包含商店的表和一个包含其他数据(替换上面的 2 arrays),然后通过 ID 字段链接它们并使用 JOIN 检索值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.