Results 1 to 6 of 6
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310

    Append query in VBA Code

    Hi to all


    The following query adds records to the selected table and runs pefectly as an access query:
    Code:
    INSERT INTO tblStorage ( BookTitle, DateRegistered, VolumeNumber, CallNumberNum, CallNumberText, NumberOfPages, YearPublished, BoxNumber, BookNumbering )
    SELECT tblBooks.BookTitle, tblBooks.DateRegistered, tblBooks.VolumeNumber, tblBooks.CallNumberNum, tblBooks.CallNumberText, tblBooks.NumberOfPages, tblBooks.YearPublished, [Forms].[frmBooks].[txtBoxNumber] AS BoxNumber, [Forms].[frmBooks].[txtBookNumbering] AS BookNumbering
    FROM tblBooks;
    The Form frmBooks is open and I added a command button to execute the above query. The code is:
    Code:
    Dim strSQL As String
    strSQL = "INSERT INTO tblStorage ( BookTitle, DateRegistered, VolumeNumber, CallNumberNum, CallNumberText, NumberOfPages, YearPublished, BoxNumber, BookNumbering )" _
                & " SELECT tblBooks.BookTitle, tblBooks.DateRegistered, tblBooks.VolumeNumber, tblBooks.CallNumberNum, tblBooks.CallNumberText, " _
                & "tblBooks.NumberOfPages, tblBooks.YearPublished, [Forms].[frmBooks].[txtBoxNumber] AS BoxNumber, [Forms].[frmBooks].[txtBookNumbering] AS BookNumbering " _
                & "FROM tblBooks"
        
        Debug.Print strSQL
    db.Execute strSQL
    I am receiving the following error:
    Error 3061: Too few parameters. Expected 2

    Any ideas?

    Sincerely
    Khalil

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Concatenate the form control values into your string.
    Debug.Print any sql string before you try and use it.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    if both BoxNumber and BookNumbering are numeric:
    Code:
    strSQL = "INSERT INTO tblStorage ( BookTitle, DateRegistered, VolumeNumber, CallNumberNum, CallNumberText, NumberOfPages, YearPublished, BoxNumber, BookNumbering )" _
                & " SELECT tblBooks.BookTitle, tblBooks.DateRegistered, tblBooks.VolumeNumber, tblBooks.CallNumberNum, tblBooks.CallNumberText, " _
                & "tblBooks.NumberOfPages, tblBooks.YearPublished, " & [Forms].[frmBooks].[txtBoxNumber] & ", " & [Forms].[frmBooks].[txtBookNumbering] & " " _
                & "FROM tblBooks"

  4. #4
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Hi
    Code in post #3 gave the error message that :

    Object doesn't support this property or method

    I used the following code and it works:
    Code:
    Dim strBoxNumber As String
       Dim strBookNumbering As String
       
       strBoxNumber = Forms.frmBooks!txtBoxNumber
       strBookNumbering = Forms.frmBooks!txtBookNumbering
    
    strSQL = "INSERT INTO tblStorage ( BookTitle, DateRegistered, VolumeNumber, CallNumberNum, CallNumberText, NumberOfPages, YearPublished, BoxNumber, BookNumbering )" _
                & " SELECT BookTitle, DateRegistered, VolumeNumber, CallNumberNum, CallNumberText," _
                & " NumberOfPages, YearPublished, '" & strBoxNumber & " ' AS BoxNumber, ' " & strBookNumbering & " ' AS BookNumbering" _
                & " FROM tblBooks "
    Thank you all

    Khalil

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Another way to try is surround the control with Eval()
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    it errors, because your Fields are String, and the SQL I wrote is for Numeric, get it?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Using APPEND Query using vba code
    By Lucky245 in forum Programming
    Replies: 4
    Last Post: 05-16-2019, 05:48 PM
  2. Replies: 11
    Last Post: 08-23-2016, 04:52 PM
  3. Replies: 1
    Last Post: 10-06-2011, 08:37 AM
  4. Replies: 7
    Last Post: 07-21-2011, 01:01 PM
  5. Append query won't append
    By yelkenli in forum Queries
    Replies: 5
    Last Post: 02-12-2010, 11:19 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums