Results 1 to 3 of 3
  1. #1
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151

    Question Using a Fie SaveAs Dialog Box to save the current DB

    Hi all

    Apologies if this is in the wrong section, but my question didn't seem to fit with the Import/Export Forum.

    The code below is used to create a backup copy of the current DB, in the current DB location. It works nicely and has the desired effect.



    However, I'd like to have a Windows File SaveAs dialog browser open so that the user can select a location in to which they can save the copy of the DB. I'm happy to have the filename be predefined, as in the code below, but am open to the user being able to specify their own file name as well.

    I've played around with various methods and functions, such as Application.FileDialog(msoFileDialogSaveAs), but can't seem to nail anything that works.

    Any thoughts on how this could be achieved would be gratefully received.

    Code:
    Public Sub cmdBackupDB_Click()
    
    Dim Source As String
    Dim Target As String
    Dim retval As Integer
    Dim LResponse As Integer
    
    Source = CurrentDb.Name
    Target = CurrentProject.Path & "\Backup_" & Format(Date, "yy-mm-dd_") & Format(Time, "hhmmss_") & CurrentProject.Name
    
    LResponse = MsgBox("Backup file will be named and written to;" & vbLf & vbLf & Target & vbLf & vbLf & "Do you wish to continue?", vbYesNo, "Continue with Backup?")
    
    If LResponse = vbYes Then
        ' create the backup
        retval = 0
        Dim objFSO As Object
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        retval = objFSO.copyFile(Source, Target, True)
        Set objFSO = Nothing
    
    Else
        Set objFSO = Nothing
        Exit Sub
    End If
    
    LResponse = MsgBox("Backup file successfully created." & vbLf & vbLf & Target & vbLf & vbLf & "Open folder location?", vbYesNo, "Open Backup Location?")
    If LResponse = vbYes Then
        'Opens the folder of the file you just created
        Application.FollowHyperlink CurrentProject.Path
    Else
        Exit Sub
    End If
    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Consider this code that returns folder path as a string:
    Code:
    Dim sFolder As String
    Dim fd As FileDialog
    Dim booResult As Boolean
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    fd.AllowMultiSelect = False
    fd.Title = "Select database folder"
    While booResult = False
        If fd.Show = True Then
            'folder path was selected
            booResult = True
            sFolder = fd.SelectedItems(1)
            Debug.Print sFolder
        End If
    Wend
    

    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
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Thank you June7. Works like a charm.

    This is the new code with your solution implemented.

    Code:
    Public Sub cmdBackupDB_Click()
    Dim stDocName As String
    Dim sFolder As String
    Dim fd As FileDialog
    Dim booResult As Boolean
    Dim Source As String
    Dim Target As String
    Dim retval As Integer
    Dim LResponse As Integer
    
    Source = CurrentDb.Name
    
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    fd.AllowMultiSelect = False
    fd.Title = "Select folder to Backup Database"
    While booResult = False
        If fd.Show = True Then
            'folder path was selected
            booResult = True
            sFolder = fd.SelectedItems(1)
            Target = sFolder & "\Backup_" & Format(Date, "yy-mm-dd_") & Format(Time, "hhmmss_") & CurrentProject.Name
            retval = 0
            Dim objFSO As Object
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            retval = objFSO.copyFile(Source, Target, True)
            Set objFSO = Nothing
            LResponse = MsgBox("Backup file successfully created." & vbLf & vbLf & Target & vbLf & vbLf & "Open folder location?", vbYesNo, "Open Backup Location?")
            If LResponse = vbYes Then
                'Opens the folder of the file you just created
                Application.FollowHyperlink sFolder
                Else
                Exit Sub
            End If
        Else
            Set objFSO = Nothing
            Exit Sub
        
        End If
    Wend
    End Sub

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

Similar Threads

  1. Replies: 2
    Last Post: 08-10-2016, 08:05 PM
  2. Print current record, open printer dialog box
    By Jamesiv1 in forum Access
    Replies: 7
    Last Post: 05-19-2014, 11:15 AM
  3. Saveas Dialog Box With .xlsx As Filetype
    By Manish_05 in forum Import/Export Data
    Replies: 7
    Last Post: 05-07-2014, 12:16 PM
  4. Save As dialog box exporting Excel file
    By accessnewbie in forum Access
    Replies: 1
    Last Post: 03-09-2013, 03:15 PM
  5. Save As Dialog Box for Reports
    By eww in forum Programming
    Replies: 1
    Last Post: 12-01-2010, 03:12 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