Results 1 to 7 of 7

Thread: [RESOLVED] Access 2003: Creating new instance of current database

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Resolved [RESOLVED] Access 2003: Creating new instance of current database

    Hi,

    I would like the user to be able to create a new instance of my Access app which would basically replicate the current database and clear the contents of all of the tables. It is similiar to simply doing File -> Save As... then clearing all of the tables. But, for some reason, i can't get the FileDialog object to do this. I have tried coding this with the FileDialog object, but Access crashes on the .Execute method of FD obj. If anyone has any other suggestions about how to do this or how to change my code to get it work, I would LOVE to hear them!! Here's my code:

    VB Code:
    1. Private Sub LblEffect2_Click() ' NOT WORKING!!
    2.     With Application
    3.         With .FileDialog(msoFileDialogSaveAs)
    4.             .InitialFileName = "New AR Tracker.mdb"
    5.             .Title = "Please choose a name for your new AR Tracker database"
    6.             .ButtonName = "Create New"
    7.             .Show
    8.             .Execute
    9.         End With
    10.     End With
    11. End Sub

    Thanks in advance!!

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

    Re: Access 2003: Creating new instance of current database

    Thats because .Execute does not do what you think it does. It basically applies any setting changes you make to the dialog without showing it. If you show it and then dismiss it then the instance is destroyed.

    If you want to create a new blank database tehn try this...

    VB Code:
    1. Application.DoCmd.RunCommand acCmdNewDatabase
    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
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Access 2003: Creating new instance of current database

    Ahh, thanks, RobDog!

    I'm not trying to create a new blank database, but basically I want to recreate the exact database I have (saving all the forms and tables) but with blank tables. Basically, it would be the same as doing a File -> Save As... or doing a File -> New -> From existing file. Any ideas?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Access 2003: Creating new instance of current database

    So I figured out how to delete all the records in my tables. Then I tried to use the command you gave me above (acCmdNewDatabase) and I get a "The Command or Action 'NewDatabase' isn't available now" error. I also tried the acCmdCopyDatabase with the same error. Why am I getting this error?

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

    Re: Access 2003: Creating new instance of current database

    [color=navy]Could be from debugging the code and placing the db in exclusive access mode. Try placing the code behind a form's command button click or such. Make sure you didnt open the db as exclusive and have no objects open in design view or altogether even open to make sure.[/coor]
    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Access 2003: Creating new instance of current database

    I have tried everything and seem to still get this message. Is there a quick way to tell if my DB is in exclusive access mode?

    Could I create another instance of Access and copy all of my tables/forms/modules to that instance or is that just WAY too much work for something so simple? I don't believe what I'm trying to do should be difficult...

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Posts
    131

    Re: Access 2003: Creating new instance of current database

    So I finally figured out how to get what i wanted. I had to create a .bat file to execute a copy and rename command to emulate a "File -> Save As". Then I deleted all the table data in the new copied database. This is probably the hardest way to do this, but it works... so oh, well. Here's the code:

    VB Code:
    1. Private Sub LblEffect2_Click()
    2. Dim tbl As AccessObject
    3. Dim strSQL As String
    4. Dim frm As Form
    5. Dim aApp As Access.Application
    6. Dim newFileFullPath As String
    7. Dim fn As Long, x As Variant
    8. Dim newFilePath As String
    9. Dim newFileName As String
    10.  
    11. On Error GoTo No_Bugs
    12.  
    13. ' show filedialog to get new file path/name
    14.     With Application
    15.         With .FileDialog(msoFileDialogSaveAs)
    16.             .InitialFileName = "New AR Tracker.mde"
    17.             .Title = "Please choose a name for your new AR Tracker database"
    18.             .ButtonName = "Create New"
    19.             .AllowMultiSelect = False
    20.             If .Show = -1 Then
    21.                 newFileFullPath = .SelectedItems(1)
    22.             Else
    23.             End If
    24.         End With
    25.     End With
    26.  
    27. ' get the file path and file name from the filedialog
    28. newFilePath = Left(newFileFullPath, InStrRev(newFileFullPath, "\") - 1)
    29. newFileName = Right(newFileFullPath, Len(newFileFullPath) - InStrRev(newFileFullPath, "\"))
    30.  
    31. ' Create batch file and run it
    32.  fn = FreeFile
    33.  Open "C:\DeleteMe.bat" For Output As fn
    34.  Print #1, "copy " & Chr(34) & Application.CurrentProject.FullName & Chr(34) & _
    35.  " " & Chr(34) & newFilePath & Chr(34) & vbCr
    36.  Print #1, "ren " & Chr(34) & newFilePath & "\" & Application.CurrentProject.Name & _
    37.     Chr(34) & " " & Chr(34) & newFileName & Chr(34) & vbCr
    38.  Close #1
    39.  x = Shell("c:\DeleteMe.bat", vbHide)
    40.  
    41. ' set a reference to the newly created DB and open it
    42.  Set aApp = GetObject(newFileFullPath, "Access.Application")
    43.  
    44. ' delete all the table data in the newly created DB
    45. For Each tbl In aApp.CurrentData.AllTables
    46.     If Left(tbl.Name, 4) <> "MSys" Then
    47.         DoCmd.SetWarnings False
    48.         strSQL = "DELETE * FROM [" & tbl.Name & "]"
    49.         Debug.Print tbl.Name
    50.         DoCmd.RunSQL strSQL
    51.         DoCmd.SetWarnings True
    52.     Else: Debug.Print "EXCLUDED: " & tbl.Name
    53.     End If
    54. Next
    55.  
    56. ' recalculate the visible forms to show updated table data (which is none)
    57. For Each frm In aApp.Forms
    58.     Debug.Print frm.Name
    59.     frm.Recalc
    60. Next
    61.  
    62. Exit_Sub:
    63. Set tbl = Nothing
    64. Set frm = Nothing
    65. Exit Sub
    66.  
    67. No_Bugs:
    68. MsgBox "An error has occurred in this application. " _
    69. & "Please contact your technical support person " _
    70. & "and tell them this information:" _
    71. & vbCrLf & vbCrLf & "Error Number " & Err.Number & ", " & Err.Description, _
    72. Buttons:=vbCritical, Title:="AR Tracker Error"
    73. Resume Exit_Sub:
    74.  
    75. End Sub

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