Results 1 to 4 of 4
  1. #1
    gammaman is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Posts
    56

    store query results into table


    I have a table which I create in vba. I want to then run a sql and store the results into the table. Here is my code, it looks right to me but nothing gets inserted into the table.

    Code:
    Set db = CurrentDb()    
    
    On Error Resume Next
    DoCmd.RunSQL ("DROP TABLE [countPrescriptionPlan]")
    DoCmd.RunSQL ("Create TABLE [countPrescriptionPlan]")
    
    Set tb2 = db.TableDefs("countPrescriptionPlan")
    
    
    countColumnsPres = Array("CountofEmplid", "CountofDepBen", "Emplid", "Dep Ben")
     
    
    For Each col In countColumnsPres
       
       
       Set FieldName = tb2.CreateField(col, dbText, 100)
       tb2.Fields.Append FieldName
       Next col
    
    DoCmd.RunSQL ("selectPresPlan")
    DoCmd.RunSQL ("countPresPlan")
    
    insString = "INSERT INTO countPrescriptionPlan (CountofEmplid,CountofDepBen,Emplid,Dep Ben)" _
                      & "Select CountOfEMPLID, CountOFDEPENDENT_BENEF, EMPLID, DEPENDENT_BENEF" _
                      & "From countPresPlan"
    
    DoCmd.RunSQL insString

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Two thoughts right off. First, why delete and recreate the table if it's always the same? Much more efficient to empty it. Second, the inadvisable space in Dep Ben would require it to be bracketed. The SQL is invalid as it is. Third, you need spaces when you have line continuations. Use this to see how the SQL is coming out and test:

    http://www.baldyweb.com/ImmediateWindow.htm

    Okay, that was three thoughts.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2003
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    I would remove the on error resume next. Also you set your "set db=currentdb", but there is no setting it to nothing at the end.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Good point. That line isn't error handling, it's error ignoring. Here are the basics:

    http://www.baldyweb.com/ErrorTrap.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 5
    Last Post: 05-21-2013, 02:21 PM
  2. Replies: 3
    Last Post: 02-05-2013, 07:47 AM
  3. Replies: 1
    Last Post: 06-26-2012, 05:02 PM
  4. list files recursively and store results in array
    By maxbre in forum Programming
    Replies: 2
    Last Post: 11-10-2011, 01:49 AM
  5. Replies: 0
    Last Post: 09-25-2006, 03:42 AM

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