简体   繁体   English

MS Access:在子窗体中运行查询

[英]MS Access: run a query in a subform

I have already searched through and tried all recommendations I can find, but nothing has worked. 我已经搜索并尝试了所有可以找到的建议,但是没有任何效果。 My problem is that I want to create a search function in a form and press a button to display the search results in a subform WITHOUT opening a new datasheet. 我的问题是我想在表单中创建搜索功能,然后按一个按钮在子表单中显示搜索结果,而无需打开新的数据表。

I have written a query which successfully searches for items based on the parameters I give it (the parameters are written in text boxes in the form). 我编写了一个查询,该查询根据我给它的参数成功地搜索了项目(这些参数写在表格的文本框中)。

I also have a button in the form which opens the query. 我还有一个打开查询形式的按钮。 But this opens the query in a new datasheet window. 但这会在新的数据表窗口中打开查询。 Not only does it do that, but I'm fairly certain that doesn't do anything to the subform at all. 它不仅可以做到这一点,而且我可以肯定地说,它对子窗体完全没有任何作用。

This is all inside a larger navigation form. 这些都在较大的导航表单中。

Edit: I'll include the query code, but it isn't really that important, so I'll put it in a format that shows only a little bit: 编辑:我将包括查询代码,但它实际上并不那么重要,因此我将其放置为仅显示一点点的格式:

SELECT [Car Table].Car_VIN, [Car Table].Car_Class, [Car Table].Car_BodyType, [Car Table].Car_Colour, [Car Table].Car_Make, [Car Table].Car_Model, [Car Table].Car_EngineType, [Car Table].Car_TransmissionType, [Car Table].Car_GPSAvailability, [Car Table].Car_BootSpace, [Car Table].Car_FuelConsumptRate, [Car Table].Car_SeatNumber, [Car Table].Car_GreenStarRating, [Car Table].Car_ANCAPSafetyRating

FROM [Car Table]

WHERE ((([Car Table].Car_VIN) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![txtVIN] & "*") AND (([Car Table].Car_Class) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbClass] & "*") AND (([Car Table].Car_BodyType) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbBodyType] & "*") AND (([Car Table].Car_Colour) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbBodyType] & "*") AND (([Car Table].Car_Make) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![txtMake] & "*") AND (([Car Table].Car_Model) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![txtModel] & "*") AND (([Car Table].Car_EngineType) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbEngineType] & "*") AND (([Car Table].Car_TransmissionType) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbTransmissionType] & "*") AND (([Car Table].Car_GPSAvailability) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![ChGPSAvailability] & "*") AND (([Car Table].Car_SeatNumber) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![txtSeatNumber] & "*") AND (([Car Table].Car_GreenStarRating) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbGreenStarRating] & "*") AND (([Car Table].Car_ANCAPSafetyRating) Like "*" & [Forms]![Navigation Form]![NavigationSubform].[Form]![cmbANCAPSafetyRating] & "*") AND ((Abs([car_fuelconsumptrate]-[Forms]![Navigation Form]![NavigationSubform].[Form]![txtFuelConsumption]))<=2) AND ((Abs([car_bootspace]-[Forms]![Navigation Form]![NavigationSubform].[Form]![txtBootSpace]))<=100));

I have tried creating a button that performs a requery on the subform, but the requery didn't do anything when I clicked it, I am guessing because I didn't really reference the query itself anywhere. 我尝试创建一个在子窗体上执行重新查询的按钮,但是单击该查询后,它什么也没做,这是因为我并没有真正引用查询本身。

I also tried doing a split-view form, but not only did I not get anywhere, I did not want to have a split view. 我也尝试过创建拆分视图表单,但是我不仅没有得到任何结果,而且我也不想拥有拆分视图。

RIGHT NOW I have a button that runs the query, textboxes which I can enter parameters into (for example if I typed in 100 into the bootspace text field, it would return all values near 100 including 100, or if I also typed in 'kia' into the carmake text field it would return all cars that have a bootspace near 100 and are Kias) and my subform underneath, which is completely blank. 现在,我有一个运行查询的按钮,可以输入参数的文本框(例如,如果我在引导空间文本字段中键入100,它将返回所有接近100的值,包括100,或者如果我也键入了'kia '进入carmake文本字段,它将返回引导空间接近100且为Kias的所有汽车,以及下面的我的子表单,该表单完全空白。

Edit: VBA code 编辑:VBA代码

Private Sub Command409_Click()

Me.[Car Table subform1].Form.Requery

End Sub

Set the query to the subform-recordsource in the form properties. 将查询设置为表单属性中的subform-recordsource。 I assume you have done that already. 我想你已经做到了。

In your button place vba code: 在您的按钮中放置VBA代码:

Private Sub button_click()
    Me.PUT_SUBFORM_NAME_HERE.Form.Requery
End sub

at the end of your search button's on click event write this line of code : 在搜索按钮的单击事件结束时,编写以下代码行:

Main_form_name.sub_form_name.Form.Requery
End Sub

that should do the trick 这应该够了吧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM