Results 1 to 3 of 3
  1. #1
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402

    Copy data from one table to another

    I Guys I'm having problems trying to get this working

    what I as trying to do is this



    I have a table that holds imported customer details called tblExcelImport I am trying to copy this data into the table tblCustomers when a button is clicked

    the code I am trying to get working is this

    Code:
    DoCmd.SetWarnings False
    
       'Delete existing data in table
       DoCmd.RunSQL "DELETE * FROM tblExcelImport"
       DoCmd.TransferSpreadsheet acImport, 8, "tblExcelImport", selectFile(), True 'select file is the file dialog screen
       
       'now we append these importred customers into the correct table - tblCustomers
       
       Dim db As DAO.Database
       Dim rsTbl1 As DAO.Recordset, rsTbl2 As DAO.Recordset
       Dim strFullName As String, strStreet As String, strTown As String
       Dim strCounty As String, strPostCode As String, strPhoneNumber As String, strMobile As String
       Dim strEMailAddress As String, strWebSite As String
       Dim fld As Field
      
       Set db = CurrentDb()
       Set rsTbl1 = db.OpenRecordset("tblExcelImport", dbOpenDynaset)
       Set rsTbl2 = db.OpenRecordset("tblCustomers", dbOpenDynaset)
       rsTbl1.MoveFirst
       Do Until rsTbl1.EOF
          For Each fld In rsTbl1.Fields
          
             strFullName = fld.Value
             strStreet = fld.Value
             strTown = fld.Value
             strCounty = fld.Value
             strPostCode = fld.Value
             strPhoneNumber = fld.Value
             strMobile = fld.Value
             strEMailAddress = fld.Value
             strWebSite = fld.Value
             
             
             With rsTbl2
               
                !FullName = strFullName
                !Street = strStreet
                !Town = strTown
                !County = strCounty
                !PostCode = strPostCode
                !PhoneNumber = strPhoneNumber
                !Mobile = strMobile
                !EMailAddress = strEMailAddress
                !WebSite = strWebSite
             End With
          Next fld
          rsTbl1.MoveNext
       Loop
    
       rsTbl1.Close
       rsTbl2.Close
       Set rsTbl1 = Nothing
       Set rsTbl2 = Nothing
       Set db = Nothing
       
    ImportCustomersButton_Click_Error:
       MsgBox "Error Has Been Found " & Err.Number & " (" & Err.Description & ") In Procedure (ImportCustomersButton_Click) Of (Sub Form_frmCompanyDetails)"
    when this runs I get this error

    "Error Has been Found 3020 (Update or CancelUpdate without addnew or edit)

    so I amended my vode to this

    Code:
    With rsTbl2
               .Edit
                !FullName = strFullName
                !Street = strStreet
                !Town = strTown
                !County = strCounty
                !PostCode = strPostCode
                !PhoneNumber = strPhoneNumber
                !Mobile = strMobile
                !EMailAddress = strEMailAddress
                !WebSite = strWebSite
                .Update
             End With
          Next fld
          rsTbl1.MoveNext
       Loop
    now when this runs I get "Error Has Been Found 94(invalid use of null)

    how can I change the code so that data held in tblExcelImport is copied into tblCustomers?


    Many thanks for your help

    Steve

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    This is a horrible way to copy data. You don't copy 1 record at a time,
    you run an append query to do it in bulk.
    No code needed.

  3. #3
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi Ranman256

    OMG, how the hell did I miss that lol,
    many thanks

    Steve

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

Similar Threads

  1. Copy data from table in another database
    By jcc285 in forum Import/Export Data
    Replies: 8
    Last Post: 04-07-2016, 05:12 AM
  2. Copy data in table
    By Tommo in forum Access
    Replies: 3
    Last Post: 09-27-2015, 07:23 AM
  3. How to copy data within the same table?
    By price12 in forum Access
    Replies: 1
    Last Post: 04-23-2014, 08:34 PM
  4. Replies: 3
    Last Post: 03-05-2013, 05:14 AM
  5. copy data from one table to another
    By Sureshbabu in forum Access
    Replies: 1
    Last Post: 01-08-2012, 01:27 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