简体   繁体   中英

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. 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.

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 "). 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. Clearly, a visual aid is in order: Media Type Drop-Down Media Type Field once selected

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. 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.

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:

    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. 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). Additionally, consider table aliases for readability and NZ to handle missing controls check in optional querying.

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]) & "*"
          )
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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