使用 Access VBA 更改表单的记录源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22940898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:42:13  来源:igfitidea点击:

Use Access VBA to change the record source for a form

vbams-accessaccess-vbasubform

提问by ksagar

I am creating a search form and I am unable to see the search results in the subform. The query runs fine, I have set the subforms record source to the query. I donot get any errors. When I click the search button the query runs and it shows the number of rows in the record selector at the bottom of the subform, but i cannot see the rows.

我正在创建一个搜索表单,但我无法在子表单中看到搜索结果。查询运行良好,我已将子表单记录源设置为查询。我没有收到任何错误。当我单击搜索按钮时,查询会运行,它会在子表单底部的记录选择器中显示行数,但我看不到这些行。

Here is my code for the OnClick event of the button:

这是我的按钮 OnClick 事件代码:

Private Sub cmdSearch_Click()

Dim tableName As String
Dim colName As String
Dim keyword As String
Dim strSQL As String

tableName = Me.cmbTableNames.Value
colName = Me.cmbColumnNames.Value
keyword = Me.txtKeyword.Value
strSQL = "Select * from [" & [tableName] & "] where [" & [colName] & "] like '*" &  [keyword] & "*';"
Debug.Print strSQL
Me.searchResultsForm.Visible = True

Forms![F_SearchForm]![searchResultsForm].Form.RecordSource = "Select * from [" &   [tableName] & "] where [" & [colName] & "] like '*" & [keyword] & "*';"
Forms![F_SearchForm]![searchResultsForm].Form.Requery
End Sub

Can someone tell me what I am doing wrong.

有人可以告诉我我做错了什么。

Thank You

谢谢你

This is what shows up in the Immediate Window for Debug.Print

这是显示在 Debug.Print 的即时窗口中的内容

Select * from [dbo_Internal Contacts] where [First Name] like '*Amy*';

My Form looks like this in the form view:

我的表单在表单视图中看起来像这样:

enter image description here

在此处输入图片说明

I have added some text boxes to my subform (around 35). Now if I run the query my form looks like this: enter image description here

我在我的子表单中添加了一些文本框(大约 35 个)。现在,如果我运行查询,我的表单如下所示: 在此处输入图片说明

How can I link these text boxes on the subform to the columns in the recordsource using vba?

如何使用 vba 将子表单上的这些文本框链接到记录源中的列?

Please help

请帮忙

回答by ksagar

I figured it out. Here's what I did

我想到了。这是我所做的

I modified the query to

我将查询修改为

Select * into tmpSearchResults from [" & [tableName] & "] where [" & [colName] & "] like '*" & [keyword] & "*';

And I gave the tmpSearchResults as recordsource to my subform.

我将 tmpSearchResults 作为记录源提供给我的子表单。

I drop the tmpSearchResults table everytime the the table name combobox is updated.

每次更新表名组合框时,我都会删除 tmpSearchResults 表。

It works just as I wanted it to.

它就像我想要的那样工作。