从Access SQL选择查询返回值

时间:2018-09-06 17:04:48

标签: ms-access

我想将SELECT的结果保存到MS Access中的变量中。 它应该从PriceFeed表中选择最近的报价,其中表中的股票代码与从comboBox表单中选择的项目匹配。

Dim sq2 As Variant
sql = "SELECT PriceFeed.Offer FROM PriceFeed WHERE PriceFeed.StockSymbol =    Me.CBSymbol.Column(1) AND DateTime =(SELECT MAX([PriceFeed.DateTime])FROM PriceFeed)"
DoCmd.RunSQL sq2

1 个答案:

答案 0 :(得分:0)

您可以使用 DMax DLookup

RecentPrice = DLookup("Offer", "PriceFeed", "StockSymbol = " & Me.CBSymbol.Column(1) & " AND DateTime = DMax('DateTime', 'PriceFeed')")

如果未选择CBSymbol,为避免错误:

RecentPrice = DLookup("Offer", "PriceFeed", "StockSymbol = " & Nz(Me.CBSymbol.Column(1), 0) & " AND DateTime = DMax('DateTime', 'PriceFeed')")