如何在 MS Access 中通过 VBA 填充文本框? [英] How can I populate textbox through VBA in MS Access?

查看:67
本文介绍了如何在 MS Access 中通过 VBA 填充文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表 RR_info,其中包含以下字段 RR_ID、HR_ID、No_of_Beds、Room_Category.现在我希望通过带有 Form_load 事件的 VBA 代码,我应该为所有这些表字段填充文本框.为此,我编写了一个查询,它根据 hotel_id 作为条件获取某些记录,但代码不起作用.

I have a table RR_info which hold following fields RR_ID, HR_ID, No_of_Beds, Room_Category. Now i want that through VBA code with Form_load event I should populate textboxes for all these table fields. For this I wrote a query which get certain records according to hotel_id as a criteria but code is not working.

Private Sub Form_Load()
Dim SQL As String
Dim db As Database
Dim rs As DAO.Recordset

SQL = "select * from RR_info where hr_id = " & Forms![hhrrr]![List38] & ";"
Set db = CurrentDb
Set rs = db.OpenRecordset(SQL)

Me.RR_ID.Text = rs!RR_ID
Me.HR_ID.Text = rs!HR_ID
Me.Room_No.Text = rs![Room No]
Me.No_of_Beds.text = rs!No_of_Beds
Me.Room_Category.text = rs!Room_Category

Set rs = Nothing
Set db = Nothing

End Sub

这是我想通过VBA根据条件添加表格数据的表格图片.

This is the Picture of Table in which I want to add table data according to criteria through VBA.

推荐答案

.Text 属性仅适用于当前具有焦点的控件.当您尝试在任何其他控件上引用 .Text 时,您将触发错误.无论控件是否具有焦点,.Value 属性都可用.

The .Text property is only available for the control which currently has focus. When you attempt to reference .Text on any other control, you will trigger an error. The .Value property is available whether or not the control has focus.

为了安全起见,将它们全部更改为 .Value.

To be safe, change them all to .Value.

Me.RR_ID.Value = rs!RR_ID
Me.HR_ID.Value = rs!HR_ID
Me.Room_No.Value = rs![Room No]
Me.No_of_Beds.Value = rs!No_of_Beds
Me.Room_Category.Value = rs!Room_Category

这篇关于如何在 MS Access 中通过 VBA 填充文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