简体   繁体   English

在 Access 中使用按表单查询时,有没有办法显示字段中的值而不是用于搜索的 ID 值?

[英]Is there a way when using query by form in Access to display a value from a field other than the ID value it uses to search?

Forgive the word salad of the title--this is difficult to reduce to one sentence.原谅标题的沙拉这个词——这很难简化为一句话。

I have a non-vital but persistently annoying problem with an Access database I'm working on, one which will ultimately be used as a media database in my workplace.我正在处理的 Access 数据库有一个非重要但持续烦人的问题,该数据库最终将用作我工作场所的媒体数据库。 Full disclosure, I'm new to Access and SQL, so there may be some underlying flaws in my database structure--I have the continued concern that it's not well normalized, so perhaps one of you can suggest improvements there.完全公开,我是 Access 和 SQL 的新手,所以我的数据库结构可能存在一些潜在的缺陷——我一直担心它没有很好地规范化,所以也许你们中的一个人可以在那里提出改进建议。

The gist of the problem is that I've created a form to perform a multi-field query.问题的要点是我创建了一个表单来执行多字段查询。 Most of the fields are combo boxes that draw their values from "auxiliary" tables (titled " tbl_MediaType ," " tbl_Author " and so on), which are all connected via field relationships with the main table (" tbl_Items ").大多数字段是从“辅助”表(标题为“ tbl_MediaType ”、“ tbl_Author ”等)中tbl_Author其值的组合框,这些表都通过字段关系与主表(“ tbl_Items ”)连接。 The query is functioning properly insofar as it can search using multiple criteria, and when selecting on the combo boxes you can see both the record ID number and the value associated with that ID, but once the value is selected, only the record ID is displayed.查询运行正常,因为它可以使用多个条件进行搜索,并且在组合框中选择时,您可以看到记录 ID 号和与该 ID 关联的值,但是一旦选择了该值,则只显示记录 ID . Clearly, a visual aid is in order: Media Type Drop-Down Media Type Field once selected显然,视觉辅助是有序的: Media Type Drop-Down Media Type Field 一旦选择

The database is going to be used internally and therefore doesn't have to be a thing of beauty, which is to say we can totally live with this.数据库将在内部使用,因此不必非常美观,也就是说我们完全可以接受它。 Still, I'd like to save users the step of having to click on the drop-down to double-check that they've selected the right media type, etc. That'll become a chore particularly if a user is doing multiple queries in a row.不过,我想为用户省去必须单击下拉列表以仔细检查他们是否选择了正确的媒体类型等的步骤。这将成为一件苦差事,尤其是当用户进行多个查询时连续。

I've tried to work around this by making the value field itself (ie, the field titled " MediaType " on tbl_MediaType ) the field that the relationship between tables is based on (ie, the field that forms the relationship between tbl_MediaType and tbl_Items ) instead of using the numerical ID (ie, the field titled " MediaTypeID ") as the related field.我试图通过将值字段本身(即tbl_MediaType上标题为“ MediaType ”的字段)作为表之间关系所基于的字段(即,形成tbl_MediaTypetbl_Items之间关系的字段)来解决这个问题而不是使用数字 ID(即标题为“ MediaTypeID ”的字段)作为相关字段。 This causes the form to break.这会导致表单中断。 I also tried simply not displaying the numerical ID in the search drop-down, but given that the query is built on matching what's in the search drop-down with a field on tbl_Items , that also breaks the form.我还尝试在搜索下拉列表中不显示数字 ID,但鉴于查询是建立在将搜索下拉列表中的内容与tbl_Items上的字段相匹配的基础上的,这也会破坏表单。

As of right now, the database is still under construction and therefore contains all dummy data, so if anyone wants to poke around in the file to tell me what I'm doing wrong, I can send it your way.截至目前,数据库仍在建设中,因此包含所有虚拟数据,因此如果有人想在文件中查看我做错了什么,我可以将其发送给您。 In the meantime, here's the SQL for the query, in case that's of any help:同时,这里是查询的 SQL,以防万一:

    SELECT 
  tbl_Items.ItemID, 
  tbl_Items.FileName, 
  tbl_Items.FileLocation, 
  tbl_Items.AuthorID, 
  tbl_Author.AuthorName, 
  tbl_Items.MediaTypeID, 
  tbl_MediaType.MediaType, 
  tbl_Items.SubjectCategoryID, 
  tbl_SubjectCategory.SubjectCategory, 
  tbl_Items.YearID, 
  tbl_Year.ActualYear, 
  tbl_Items.FileTypeID, 
  tbl_FileType.FileType 
FROM 
  tbl_FileType 
  INNER JOIN (
    (
      tbl_Author 
      INNER JOIN (
        tbl_MediaType 
        INNER JOIN (
          tbl_SubjectCategory 
          INNER JOIN tbl_Items ON tbl_SubjectCategory.SubjectCategoryID = tbl_Items.SubjectCategoryID
        ) ON tbl_MediaType.MediaTypeID = tbl_Items.MediaTypeID
      ) ON tbl_Author.AuthorID = tbl_Items.AuthorID
    ) 
    INNER JOIN tbl_Year ON tbl_Items.YearID = tbl_Year.YearID
  ) ON tbl_FileType.FileTypeID = tbl_Items.FileTypeID 
