MS Access - Archive Tables then Import new Tables via VBScript if possible

Posted on 2009-02-16
Last Modified: 2012-08-13

I have a very large question that I was wondering if anybody here could help.

We use "Retina" as our network security scanner at work.  It's a fairly decent program that audits any and all vulnerabilities on workstations accross the whole enterprise.  When you run a scan using Retina, it saves the information as a file with an "rtb" extension that can be opened in MS Access to view all the tables, etc.

My question is then, I have an MS Access database that's tracking the mitigation of all the vulnerabilites in our network, up to the point of resolve.  There's a TON of cutting/pasting involved, but what I'd like to do - is create a button on a form, or use a function in access - that does alot of that work for me.

I'd like to have a button that I push that asks where the Retina filename is stored.  It would then backup all the previous data into an archive of sorts, then purge the tables of anything - and then import the new data from the RTB file into the required tables.

The RTB file structure is always the same as far as the tables, etc. are concerned - the only difference between scans is the filename of whatever we give it for that particular day.

Any information you guys can provide would be greatly appreciated on this topic.  Thanks in advance.
Question by:usslindstrom
    LVL 11

    Expert Comment

    SQL Server for Storage

    So far as achiving goes, i'd suggest you look at implementing a sql server of some description to hold all of your data within the one database.  I would recreate the structure of the tables that you need to archive, but put an addition int field in each and map this to a fact table containing the name/path to the source access database.

    If the rtb files are always in the same folder, then i would just write a visual basic script or exe file to find the files that have not yet been imported and then suck the data in.

    Using this method the whole process could be scheduled.

    LVL 5

    Author Comment

    That VBscript or EXE solution would be great.

    Unfortunately - at current I don't have access to a SQL server that I could inject any of the tables into, and M.S. A is my only hope (sounds like Star Wars when I say it that way).

    I'd be content with just having the database archived within Access, as it's never really that much data to begin with (maybe 50 record sets each import) - or maybe even creating a separate mdb that just holds archival info.  **A solution here, could be to just export the relevent tables into a database by themselves and named by the date/time they were backed up...   Really, any solution would be more than exceptable (but the powers that be wouldn't let me stand up a SQL server *even mySQL or SQLExpress* just to store this data)

    I'm not as up to speed on VB as I'd like to be, but if possible, could you point me in the right direction to implement the scripting to be able to pull off the import / export function that you mentioned in your post?

    Thanks for any help you can provide.
    LVL 11

    Assisted Solution

    Sorry for the delay, but here's a starting point.

    Destination table(s)
    a) In your destination database, create the table(s) with the same fieldnames, datatypes etc with the exception of the autonumber field, make it of type long.
    b) Add an autonumber field maybe called iRecordID, set it to primary key
    c) and a field of long type called iFileID.

    File Table
    Create a table called tblFile in the destination database with fields (iFileID - Autonumber, sFileConnectionString string)

    Code snippet below
    This will need some massaging and extending (to loop through all files in a directory using DIR) but will give you the general idea. You will need to save the file as [yourfilename].vbs

    Visit for assistance in constructing connection strings.

    Private Sub CopyTable(ByVal SourceCS,ByVal DestCS,ByVal TableName)
          'Get file id or write connection string to database tblFile
          Dim FileID
          set rsFile = Server.CreateObject("adodb.recordset")
          rsFile.Open("SELECT iFIleID,sFileConnectionString FROM tblFile WHERE sFileConnectionString=" & chr(34) & SourceCS  & Chr(34))
          with rsFile
                if .eof then
                      FileID = 0
                      .fields("sFileConnectionString").value = SourceCS
                     FileID = .fields("iFileID").value
                End If
           end with
          If FileID=0 then
              rsFile.Open("SELECT iFIleID,sFileConnectionString FROM tblFile WHERE sFileConnectionString=" & chr(34) & SourceCS  & Chr(34))
              FileID = rsFile.fields("iFileID").value
          End If 
          set rsFile = nothing
          'Open source and destination objects, and copy data
          set rsSource = Server.CreateObject("adodb.recordset")
          rsSource.Open("SELECT * FROM " & TableName,SourceCS)
          rsDest = Server.CreateObject("adodb.recordset")
          rsDest.Open("SELECT * FROM " & TableName & " WHERE 1=2")
          while rsSource.eof = false
                for each f in rsSource.fields         
                     rsDest.fields( = rsSource.fields(
               rsDest.fields("FileID").value = FileID
         set rsDest = nothing
         set rsSource = nothing
    End Sub
    'Run the sub
    Call CopyTable("YourSourceConnectionString","YourDestConnectionString","tblData1")
    Call CopyTable("YourSourceConnectionString","YourDestConnectionString","tblData2")

    Open in new window

    LVL 11

    Accepted Solution

    line 28 should be rsDest.Open("SELECT * FROM " & TableName & " WHERE 1=2", DestCS)
    LVL 11

    Assisted Solution

    lines 6, 19 should be
    rsFile.Open("SELECT iFIleID,sFileConnectionString FROM tblFile WHERE sFileConnectionString=" & chr(34) & SourceCS  & Chr(34),DestCS)

    Open in new window

    LVL 5

    Author Closing Comment

    So sorry for the late response here.  Sometimes (rather - ALWAYS) - work requirements change from day to day and I sometimes find myself having a hard time coming back and following through on a previous project.

    I was able to successfully implement your code and after a little massaging, get it to do exactly what I needed.  Thank you very much for the solution here, I greatly appreciate it.
    LVL 11

    Expert Comment

    No problems. Glad to hear you've achieved a good outcome.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now