简体   繁体   English

MS Access VBA选择查询

[英]MS Access VBA Select query

So first sorry for my english, my native language is german. 因此,首先抱歉我的英语,我的母语是德语。

I have an ACCESS DB with a few Tables, the DB is filled with healing Plants, and there is one Table named "Issues" it looks like that: 我有一个带有几个表的ACCESS数据库,该数据库中充满了正在愈合的植物,并且有一个名为“问题”的表,它看起来像这样:

Columns: ID -- Name -- Headache -- Pain -- Vomitting - and so on.... 列:ID-名称-头痛-疼痛-呕吐-等等。

And the Columns for the indications like headache and so on, are boolean- True or False. 诸如头痛之类的迹象的列是布尔值-True或False。

Now i`d like to make an query that asks the user (With a listbox in a form or so, or a Text input) to tell his indication, and then there should be a list of Substances/Plants where the Value for the indication (ColumnName) is true. 现在,我想进行一个查询,要求用户(带有某种形式的列表框或文本输入)告诉他的指示,然后应该有一个物质/植物的列表,指示的值(ColumnName)为true。

I think thats a parameter for a search in a table for columns. 我认为多数民众赞成在一个表中搜索列的参数。

I'd look at the design of your database. 我将看一下您的数据库的设计。 Having a table with separate columns for each issue would be a real headache to update if another issue became apparent. 如果另一个问题变得很明显,那么要为每个问题创建一个带有单独列的表格将是一件令人头疼的更新工作。

I'd probably use four tables for this: 我可能为此使用四个表:

  • Users : UserID (AutoNum, PK ), UserName (Text) 用户 :UserID(自动编号, PK ),UserName(文本)
    在此处输入图片说明
  • Plants : PlantID (AutoNum, PK ), PlantName (Text) 植物 :PlantID(AutoNum, PK ),PlantName(文本)
    在此处输入图片说明
  • IssueList : IssueID (AutoNum, PK ), IssueDescription (Text) IssueList :IssueID(自动编号, PK ),IssueDescription(文本)
    在此处输入图片说明
  • User_Issues : UserID (Num, PK ), PlantID (Num, PK ), IssueID (Num, PK ), HasIssue (Boolean) User_Issues :UserID(数字, PK ),PlantID(数字, PK ),IssueID(数字, PK ),HasIssue(布尔)
    在此处输入图片说明

The User_Issues table has a composite key made up each identifier from the other tables - this will ensure that a user can't have the same issue for a plant more than once. User_Issues表具有一个组合键,该组合键由其他表中的每个标识符组成-这将确保用户不会多次遇到同一工厂的问题。

When a new user is created a query runs to update the User_Issues table: 创建新用户后,将运行查询以更新User_Issues表:

INSERT INTO User_Issue(PlantID, IssueID, UserID)
SELECT      PlantID, IssueID, UserID
FROM        Plants, IssueList, Users
WHERE       UserName = "Darren"

This will create a Cartesian product from the plants and issues for each user. 这将为每个用户从工厂和问题创建笛卡尔产品。 So, for example, if you have two plants and three issues you'll get 2x3 records created - a possible 6 issues across the two plants. 因此,例如,如果您有两个工厂和三个问题,则将创建2x3记录-两个工厂中可能有6个问题。
在此处输入图片说明

This SQL will allow you to allocate an issue: 此SQL将允许您分配问题:

SELECT        UserName
            , PlantName
            , IssueDesc
            , HasIssue
FROM        ((
             User_Issue INNER JOIN Users ON User_Issue.UserID = Users.UserID)
                        INNER JOIN Plants ON User_Issue.PlantID = Plants.PlantID)
                        INNER JOIN IssueList ON User_Issue.IssueID = IssueList.IssueID
ORDER BY    PlantName, IssueDesc

在此处输入图片说明

To view the issues you just have to add WHERE HasIssue to the above SQL. 要查看问题,只需将WHERE HasIssue添加到上述SQL中。

SELECT        UserName
            , PlantName
            , IssueDesc
            , HasIssue
FROM        ((
             User_Issue INNER JOIN Users ON User_Issue.UserID = Users.UserID)
                        INNER JOIN Plants ON User_Issue.PlantID = Plants.PlantID)
                        INNER JOIN IssueList ON User_Issue.IssueID = IssueList.IssueID
WHERE       HasIssue
ORDER BY    PlantName, IssueDesc

在此处输入图片说明

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

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