简体   繁体   中英

How to set TextBox Comment Value Input as Table Data Insert on MS Access VBA

I have a main "final table" being updated through VBA based on many inputs of a Form and other tables data. I am trying to callout a TextBox object value that exists in a form to the VBA code so user can add a comment to all the data being inserted into the final table.

Dim strComment As String
strComment = TextBox_Comment.Value

strSQL = "INSERT INTO Main_Table (Period, Monthp, Order, Comment)"
strSQL = strSQL & "SELECT ""Weekly"" AS Period, [001_SelectedMonth].Monthp, [001_OrderTable].Order, strComment AS Comment FROM 001_SelectedMonth, 001_OrderTable;"

CurrentDb.Execute strSQL, dbFailOnError

The code works fine when I remove both the "Comment" from the Main Table insertion points and the "strComment AS Comment" part of the code. Otherwise I get the Run-Time Error '3061', Too few parameters. Expected 1.

Question : is there a way for me to callout the text box value to be inserted in the database as a field data for all the data being added or should I use another method to do this?

Example of the final table:

决赛桌

You need a space and proper concatenation:

strSQL = "INSERT INTO Main_Table ([Period], Monthp, [Order], [Comment]) "
strSQL = strSQL & "SELECT 'Weekly' AS [Period], [001_SelectedMonth].Monthp, [001_OrderTable].Order, '" & strComment & "' AS [Comment] FROM 001_SelectedMonth, 001_OrderTable;"

That said, use parameters to avoid the concatenation.

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