显示名称而不是 ID

发布于 2025-01-10 05:14:42 字数 861 浏览 4 评论 0原文

我目前在组合框中设置了这些选项:

在此处输入图像描述

此框将显示当前公司拥有的根据条件设置的项目 ID。我只是想显示项目的名称,因为人们仅通过 id 是无法知道的。如果我删除项目 ID 的复选框,它将完全删除该字段并且不显示任何内容。我已经调试了companyBox.Value 是什么,它确实是一个公司的数字,如果采用这个id 并将其替换为以下根据图片构建的查询,它会返回我想要的结果。我只是无法获取要在组合框中显示的值。

SELECT projects.projectName
FROM companys INNER JOIN projects ON companys.companyID = projects.companyID
WHERE (((companys.companyID)=7));

给我所有 companyID 为 7 的项目名称

在此处输入图像描述

如下所示,同一查询只给出空格而不是名称:

在此处输入图像描述

I currently have these options set in a combo box:

enter image description here

This box will show the projectIDs that a current company has which is set with the criteria. I just want to show the name of the project as people will not know just by the id. If I remove the check box for the projectID it will just remove that field completly and not show anything. I have debugged what companyBox.Value is and it is indeed a number for a company and if taken this id and replaced it with the following query which is built from the picture and it returns the results I want. I just cant get the values to show in the combo box.

SELECT projects.projectName
FROM companys INNER JOIN projects ON companys.companyID = projects.companyID
WHERE (((companys.companyID)=7));

Gives me all the projectNames where the companyID is 7

enter image description here

And as seen below that same query just gives me blank spaces instead of the names:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2025-01-17 05:14:42

好的,如果您有一个查询,那么如上所述,您可以使用“连接”来拉入另一个表(基于该“projectID”)。

因此,对于一般报告等,那么您使用查询的方法是正确的。

然而!!!

对于组合框?他们内置了此功能和能力。换句话说,您通常不需要加入。

组合框有两部分:

“驱动”或“填充”组合框的 SQL 查询。当然,这可以(并且将会)基于项目表。

非常重要:

我们的组合框是将项目 ID 保存、存储、放置、使用到当前表单的列中。该表单当然基于不同的表(表单当前表)。因此,请记住两个概念:

组合框可以由任何表驱动来显示数据。

组合框将/可以使用“其他”表中的列来保存到当前表单上的当前列中。

当然,现在项目“id”是我们想要的值,但我们肯定不想显示该“项目 id”值,因为正如您所注意到的,人们想要漂亮的文本。

第一条规则:

始终,但始终确保组合框“sql 查询”的第一列是我们想要保存到当前表单中的列,但也是我们要隐藏而不向用户显示的列。

因此,组合框可以轻松隐藏第一列。您可以隐藏其他列,但作为一般规则,我的组合框中很大一部分将有两列。因此,您可能需要公司“id”,但希望将公司名称显示为友好的文本给用户,但为此目的存储/保存/使用公司的“id”。

那么,就你而言?更改列的顺序。

您想要:

ProjectID, ProjectName, and you ALSO can continue to have a filter based on company.

所以,一旦完成上述设置,您将不需要 sql 连接。请记住,组合框有自己的基于表项目的“完整 sql 语句”。

因此,组合框将:

选择项目时保存/存储/使用 ProjectID。

它将显示项目名称,但在幕后它将使用 + 保存 ProjectID。

因此,只需确保将组合框中第一列的长度设置为 0(以隐藏该项目 ID 的显示)。

接下来,确保设置组合框要保存查询中的哪个列值。

该列将是第一列,因此您希望将其设置为 1

您的组合设置将如下所示:

在此处输入图像描述

在上面,控制源是您当前的表单和表格。

因此,我想从酒店表中获取酒店“ID”,但我要将组合框选择的结果保存到名为 Hotel_ID 的列中

