Results 1 to 9 of 9

Thread: [02/03] Clean up Access Database

  1. #1

    Thread Starter
    Lively Member Xcoder's Avatar
    Join Date
    Jan 2004
    Posts
    120

    [02/03] Clean up Access Database

    I have an Access database which the user might want to clean up to start fresh from time to time, how can I accomplish this?

    Im working with the Jet engine btw.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] Clean up Access Database

    Do you mean like recreating just the database schema or a compact and repair?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member Xcoder's Avatar
    Join Date
    Jan 2004
    Posts
    120

    Re: [02/03] Clean up Access Database

    Quote Originally Posted by RobDog888
    Do you mean like recreating just the database schema or a compact and repair?
    I guess it would be a clean recreation of the existing database. There is a consecutive number (key) which is incrementing with every interaction, and the user might want to restart that number back to 1.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] Clean up Access Database

    Well if there are records still in the table then you culdnt reset it back to one so I take it would be a blank database with just the table definitions and queries ets. (schema).

    You could use the TransferDatabase method to export a table to another access database.

    VB Code:
    1. 'Using the Access Object Model
    2. 'Repeat for each objecvt to be replicated to the new blank db
    3. 'Change constants to .NET
    4. Application.DoCmd.TransferDatabase acExport, "Microsoft Access", "C:\RobDog888New.mdb", acTable, "Table1", "Table1", True
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member Xcoder's Avatar
    Join Date
    Jan 2004
    Posts
    120

    Re: [02/03] Clean up Access Database

    Quote Originally Posted by RobDog888
    Well if there are records still in the table then you culdnt reset it back to one so I take it would be a blank database with just the table definitions and queries ets. (schema).

    You could use the TransferDatabase method to export a table to another access database.

    VB Code:
    1. 'Using the Access Object Model
    2. 'Repeat for each objecvt to be replicated to the new blank db
    3. 'Change constants to .NET
    4. Application.DoCmd.TransferDatabase acExport, "Microsoft Access", "C:\RobDog888New.mdb", acTable, "Table1", "Table1", True

    Thanks Rob, how do I import the Access Object Model?

    Another question, would I need a clean version of the same file?

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] Clean up Access Database

    What this method does is to copy each object individually one at a time with just the table definition only or if you want you can copy the data too.
    so if you have 5 tables then you would have to make 5 calls passing each table name separately.

    There is another method call that can create the blank empty database file -

    Application.NewCurrentDatabase "C:\RobDog888.mdb"

    But then your current db will be the empty one so your TransferDatabase call would be turned into an Import call.

    Add a reference or use Late Binding if the Access version is or may change.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] Clean up Access Database

    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3. 'Add a reference to MS Access xx.0 Object Library
    4. Imports Microsoft.Office.Interop
    5.  
    6. Public Class Form1
    7.     Inherits System.Windows.Forms.Form
    8.  
    9.     Private moApp As Access.Application
    10.  
    11.     Private Sub frmClients_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12.         moApp = DirectCast(CreateObject("Access.Application"), Access.Application)
    13.     End Sub
    14.  
    15.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    16.         moApp.NewCurrentDatabase "C:\RobDog888New.mdb"
    17.         moApp.DoCmd.TransferDatabase Access.AcDataTransferType.acImport, "Microsoft Access", "C:\RobDog888.mdb", Access.AcDataObjectType.acDataTable, "Table1", "Table1", True
    18.         'More object calls etc.
    19.         '...
    20.     End Sub
    21.  
    22. End Class
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Lively Member Xcoder's Avatar
    Join Date
    Jan 2004
    Posts
    120

    Re: [02/03] Clean up Access Database

    Thanks Rob, I now have a problem with casting the Access.acDataObjectType as Access.acObjectType in the line:

    VB Code:
    1. moApp.DoCmd.TransferDatabase(Access.AcDataTransferType.acImport, "Microsoft Access", "C:\RobDog888.mdb", [B][COLOR=RED]Access.AcDataObjectType.acDataTable[/COLOR][/B], "Table1", "Table1", True)

    What am I missing? Thanks again.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [02/03] Clean up Access Database

    Not sure how that changed but it should be in my post as

    Access.AcObjectType.acTable
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width