简体   繁体   English

向查询传递参数[Access 2016]

[英]Passing a query a parameter [Access 2016]

To make a longer story shorter: 缩短故事长度:

I'm an Access noob, doing a quick-and-dirty conversion of a massive Excel spreadsheet into an Access database. 我是一个Access新手,将大量的Excel电子表格快速转换为Access数据库。 Part of the requirements are to mimic some of the functionality of Excel, specifically, pulling data from a certain table and doing some basic calculations on it (sums, averages, etc.). 要求的一部分是模仿Excel的某些功能,特别是从特定表中提取数据并对其进行一些基本计算(总和,平均值等)。

I've written a chain of queries to pull the data, count/sum it, etc., and have been testing them by using a manually-entered Parameter (ie, the kind where the input box pops up and asks you to type a response). 我编写了一系列查询来提取数据,对数据进行计数/求和,等等,并通过使用手动输入的参数(即,输入框弹出并要求您键入响应)。 Now that I'm ready to drop these queries into a (sub)form, though, I have no idea how to automatically pass that parameter from a box in the form into the subform into the query. 现在,我准备将这些查询放入一个(子)表单中,我不知道如何自动将该参数从表单中的框传递到子表单中进行查询。

Every query I've written uses a manually-entered Parameter named "MATCHNAME," which holds the name of an individual. 我编写的每个查询都使用一个名为“ MATCHNAME”的手动输入的参数,该参数保存一个人的名字。 In manual testing, if I enter this parameter on one query, all the queries it calls also get that value. 在手动测试中,如果我在一个查询中输入此参数,则调用的所有查询也会获得该值。 So, I think I just need to figure out how to tell the top query what MATCHNAME actually is, and that'll take care of it. 所以,我想我只需要弄清楚如何告诉顶级查询MATCHNAME实际是什么,就可以解决这个问题。

Problem is, I don't know how to do that in Access. 问题是,我不知道如何在Access中做到这一点。 If it was any other programming language, I'd do something like "queryXYZ(MATCHNAME);", but I don't think I can do that in Access. 如果是任何其他编程语言,我都会做类似“ queryXYZ(MATCHNAME);”的事情,但是我认为我无法在Access中做到这一点。 Plus, since the values queryXYZ returns are all calculated, I'm not sure how to add an extra MATCHNAME field, nor how to actually make sure that gets read by the queries, nor how to make sure it gets passed down the chain. 另外,由于queryXYZ返回的值都是经过计算的,所以我不确定如何添加一个额外的MATCHNAME字段,也不确定如何真正确保查询读取该值,也不确定如何确保它沿链传递。 I've even tried creating a Parameter in design view, then trying to set up Link Master Fields, but the Parameter doesn't appear in that window. 我什至尝试在设计视图中创建参数,然后尝试设置“链接主字段”,但是该参数未出现在该窗口中。

I'd also like to re-run these queries whenever a new record is pulled up, but I'm not sure how to do that either--ie, the numbers should be current for whatever record I'm looking at. 每当提取新记录时,我也想重新运行这些查询,但是我也不知道该怎么做-即,无论我在看什么记录,数字都应该是最新的。

And, before we go there--I feel like a Relationship is out of the question, as the data itself is auto-generated, and is in rough enough shape to where I can't guarantee that any given key is wholly unique, and large enough (20k+) that, outside of writing a magical script, I can't assign a numerical key. 而且,在我们去那里之前-我觉得没有关系,因为数据本身是自动生成的,并且形状足够粗,以至于我无法保证任何给定的键都是唯一的,并且足够大(超过20k),除了编写神奇的脚本外,我无法分配数字键。 However, I don't know much about Relationships in Access, so please prove me wrong. 但是,我对Access中的关系了解不多,所以证明我错了。

(Is this all making sense?) (这一切有意义吗?)

Do you have any suggestions for me--for how to make a subform read a field on the main form to run its queries on? 您是否对我有任何建议-如何使子窗体读取主窗体上的字段以对其进行查询? Alternately, is there an easier way to do this, ie, to bed SQL calls inside a form? 或者,是否有更简单的方法可以做到这一点,即将SQL调用放在表单中?

Thanks very much for your help... 非常感谢您的帮助...

You can use SQL as the recordsource of the subform in the property tab and use the afterupdate event of your matchname field to change yourform.recordsource = "Select * from table where filteredfieldname = & me.matchname & ";" . You can also use sql as the control source of form fields. To pass criteria to filter the subform using the whole table as the recordsource, add an event procedure to your field's after update event like this 您可以在属性选项卡中将SQL用作子表单的记录源,并使用matchname字段的afterupdate事件更改yourform.recordsource = "Select * from table where filteredfieldname = & me.matchname & ";" 。您也可以使用sql作为表单字段的控制源。若要通过使用整个表作为记录源的条件来筛选子表单,请向该字段的after after事件添加一个事件过程,如下所示

`In the declarataions at the top
Global mtchnmfltr as string 

Private Sub MATCHNAME_AfterUpdate() 
'use the same procedure for Private Sub yourmainform_Current() 
mtchnmfltr  = "[yourfilterfield] = " & Chr(34) & me.matchname & Chr(34)  
'if matchname is not text then just = "[yourfilterfield] = " & me.matchname  
with me.subformname.form  
.filter = mtchnmfltr  
.filteron = true  
end with
'Build your sql as a string for your sum avg fields etc. using mtchnmfltr in the where clause  
 me.yoursumfield.controlsource = "Select...where " & mtchnmfltr & ";"
 'etc.
 end sub  

Or you could throw Matchname into a sql recordsource of the subform and add the function fields to the subform on the same on current and after update events 或者,您可以将Matchname放入子窗体的sql记录源中,并将函数字段添加到当前和更新事件之后的子窗体中

if me.newrecord = true then    
me.dirty = false  
end if  
me.subform.form.recordsource = "Select Table.Matchname, sum(yourfield) as sumalias, _  
(etc.) from yourtable where table.matchname = " & chr(34) & me.matchname & _  
chr(34) & Group By table.matchname" 

If you are storing your sums etc in a table you need to do it a bit different, since your controls controlsource are bound to fields. 如果将总计等存储在表中,则需要做一些不同的操作,因为控件控件源绑定到字段。

dim strsqlsumfld as string 
dim rs as dao.recordset 
strsqlsumfld= "Select SUM.....AS sumfldalias where " & mtchnmfltr & ";"  
set rs = currentdb.openrecordset(strsqlsumfld) 
me.yoursumfield = rs("sumfldalias")
rs.close

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

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