选择使用输入SQL查询从表单字段访问2007字段、表单、SQL

2023-09-08 11:15:51 作者:呆呆ら

我有两个组合框和运行具有以下code选择查询按钮的表单。目前的问题是它没有考虑的组合框作为栏中输入的输入。这是不是可以在SQL解决或我会用VBA建立一个形式?

 选择字段= [表格]![TestInteractForm]![Combo18]字段= [表格]![TestInteractForm]![Combo20]
从TestInteract;
 

解决方案

虽然它可能不是很明显,你的目标是基本相同的 SELECT 子句来标识一个字段。

不幸的是,Access不支持该功能。您的组合框的值,它是一个字段名,将被识别为有效文本字符串,但也没有办法通知访问该字符串应该是PTED为字段名间$ P $。

您可以得到最接近的将是使用开关语句它映射每个字段名称的字符串到相应的字段值。所以,如果的 TestInteract 和 Combo18 的都包括4个领域(的 FLD1 的 FLD2 的 fld3 和 fld4 的),这样​​的事情可能会实际工作...

SELECT     开关(         [表格]![TestInteractForm]![Combo18] ='FLD1',[FLD1]         [表格]![TestInteractForm]![Combo18] ='FLD2',[FLD2]         [表格]![TestInteractForm]![Combo18] ='fld3',[fld3]         [表格]![TestInteractForm]![Combo18] ='fld4',[fld4]     )AS是first_column 从TestInteract; excel表格是否可以访问数据库中的数据,

不过,我不建议您采用这种方法。我想这可能是简单的生成 SELECT 语句在您的命令按钮的单击事件。

昏暗strSelect作为字符串 strSelect =SELECT [&放大器; Me.Combo18.Value和放大器; ]和放大器; vbCrLf&安培; _     从TestInteract;

和再使用的 strSelect 的在你需要它......作为一个窗体的记录,作为数据源的记录,或者作为 .SQL 属性。

I have a form with two combo boxes and a button that runs a select query with the code below. Currently the issue is it doesn't take the input of the combo boxes as the field input. Is this something that can be solved in SQL or would I have to build a form using VBA?

 SELECT Field=[Forms]![TestInteractForm]![Combo18], Field=[Forms]![TestInteractForm]![Combo20]
FROM TestInteract;

解决方案

Although it may not be apparent, your goal is basically the same as using a query parameter in the SELECT clause to identify a field.

Unfortunately, Access does not support that feature. Your combo box value, which is a field name, will be recognized as a valid text string, but there is no way to inform Access that string should be interpreted as a field name.

The closest you can get would be to use a Switch statement which maps each field name string to the corresponding field value. So, if TestInteract and Combo18 both include 4 fields (fld1, fld2, fld3, and fld4) something like this could actually work ...

SELECT
    Switch(
        [Forms]![TestInteractForm]![Combo18]='fld1', [fld1],
        [Forms]![TestInteractForm]![Combo18]='fld2', [fld2],
        [Forms]![TestInteractForm]![Combo18]='fld3', [fld3],
        [Forms]![TestInteractForm]![Combo18]='fld4', [fld4]
    ) AS first_column
FROM TestInteract;

However, I'm not recommending you adopt that method. I think it could be simpler to generate the SELECT statement in your command button's click event.

Dim strSelect As String
strSelect = "SELECT [" & Me.Combo18.Value & "]" & vbCrLf & _
    "FROM TestInteract;"

And then use strSelect where you need it ... as a form's RecordSource, as the data source for a recordset, or as the .SQL property of a saved query.