+ Reply to Thread
Results 1 to 5 of 5

Moving data from one table to another in access stopped working

Hybrid View

DannyJ Moving data from one table to... 06-14-2017, 09:46 AM
Norie Re: Moving data from one... 06-14-2017, 09:51 AM
DannyJ Thanks. Will try it when i... 06-14-2017, 05:01 PM
DannyJ Re: Moving data from one... 06-15-2017, 04:30 AM
DannyJ Re: Moving data from one... 06-15-2017, 05:02 AM
  1. #1
    Forum Contributor
    Join Date
    08-29-2012
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    469

    Moving data from one table to another in access stopped working

    Hi All,

    I have the following code. I posted about it yesterday and then found a way to get it working. It worked for a few hours and now doesn't want to play ball, hoping one of you can help me please.

    Sub ExportData()
    
    'Declaring the necessary variables.
    Dim cnn As ADODB.Connection 'dim the ADO collection class
    Dim rs As ADODB.Recordset 'dim the ADO recordset class
    Dim dbPath As String
    Dim SQL As String
    Dim var As Range
    Dim ws As Worksheet
    Dim TableName As String
    Dim ArchiveTable As String
    
    
    'add error handling
    On Error GoTo errHandler:
    'Disable screen flickering.
    Application.ScreenUpdating = False
    'clear the values from the worksheet
    'get the path to the database
    dbPath = Sheet5.Range("I3").Value
    'set the search variable
    Set ws = Worksheets("Audit")
    Set var1 = ws.Range("C2")
    TableName = "[" & var1 & " Current" & "]"
    
    'set the search variable
    
    Set cnn = New ADODB.Connection ' Initialise the collection class variable
    'Connection class is equipped with a -method- named Open
    '--4 aguments-- ConnectionString, UserID, Password, Options
    'ConnectionString formula--Key1=Value1;Key2=Value2;Key_n=Value_n;
    cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
    
    ArchiveTable = "[" & var1 & " Archive" & "]"
    SQL = "INSERT INTO" & " " & ArchiveTable & _
          "Select * FROM" & TableName
    
    
    
    Debug.Print SQL
    
    cnn.Execute SQL
    
    SQL = "DELETE FROM" & ArchiveTable
    Debug.Print SQL
    
    cnn.Execute SQL
    
    Set rs = Nothing
    Set cnn = Nothing
    
    Call PushTableToAccess
    
    'Enable the screen.
    Application.ScreenUpdating = True
    'In case of an empty recordset display an error.
    
    'error handler
    On Error GoTo 0
    Exit Sub
    errHandler:
    'clear memory
    
    Set cnn = Nothing
    
    
    End Sub

    The bit of code that doesn't seem to work is the movement from table to table below:

    
    SQL = "INSERT INTO" & " " & ArchiveTable & _
          "Select * FROM" & TableName
    
    
    
    Debug.Print SQL
    
    cnn.Execute SQL
    It show the below in the immediate box

    INSERT INTO [LHR Archive]Select * FROM[LHR Current]

    Any help is greatly appreciated.

    Thanks.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Moving data from one table to another in access stopped working

    You need a space in front of SELECT and FROM
    SQL = "  INSERT INTO  " & ArchiveTable & _
             "  SELECT * FROM  " & TableName
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    08-29-2012
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    469
    Thanks. Will try it when i get to work tomorrow. Such a stuoid mistake :/

  4. #4
    Forum Contributor
    Join Date
    08-29-2012
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    469

    Re: Moving data from one table to another in access stopped working

    This still isn't working, I have tried adding the spaces. Are there any other suggestions please?

  5. #5
    Forum Contributor
    Join Date
    08-29-2012
    Location
    Slough
    MS-Off Ver
    Excel 2007
    Posts
    469

    Re: Moving data from one table to another in access stopped working

    I worked it out, I had changed some of the field names in the current table but not in the Archive table so it couldn't be moved across. Feel rather stupid for that one!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Errors: Microsoft Excel has stopped working - crash in pivot table
    By EvaLina in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 02-12-2014, 09:58 AM
  2. Updating pivot table filters through a macro has stopped working, help please
    By JasperTata in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-21-2012, 09:53 AM
  3. Help - Web data source stopped working with formulas
    By Daniel86 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-03-2012, 04:12 AM
  4. Quick Access Toolbar macros stopped working
    By surfengine in forum Excel General
    Replies: 1
    Last Post: 10-13-2012, 11:02 PM
  5. VB code stopped working involving data import using XML
    By hariimmadi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-09-2011, 06:04 PM
  6. [SOLVED] Remote data query stopped working
    By Mark Scholes in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2005, 05:06 AM
  7. Importing Data from Access stopped working
    By FrankTimJr in forum Excel General
    Replies: 0
    Last Post: 02-14-2005, 10:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1