我发现了这篇文章Set Default Value of Text Box to Query Result 我正在尝试使用以下代码,但始终出现错误。查询的名称是MaxNote,然后查询中记录的字段是MaxNote。
Public Function MaxNote()
MaxNote = CurrentDb.OpenRecordset("MaxNote").Fields("MaxNote")
End Function
答案 0 :(得分:1)
将 DLookup 用于此类简单任务,并注意 DefaultValue 是文本,如果MaxNote可以为 Null,则使用 Nz :
Me!YourTextbox.DefaultValue = Chr(34) & LTrim(Nz(Str(DLookup("MaxNote", "MaxNote")))) & Chr(34)
答案 1 :(得分:0)
Public Function MaxNote()
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("MaxNote")
rst.MoveFirst
MaxNote = rst.Fields("MaxNote")
End Function
如果您要先打开而不是DLookup,请尝试此操作
答案 2 :(得分:0)
在这里On Error Resume Next
可能会有所帮助,因为错误表明结果为空。
Public Function MaxNote() as Variant
MaxNote = "" ' default return value for not records found. Can be NULL or 0 too
On Error Resume Next ' if lookup fails code resumes next line, what is End Function (nothing happens)
MaxNote = CurrentDb.OpenRecordset("MaxNote").Fields("MaxNote") ' fetch the field from first row of recordset (standard after .OpenRecordSet() ), if no results an error is raised, but function resumes next line of code, what ends the function and still with MaxNote = "", the value if no records are found
End Function