Results 1 to 10 of 10
  1. #1
    pyrosaxplayer's Avatar
    pyrosaxplayer is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Location
    Corona, CA
    Posts
    5

    Exclamation Need Help With Macro ("Snapshot" A Linked Table)

    Hello,



    I am having issues finding the right information for this. Currently, I am trying to accomplish creating a macro that will take a linked table from a database (Already linked in the .accdb file) | Take the whole data in that table (~40,000 records, 3 fields w/ minimal text) | Then proceed to copy it (Back it up, snapshot it etc..) into a new LOCAL table. I also need it to uniquely name it the date it was captured. This will happen every monday.

    My task is to take that snapshot and bounce it against the previous weeks snapshot to review the changes (data changes, Rows removed, and Rows added).

    I hope that makes sense LOL If you need any more info from me let me know.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Could try a SELECT INTO sql action. http://www.w3schools.com/sql/sql_select_into.asp

    In VBA, something like:

    CurrentDb.Execute "SELECT * INTO ..."
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Here are some links on copying Access tables with VBA:
    http://msdn.microsoft.com/en-us/libr...ffice.15).aspx
    http://www.geeksengine.com/article/d...ess-table.html

    Creating the name is pretty easy too. Here is a variable to calculate the filename based on current date, i.e.
    Code:
    myFileName = Format(Date, "yyyymmdd")

  4. #4
    pyrosaxplayer's Avatar
    pyrosaxplayer is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Location
    Corona, CA
    Posts
    5
    Quote Originally Posted by JoeM View Post
    Here are some links on copying Access tables with VBA:
    http://msdn.microsoft.com/en-us/libr...ffice.15).aspx
    http://www.geeksengine.com/article/d...ess-table.html

    Creating the name is pretty easy too. Here is a variable to calculate the filename based on current date, i.e.
    Code:
    myFileName = Format(Date, "yyyymmdd")

    Thank you for your reply, I am going to use your suggestion.

    But I am having two issues.

    1) Where and how do I set that code that would grab and format the date as the name. It is saving it as 00:00:00.

    2) It saves it as a link and not a local table.

    Code:
    Sub CopyTable()
        
        Dim myFileName As Date
           
        DoCmd.CopyObject , myFileName, acTable, "cdbfi_inventory_vw"
    End Sub
    I appreciate all your help everybody I am new to the VBA.

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    It saves it as a link and not a local table.
    I hadn't thought of that originally, but now it makes sense. Because it is a linked table, if you copy the object, the new object will also be a linked table.
    You'll probably have to use the methodology that June suggested, or the first item in the other link I provided (a "Make Table" query).

    BTW, regarding the file naming issue. The problem with your code was that:
    1. You declared the variable name as a Date (do not do that, choose String)
    2. You never set it equal to anything

    So, your code should start like this (I am changing the name from "myFileName" to "myTableName" because it makes more sense).
    Code:
    Dim myTableName as String
    myTableName=Format(Date,"yyyymmdd")
    ...
    Then you can use myTableName in your create table command.

  6. #6
    pyrosaxplayer's Avatar
    pyrosaxplayer is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Location
    Corona, CA
    Posts
    5
    Quote Originally Posted by JoeM View Post
    BTW, regarding the file naming issue. The problem with your code was that:
    1. You declared the variable name as a Date (do not do, choose String)
    2. You never set it equal to anything

    So, your code should start like this (I am changing the name from "myFileName" to "myTableName" because it makes more sense).
    Code:
    Dim myTableName as String
    myTableName=Format(Date,"yyyymmdd")
    ...
    Then you can use myTableName in your create table command.
    OHHH I see, Thanks again! I will do some more work on it and will post if it is solved or have another issue (CROSSING FINGERS LOL :P )

  7. #7
    pyrosaxplayer's Avatar
    pyrosaxplayer is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Location
    Corona, CA
    Posts
    5
    Alright, everything is working fine EXCEPT! lol

    How do I tell VBA to name the table from the pre-defined variable. Obviously, VB is taking the myTableName as a literal string and not executing it like it should.

    The Date changed worked on the DoCmd VB line but like we had said it creates it linked

    Code:
    Dim myTableName As String
        
        myTableName = Format(Date, "mmddyyyy")
        
        CurrentProject.Connection.Execute "SELECT * INTO myTableName FROM cdbfi_inventory_vw"
    Last edited by pyrosaxplayer; 07-24-2014 at 02:47 PM. Reason: Forgot to add line

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Everything inside of quotes is treated as literal text. So it is not recognizing your variable as a variable. The variable name cannot be enclosed in the quotes. I think it is easier to follow along if you build the SQL string separately before using it (not necessary, but keeps things looking a little cleaner).

    So try this:
    Code:
    Dim myTableName As String
    Dim mySQL as String
        
    myTableName = Format(Date, "mmddyyyy")
    
    mySQL = "SELECT * INTO " & myTableName & " FROM cdbfi_inventory_vw"
        
    CurrentProject.Connection.Execute mySQL

  9. #9
    pyrosaxplayer's Avatar
    pyrosaxplayer is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Location
    Corona, CA
    Posts
    5
    AWESOME! Thank you so much. Goes to show you how much more I have to learn. Issue is all Solved

  10. #10
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Your welcome!

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

Similar Threads

  1. Replies: 5
    Last Post: 02-07-2014, 11:57 AM
  2. UK Date Format In Linked Table ("dd/mm/yyyy")
    By smoothlarryhughes in forum Queries
    Replies: 1
    Last Post: 06-18-2013, 08:29 PM
  3. Linked Table Manager - No "Save PW"
    By dweller569 in forum Import/Export Data
    Replies: 1
    Last Post: 07-30-2011, 07:27 AM
  4. Replies: 1
    Last Post: 04-25-2011, 12:36 PM
  5. Replies: 1
    Last Post: 04-07-2010, 08:49 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