简体   繁体   English

MS Access-在多值字段中查询多个答案?

[英]MS Access- Query for multiple answers in a Multi-Value Field?

I am new to Stack Overflow but think I have thoroughly searched for an answer to this so I hope it isn't repetitive. 我是Stack Overflow的新手,但我想我已经彻底搜索了答案,所以我希望它不会重复。 I'm also a total novice with Access and SQL so please bear with me! 我还是Access和SQL的新手,所以请多多包涵! I am learning everything as I go. 我正在学习中的一切。

We've created a database of education programs, with many “multi-valued fields” that appear as drop-downs with check boxes. 我们已经创建了一个教育计划数据库,其中包含许多“多值字段”,这些下拉列表带有复选框。 I wish we'd known beforehand that these are not the best functionality of Access, but this is the data that we have, so we will have to work with it. 我希望我们事先知道这些不是Access的最佳功能,但这是我们拥有的数据,因此我们将不得不使用它。

My question is, in a query how can you indicate a criteria of having multiple boxes checked? 我的问题是,在查询中如何指示选中多个复选框的条件?

For example, I want to select and count all the records that have multiple boxes checked in the “source” field- meaning how many of these programs appeared on multiple lists we looked at? 例如,我要选择并计算在“源”字段中选中多个框的所有记录,这意味着在我们查看的多个列表中出现了多少这样的程序?

The values are all strings. 这些值都是字符串。 I also have started to understand that they each have an underlying numerical value that Access codes them as, but I don't know how to find out what those values are for this different lookups. 我也已经开始理解它们每个都有一个基本数字值,Access会将它们编码为,但是我不知道如何找出这些值用于不同的查找。 Also, all of our data is stored in one table, there aren't any other linked tables. 同样,我们所有的数据都存储在一个表中,没有其他链接表。

For example: 例如:

SELECT [Database Source].Value
FROM [Database]
WHERE ([Database Source].Value = **TWO OR MORE VALUES**)

I just can't figure out what the criteria syntax is to indicate TWO OR MORE VALUES . 我只是不知道什么标准语法表示两个或更多值 So far my only way of doing it is to create the combinations for each one: 到目前为止,我唯一的方法是为每个组合创建组合:

WHERE ([Database Source].Value = Source 1 AND Source 2) OR ([Database Source].Value =  Source 1 AND Source 3) OR *(And so on)*

I hope there is a more efficient way to do this than typing out all possible combinations. 我希望有一种比输入所有可能组合更有效的方法。

Any guidance is much appreciated. 任何指导是非常感谢。 Thank you! 谢谢!

I'm looking here: https://support.office.com/en-us/article/Using-multivalued-fields-in-queries-6f64f92d-659f-411c-9503-b6624e1e323a 我在这里寻找: https : //support.office.com/en-us/article/Using-multivalued-fields-in-queries-6f64f92d-659f-411c-9503-b6624e1e323a

Okay, if I understand right, "Database" is the name of your table and "Database Source" is your multi-value field. 好的,如果我理解正确,“数据库”是表的名称,“数据库源”是您的多值字段。 (I have to note that Database is a very confusing name for a table in this context since it makes the sql look like we are referring to database properties) (我必须注意,在这种情况下,数据库是表的一个非常混乱的名称,因为它使sql看起来像我们在引用数据库属性一样)

Your multi-value field is basically abstracting a one-to-many relationship in a weird wizardy way. 您的多值字段基本上以一种怪异的向导方式抽象了一对多关系。 Your [Database Source] holds a list of ids, and the id's associate with another table that has your possible values. 您的[数据库源]包含一个ID列表,该ID与另一个具有您可能的值的表相关联。 If you refer to [Database Source].value in your query then you break your multi-values into separate rows, so you don't want to do that. 如果您在查询中引用[Database Source] .value,则会将多值分解为单独的行,因此您不想这样做。

It looks to me like you need to have another field in your column to group on. 在我看来,您需要在列中包含另一个字段进行分组。 If you have an ID column that would work. 如果您有可以使用的ID列。

SELECT Database.[field x],Database.[Database Source] 
FROM Database
GROUP BY [field x]
Having Count(Database.[Database Source]) > 1;

First, build a string from your selected values. 首先,根据您选择的值构建一个字符串。 Then, use that string in an "IN" clause. 然后,在“ IN”子句中使用该字符串。 Like this: 像这样:

Dim strSQL as String
Dim vItm as Variant
Dim rec As DAO.Recordset

For Each vItm In Me!Listbox.ItemsSelected
    strSQL = strSQL & ListBox.Column(0, vItm) & ","
Next vItm

strSQL = left(strSQL,len(strSQL) - 1) ' remove last comma

Set rec = CurrentDb.OpenRecordset("Select * FROM MyTableName " _
    WHERE MyFieldName In (" & strSQL & ")")

I'm not sure which column in your listbox actually holds the value you want to search on, if I knew the code that fills your listbox I could be more specific but the code above assumes your value is in the first column. 我不确定您的列表框中的哪一列实际上包含您要搜索的值,如果我知道填充您的列表框的代码,我可能会更具体,但是上面的代码假定您的值在第一列中。 The Columns collection is zero-based, so Column(0) is the first column, Column(1) is the second column, etc... Columns集合从零开始,因此Column(0)是第一列,Column(1)是第二列,依此类推。

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

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