Results 1 to 4 of 4
  1. #1
    snoopy2003 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    72

    Select Into VBA

    Hello,



    I am trying to populate an empty table with select query results (query name = 'qry'.

    stSQL = "SELECT * INTO tmpEmpPerType FROM (" & qry & ");"

    Method1: DoCmd.RunSQL stSQL
    Method2: CurrentDb.Execute stSQL, dbFailOnError

    Method 1 works fine and populate the table.
    Method 2 cause an error: "table tmpEmpPerType allready exists"

    What am I doing wrong in method 2 ?
    (I preffer using CurrentDb.Execute)

    Thank you for your help

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    the table already exists. that's what wrong.

    I believe, if you use docmd.runsql instead of currentdb.execute, you won't get that message because the warning message will popup asking if you want to delete the table first.

    consequently, if you want to get rid of this error completely with the ".execute" method, run this code before anything else:

    Code:
    on error resume next
    docmd.DeleteObject acTable, "tablename"
    make sure to set those vba error options to "break in class module", otherwise that code will still break.

  3. #3
    snoopy2003 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    72
    Hello,

    Thank you for your help.

    When I used docmd.runsql I disabled the error messages and so
    I was not aware that it delete the table before insert new data.

    I deleted the table contents before running "select into".
    I thought that delete the table content and adding new data
    runs faster than dropping the table and adding new data.

    What do you think ?

    Anyway, I will use db.execute to drop the table before running select into.

    Thank you for help and support

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    If you have a large dataset dropping the table and recreating it is more efficient and it saves your database some space as it works. If your dataset is small it doesn't really matter which way you choose quite honestly but if the dataset has the potential to become very large (10s of thousands of records) dropping the table then recreating/importing is what you want to do.

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

Similar Threads

  1. Replies: 8
    Last Post: 04-21-2011, 05:29 PM
  2. Replies: 4
    Last Post: 03-17-2011, 06:17 AM
  3. Replies: 1
    Last Post: 10-22-2010, 10:11 AM
  4. select statement
    By jellybeannn in forum Access
    Replies: 5
    Last Post: 08-13-2010, 05:21 AM
  5. Select MAX ( ) +1
    By marco in forum Access
    Replies: 2
    Last Post: 05-05-2010, 03:51 PM

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