如何使用OpenRecordSet?

时间:2017-12-24 22:47:01

标签: vba ms-access

我正在尝试使用sqlite malformed database

参数是查询:

OpenRecordSet

如果我把它作为查询写的话,那就完美了。

然而,Select * FROM Batiments WHERE Type = "2"; 我收到此错误:

OpenRecordSet

我不明白什么是错的,从我看到的类似案例的所有教程中,我的语法应该是正确的。

以下是完整代码:

Object variable or With block variable not set

1 个答案:

答案 0 :(得分:1)

记录集对象上的.OpenRecordset方法不打算打开新记录集,而是打开基于打开记录集的过滤记录集(使用.Filter属性)。

如果要打开新记录集,则需要使用CurrentDb.OpenRecordset

temp = "Select * FROM Batiments WHERE [Type] = " & Chr(34) & txtNouveauBatiment.Value & Chr(34) & ";"
Set dbs = CurrentDb
Set rstBatiment = dbs.OpenRecordset(temp)

If (rstBatiment.EOF) Then
    Set rstBatiment = dbs.OpenRecordset("Batiments")
    rstBatiment.AddNew
    rstBatiment!Type = txtNouveauBatiment.Value
    rstBatiment.Update
    Refresh
End If

请注意,我并没有真正获得Refresh行。如果您要刷新表单,通常会指定您在当前表单对象上执行方法,例如Me.Refresh

另请注意,Type是Access SQL中的reserved words之一,因此需要加入括号

相关问题