WHERE 
  (
    (
      (tbl_Items.YearID) Between [Forms] ! [frm_SearchForm] ! [YearSearchBox] 
      And [Forms] ! [frm_SearchForm] ! [YearSearchBox2]
    ) 
    AND (
      (
        IIf(
          [Forms] ! [frm_SearchForm] ! [CategorySearchBox] Is Null, 
          "*", [tbl_Items].[SubjectCategoryID] = [Forms] ! [frm_SearchForm] ! [CategorySearchBox]
        )
      )<> False
    ) 
    AND (
      (
        IIf(
          [Forms] ! [frm_SearchForm] ! [MediaTypeSearchBox] Is Null, 
          "*", [tbl_Items].[MediaTypeID] = [Forms] ! [frm_SearchForm] ! [MediaTypeSearchBox]
        )
      )<> False
    ) 
    AND (
      (
        IIf(
          [Forms] ! [frm_SearchForm] ! [AuthorSearchBox] Is Null, 
          "*", [tbl_Items].[AuthorID] = [Forms] ! [frm_SearchForm] ! [AuthorSearchBox]
        )
      )<> False
    ) 
    AND (
      (
        IIf(
          [Forms] ! [frm_SearchForm] ! [FileTypeBox] Is Null, 
          "*", [tbl_Items].[FileTypeID] = [Forms] ! [frm_SearchForm] ! [FileTypeBox]
        )
      )<> False
    ) 
    AND (
      (
        IIf(
          [Forms] ! [frm_SearchForm] ! [FileNameSearchBox] Is Null, 
          "*", 
          (
            [tbl_Items].[FileName] Like "*" & [Forms] ! [frm_SearchForm] ! [FileNameSearchBox] & "*"
          ) 
          Or (
            [tbl_Items].[Description] Like "*" & [Forms] ! [frm_SearchForm] ! [FileNameSearchBox] & "*"
          ) 
          Or (
            [tbl_Items].[FileLocation] Like "*" & [Forms] ! [frm_SearchForm] ! [FileNameSearchBox] & "*"
          )
        )
      )<> False
    )
  );

Any and all help is most appreciated.非常感谢任何和所有帮助。

Combo boxes in MS Access can have hidden fields to hide IDs but display the human readable value but the value still is equal to the hidden ID not displayed value. MS Access 中的组合框可以有隐藏字段来隐藏 ID,但显示人类可读的值,但该值仍然等于隐藏的 ID 未显示值。 Simply set the column width property of combobox to zero and identify its position in bound column.只需将组合框的列宽属性设置为零并确定其在绑定列中的位置。 Below is an example of a two column table/query control source of combobox:下面是组合框的两列表/查询控件源的示例:

BoundColumn: 1
ColumnCount: 2
ColumnWidths: 0; 1;

Therefore, you can query the form controls as is but still show the user the corresponding lookup value (not related ID).因此,您可以按原样查询表单控件,但仍向用户显示相应的查找值(非相关 ID)。 Additionally, consider table aliases for readability and NZ to handle missing controls check in optional querying.此外,考虑表别名的可读性和NZ处理可选查询中缺少的控件检查。

SELECT 
  i.ItemID, 
  i.FileName, 
  i.FileLocation, 
  i.AuthorID, 
  a.AuthorName, 
  i.MediaTypeID, 
  m.MediaType, 
  i.SubjectCategoryID, 
  s.SubjectCategory, 
  i.YearID, 
  y.ActualYear, 
  i.FileTypeID, 
  f.FileType 
FROM 
  tbl_FileType f
  INNER JOIN (
    (
      tbl_Author a
      INNER JOIN (
        tbl_MediaType m
        INNER JOIN (
          tbl_SubjectCategory s
          INNER JOIN tbl_Items ON s.SubjectCategoryID = i.SubjectCategoryID
        ) ON m.MediaTypeID = i.MediaTypeID
      ) ON a.AuthorID = i.AuthorID
    ) 
    INNER JOIN tbl_Year y ON i.YearID = y.YearID
  ) ON f.FileTypeID = i.FileTypeID 
WHERE 
    (
      (i.YearID) BETWEEN [Forms]![frm_SearchForm]![YearSearchBox] 
                     AND [Forms]![frm_SearchForm]![YearSearchBox2]
    ) 
    AND (
      [tbl_Items].[SubjectCategoryID] = NZ([Forms]![frm_SearchForm]![CategorySearchBox], [tbl_Items].[SubjectCategoryID])
    ) 
    AND (
      [tbl_Items].[MediaTypeID] = NZ([Forms]![frm_SearchForm]![MediaTypeSearchBox], [tbl_Items].[MediaTypeID])
    ) 
    AND (
      [tbl_Items].[AuthorID] = NZ([Forms]![frm_SearchForm]![AuthorSearchBox], [tbl_Items].[AuthorID])
    ) 
    AND (
      [tbl_Items].[FileTypeID] = NZ([Forms]![frm_SearchForm]![FileTypeBox], [tbl_Items].[FileTypeID])
    ) 
    AND (
          (      
           [tbl_Items].[FileName] LIKE "*" & NZ([Forms]![frm_SearchForm]![FileNameSearchBox], [tbl_Items].[FileName]) & "*"
          ) 
       OR (
           [tbl_Items].[Description] LIKE "*" & NZ([Forms]![frm_SearchForm]![FileNameSearchBox], [tbl_Items].[Description]) & "*"
          ) 
       OR (
           [tbl_Items].[FileLocation] LIKE "*" & NZ([Forms]![frm_SearchForm]![FileNameSearchBox], [tbl_Items].[FileLocation]) & "*"
          )
    );

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

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