简体   繁体   English

在MS Access主窗体中找到一个TempVar,并将其写入连接到该子窗体的表中

[英]Locate a TempVar in a MS Access main form and write it to a table connected to the subform

I need to add some VBA code to a checkbox located on a subform of my front end Access 2016 database. 我需要向前端Access 2016数据库的子窗体上的复选框添加一些VBA代码。

The goal is to insert to a table the value contained in a textbox located in the main form. 目标是将表中包含在主窗体中的文本框中包含的值插入表中。

So far, the steps I took are: 到目前为止,我采取的步骤是:

  1. Click on the checkbox located on the subform; 单击子窗体上的复选框;
  2. Create a temporary variable; 创建一个临时变量;
  3. Insert the temporary variable into the related table; 将临时变量插入相关表;
  4. Move on to the next item. 移至下一项。

The code is the following: 代码如下:

    Option Compare Database

    Private Sub AssegnatoCampione_Click()

        'Create temporary variable and store the selected value
        TempVars("NumeroCampione").Value = Forms!FormCampioni2.SampleNo.Value

        'Write the value in the database
        CurrentDb.Execute " INSERT INTO DB_Offerte2017 " _
                        & "([NumeroCampione]) VALUES " _
                        & "(" & TempVars![NumeroCampione] & ")" ', dbFailOnError
    End Sub

The error I get is: . 我得到的错误是:。

"Too few parameters. Expected 1." “参数太少。预期为1。”

Is there a way to know whether the variable is stored correctly or not? 有没有办法知道变量是否正确存储?

Thank you so much for your help. 非常感谢你的帮助。

Edit 1: 编辑1:

Ok, so i guess that the error was due to DB_Offerte2017([NumeroCampione]) being a text field. 好的,所以我想该错误是由于DB_Offerte2017([NumeroCampione])是文本字段所致。 I corrected the code in this way: 我以这种方式纠正了代码:

CurrentDb.Execute "INSERT INTO DB_Offerte2017 ( NumeroCampione ) VALUES ('" & Me.Parent.SampleNo & "')", dbFailOnError

This way, every time I click on the check box, a new record is added in the table "DB_Offerte2017", under the field "NumeroCampione". 这样,每次我单击复选框时,表“ DB_Offerte2017”中的“ NumeroCampione”字段下都会添加一条新记录。

However, I need to add those records to the same row where the checkboxes is located (eg Click on checkbox where ID = 2438 -> Add "SampleNo" value on DB_Offerte2017.NumeroCampione where ID = 2438). 但是,我需要将这些记录添加到复选框所在的同一行中(例如,单击ID = 2438的复选框->在ID = 2438的DB_Offerte2017.NumeroCampione上添加“ SampleNo”值)。

Is it possible to add a where statement in the code? 是否可以在代码中添加where语句?

Why not just a normal VBA variable or just reference the control? 为什么不只是普通的VBA变量或仅引用控件? Code is behind subform and field/control on main form? 代码在子窗体和主窗体上的字段/控件后面? Try: 尝试:

CurrentDb.Execute "INSERT INTO DB_Offerte2017([NumeroCampione]) VALUES (" & Me.Parent.SampleNo & ")", dbFailOnError . CurrentDb.Execute "INSERT INTO DB_Offerte2017([NumeroCampione]) VALUES (" & Me.Parent.SampleNo & ")", dbFailOnError

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

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