2012-02-07 37 views
0

我有一个连续的形式,我想通过我的VBA脚本中的sql select语句填充。我似乎无法做到这一点:填充访问表格通过sql在vba

sel = "Select * from table1 where a = 10;" 
Set SQL = db.OpenRecordset(sel) 
SQL.Requery 

回答

3

为什么不把你的select指令放在窗体的recordsource属性中?

Me.recordsource = "Select * from table1 where a = 10;" 

如果你的脚本是在形式的程序之一,或

myForm.recordsource = "Select * from table1 where a = 10;" 

如果你的脚本是一个独立的模块

1

中如果你想使用记录,而不是只设置RecordSource(在Philippe Grondier's answer已经建议等),你也可以这样做:

Set Me.Recordset = db.OpenRecordset("select ...") 

我必须承认,设置RecordSource是“标准方法”(尤其是如果您已经有一条SQL语句并希望使用它填充表单),但我仍然想要展示此替代解决方案。
我通常用它来填充由函数返回的Recordset的表单。

+0

完全正确,这也是我这样做的方式,虽然我系统地使用ADODB而不是DAO记录集。 – 2012-02-07 18:11:55

+0

是的,我也是(我们使用Access作为SQL Server前端) – 2012-02-07 18:18:20