2011-10-31 68 views
4

我知道我在想这个,但我想检查单个记录中的单个值/字段。例如,我想知道主键为33的记录中“closedDate”字段的值是否为空。如何从一个记录中读取一个字段

我的想法是这样的:

dim db as DAO.Database 
dim rs as DAO.Recordset 
set db = CurrentDb 
set rs = db.OpenRecordset("record_holdData") 

If not isNull(rs.Fields("closedDate")) then 
    'do nothing 
Else 
    'add a close date 
End If 

但我不认为这是正确的。它没有指定记录号码。在应用程序中,通过绑定到有问题的记录打开表单,但我认为CurrentDb不考虑这一点,而是引用整个表格。

所以我的问题是,如何以这种方式打开记录集并仅在该特定记录中引用此字段?

回答

5

你找到了你想要的答案,但我会用DLookup Function来代替。

Dim db As DAO.Database 
Dim strWhere As String 
Dim varClosedDate As Variant 
Set db = CurrentDb 
strWhere = "id = 33" 
varClosedDate = DLookup("closedDate","record_holdData",strWhere) 
If IsNull(varClosedDate) = True Then 
    'use today's date as closedDate 
    db.Execute "UPDATE record_holdData Set closedDate = Date() WHERE " & strWhere 
End If 
+0

已经基准了更好的时间? – Sinaesthetic

+0

我没有做比较测试。有了id字段的索引,你的记录集解决方案和这个应该都很快。如果我不得不猜测,我会选择DLookup更快,因为它不包含创建记录集对象的开销。但是,如果没有精确的测试方法,我怀疑你会注意到差异。如果性能对您的情况至关重要,请对其进行测试。 – HansUp

+0

很酷谢谢。我完全忘记了dlookup – Sinaesthetic

3

我的印象是,.OpenRecordset()方法的参数只是表名。但事实证明,你也可以发送一个查询:

set rs = db.OpenRecordset("select * from record_holdData where id = 33") 
相关问题