我在这样的表中保存了一个日期:
Me!lastchangedate.Caption = Now
Set db = CurrentDb
Set rs = db.OpenRecordset("background", dbOpenTable)
rs.AddNew
rs![date] = Me!lastchangedate.Caption
rs.Update
rs.Close
稍后我想从数据库中读出这个日期,并在标签中显示:
sqlstrdate = "SELECT date FROM background " _
& " WHERE SAP_ID = '" _
& Me!sapidtxt.Value & "'"
retvaldate = CurrentDb.OpenRecordset(sqlstrdate)
直到这里工作但如果我现在尝试将“retvaldate”显示为MsgBox或在标签中我总是收到错误消息:错误13类型不匹配。
我试图用这个来在标签中显示保存的日期。
Me!lastchangedate.Caption = (retvaldate)
是否有更改标签类型的选项,或者我必须将“retvaldate”更改为日期类型(这也会给我带来相同的错误)。
答案 0 :(得分:0)
您正在尝试设置'retvaldate',就像打开记录集一样。以下应该提供正确的结果(顺便说一句,为什么你使用像'date'这样的保留字作为字段名?)
Dim rs As Recordset
Dim sqlstrdate As String
sqlstrdate = "SELECT date FROM background " _
& " WHERE SAP_ID = '" _
& Me!sapidtxt.Value & "'"
Set rs = CurrentDb.OpenRecordset(sqlstrdate)
If Not rs.EOF Then
retvaldate = rs.Fields("Date")
Else
retvaldate = "No Records"
End If
rs.Close
Set rs = Nothing
'Then later... but I hope the variable is in scope (Global, form, subroutine)
Me!lastchangedate.Caption = retvaldate