,并注意非常小心 - 我设置绑定列 = 1 (即驱动组合框的查询中的第一个值

下一步:

我们想要隐藏第一列值,因此您需要在组合框的属性表的格式选项卡中进行此设置:

在此处输入图像描述

注意上面非常接近。我设置第一列宽度 = 0。这就是隐藏酒店“ID”的方法 - 我只希望用户看到漂亮的酒店名称。(或者在您的情况下)项目 因此

但是,第二列,我有一个酒店名称的宽度(或者在您的情况下是项目名称),

,将组合框查询中的第一列设置为项目 id 的宽度。第一列的= 0。

那么,驱动组合框的查询是基于一个表,但您需要确保要使用的“id”是第一列,并简单地将其隐藏展示。

因此,组合框有一个驱动组合框的 SQL 查询,并且该查询在大多数情况下不会是当前表。

因此,您必须设置两个设置(组合 sql 查询将使用的列 - 但为了避免混淆,然后养成将第一列设为“id”或您想要从该 sql 中获取的值的习惯。

但是,在进行上述设置之后,您仍然必须设置要将该列推入/放入当前表单的列(该设置

实际上是控制源),组合框是一种对其他表的查找,并且它可以。显示漂亮的用户友好文本列,但是仍然使用+存储ID。

所以,您的查询应该看起来更像这样:

SELECT ID, projectName 
FROM projects 
WHERE (((companys.companyID)=7));

我不知道您的Projects表的第一列是否是“ID”(或者是ProjectID),但正如您所看到的,只有组合框需要基于一个表,并且使用我们的新规则 - 我们始终使用查询的第一列我们想要的“id”值

现在,当然上面的内容是公司的“硬编码”,以及您的原始查询。驱动组合框很好 - 只是顺序列显示不正确。

Ok, if you have say a query, then as noted, then you can use a "join" to pull in the other table (based on that "projectID").

So, for a general report etc., then your approach of using the query you have is correct.

HOWEVER!!!

For a combo box? They have this feature and ability built in. In other words you do NOT in general need a join.

The combo box has two parts:

A sql query that "drives" or "fills" the combo box. This can (and will be) of course based on the Projects table.

VERY important:

Our combo box is to save, store, put, use the project ID into a column in our CURRENT form. That form of course is based on a differnt table (the forms current table). So, keep in mind the two concpets:

Combo box can be driven by any table to display data.

Combo box will/can use a column from that "other" table to SAVE into a current column on the current form.

Now of course the Project "id" is the VALUE we want, but we sure as don't want to display that "project id" value, since as you note, humans want the nice looking text.

First Rule:

ALWAYS, but ALWAYS make sure the FIRST column of the combo box "sql query" is the column we WANT to save into the current form, but ALSO the column we are going to HIDE AND NOT show to the user.

So, a combo box can with great ease HIDE the first column. You can hide other columns, but as a general rule a VERY HIGH number of my combo boxs will have two columns. So, you might need company "id", but would want to display company name as nice text to the user, but store/save/use the "id" of company for this purpose.

So, in your case? Change the order of your columns.

You want:

ProjectID, ProjectName, and you ALSO can continue to have a filter based on company.

So, once you get above setup, and you WILL NOT need a sql join. Remember, the combo box has it own "whole sql statement" based on table projects.

So, the combo box will:

Save/store/use the ProjectID when you select a project.

And it will display the project name, but behind the scenes it will use + save ProjectID.

So, just make sure that you set the length of the first column in the combo box to 0 (to hide that projectID from display).

Next, make sure you set WHICH column value from the query the combo box is to save.

That column will be the FIRST column, so you want to set that to 1

Your combo settings will look something like this:

enter image description here

In above, the CONTROL source is your CURRENT form and table.

So, I want to get a Hotel "ID" from the table hotels, but I am going to save the results of the combo box selection INTO a column called Hotel_ID

And note VERY careful - I set the bound column = 1 (that is the FIRST value from the query that drives the combo box.

Next up:

We want to hide the first column value, so you need this setting in format tab of the property sheet for the combo box:

enter image description here

NOTE VERY close in above. I set the FIRST column width = 0. this is HOW you hide the "ID" of hotels - I only want the user to see the nice hotel name. (or in your case Project name).

But, the 2nd column, I have a width for the Hotel name (or in your case project name).

So, make your first column in the query for the combo box the "id" of project id. Set the width of the first column = 0.

So, the query that drives the combo box? It is based on the ONE table, but you need to ensure that the "id" that you going to use is the first column, and simple hide it from display.

So, a combo box has a sql query that drives the combo box, and that query in most cases will NOT be the current table.

So, you have to set BOTH settings (the column the combo sql query will use - but to avoid confusing, then just adopt the habit of making the 1st column the "id" or value you want from that sql.

But, after setting above, you STILL have to set what column to shove/put that column into on the current form. (that setting is the control source).

In effect, a combo box is a kind of look-up into the other table, and it can display nice user friendly text columns, but still use + store the ID.

So, your query should look more like this:

SELECT ID, projectName 
FROM projects 
WHERE (((companys.companyID)=7));

I don't know if your first column of Projects table is "ID" (or is it ProjectID), but as you can see, the combo box ONLY needs to be based on the one table, and with our new rule - we always use the first column of the query the "id" value we want.

Now, of course the above is "hard coded" for company, and your original query that drives the combo box was fine - just that the order of the columns display was incorrect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文