MS Access .Movenext / .moveprevious

时间:2018-07-16 08:34:41

标签: vba ms-access

因此,我尝试使用tblReceipts变量作为记录集,该变量有效,并且已在form_load中进行设置,但是当我尝试在下一个移动和上一个按钮的移动中使用它时,它表示该变量没有设置,似乎它初始化了变量,但仅用于表单加载,但没有执行其余的代码,有没有办法公开它?

我得到的错误是:错误91,对象变量或未设置块变量

Private Sub Form_Load()

Dim tblReceipts As Recordset
Set tblReceipts = CurrentDb.OpenRecordset("SELECT * FROM [tblReceipts]", dbOpenDynaset)

tblReceipts.MoveFirst
Me.RecieptID.Value = tblReceipts![ReceiptID]
Me.RefNo.Value = tblReceipts![ReferenceNumber]
Me.TrackingID.Value = tblReceipts![TrackingNumber]
Me.CustID.Value = tblReceipts![CustomerID]
Me.CustPostcode.Value = tblReceipts![CustomerPostcode]

End Sub


Private Sub btnNext_Click()

MsgBox "There are no more records to show", vbOKOnly, "End of file"

Exit Sub

If tblReceipts.EOF Then

Else

tblReceipts.MoveNext
Me.RecieptID.Value = tblReceipts![ReceiptID]
Me.RefNo.Value = tblReceipts![ReferenceNumber]
Me.TrackingID.Value = tblReceipts![TrackingNumber]
Me.CustID.Value = tblReceipts![CustomerID]
Me.CustPostcode.Value = tblReceipts![CustomerPostcode]

End If


End Sub

感谢您的帮助,我将尝试回答任何问题。

1 个答案:

答案 0 :(得分:1)

  

我在form_load之外的选项比较数据库下有Public tblReceipts As Recordset作为记录集

但是你也有

Dim tblReceipts As Recordset

内部 Form_Load()。而且局部变量优先于全局变量,因此这里设置的是局部变量。

Dim中删除Form_Load()语句。