简体   繁体   中英

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.

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

Is there a way to know whether the variable is stored correctly or not?

Thank you so much for your help.

Edit 1:

Ok, so i guess that the error was due to DB_Offerte2017([NumeroCampione]) being a text field. 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".

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

Is it possible to add a where statement in the code?

Why not just a normal VBA variable or just reference the control? Code is behind subform and field/control on main form? Try:

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

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