How to copy an already opened file to different location in VB?

Posted on 1999-01-13
Last Modified: 2010-05-03
I have a MS Access file that is already opened but I need to make a backup of this. In Windows Explorer, I can copy the file and paste it any where. Unfortunately, my vb app does not allow to copy. I get error 70 - Permission denied message. Any workaround?
Question by:hoosang

Expert Comment

ID: 1456038
Shell to a command line and try it.  

Someone posted some code here that allowed you to pass keystrokes to the shell prompt.  That should do the trick.

Author Comment

ID: 1456039
I still get 'File Not Found' Error Message.

Condition here is that I have an already opened MDB file here. This is the way I did but not successful.

stmp = "D:\Source\File1.MDB D:\Target\File2.MDB"
vRet = Shell(stmp, 0)


Expert Comment

ID: 1456040
Try folloing:

prompt$ = "Make Backup?"
reply = MsgBox(prompt$, vbOKCancel, dat.Databasename)
If reply = vbOK Then
 FileNM$= InputBox$("Insert name of the new file:")
 If FileNM$ <> "" Then FileCopy dat.DatabaseName, FileNM$
end if
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


Author Comment

ID: 1456041
I tried FileCopy and Shell but neither worked.

Accepted Solution

cantrell earned 50 total points
ID: 1456042
try this (using the windows copy routine)
paste this in a module...
Option Explicit

      Private Const FO_COPY = &H2&   'Copies the files specified in the
                                     'pFrom member to the location
                                     'specified in the pTo member.
      Private Const FO_DELETE = &H3& 'Deletes the files specified in pFrom
                                     '(pTo is ignored.)
      Private Const FO_MOVE = &H1&   'Moves the files specified in pFrom
                                     'to the location specified in pTo.
      Private Const FO_RENAME = &H4& 'Renames the files specified in pFrom
      Private Const FOF_ALLOWUNDO = &H40&   'Preserve Undo information
      Private Const FOF_CONFIRMMOUSE = &H2& 'Not currently implemented.
      Private Const FOF_CREATEPROGRESSDLG = &H0& 'handle to the parent
                                                 'window for the progress
                                                 'dialog box
      Private Const FOF_FILESONLY = &H80&        'Perform the operation
                                                 'on files only if a
                                                 'wildcard file name
                                                 '(*.*) is specified.
      Private Const FOF_MULTIDESTFILES = &H1&    'The pTo member
                                                 'specifies multiple
                                                 'destination files (one
                                                 'for each source file)
                                                 'rather than one directory
                                                 'where all source files
                                                 'are to be deposited.
      Private Const FOF_NOCONFIRMATION = &H10&   'Respond with Yes to All
                                                 'for any dialog box that
                                                 'is displayed.
      Private Const FOF_NOCONFIRMMKDIR = &H200&  'Does not confirm the
                                                 'creation of a new
                                                 'directory if the
                                                 'operation requires one
                                                 'to be created.
      Private Const FOF_RENAMEONCOLLISION = &H8& 'Give the file being
                                                 'operated on a new name
                                                 'in a move, copy, or
                                                 'rename operation if a
                                                 'file with the target
                                                 'name already exists.
      Private Const FOF_SILENT = &H4&            'Does not display a
                                                 'progress dialog box.
      Private Const FOF_SIMPLEPROGRESS = &H100&  'Displays a progress
                                                 'dialog box but does not
                                                 'show the file names.
      Private Const FOF_WANTMAPPINGHANDLE = &H20&
                                   ' If FOF_RENAMEONCOLLISION is specified,
                                   'the hNameMappings member will be filled
                                   'in if any files were renamed.
 ' The SHFILOPSTRUCT is not double word aligned.  If no steps are
 ' taken, the last 3 variables will not be passed correctly.  This
 ' has no impact unless the progress title needs to be changed.
  hwnd As Long
  wFunc As Long
  pFrom As String
  pTo As String
  fFlags As Integer
  fAnyOperationsAborted As Long
  hNameMappings As Long
  lpszProgressTitle As String
 End Type
 Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
 Private Declare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" (lpFileOp As Any) As Long
Public Function ShellCopyFile(Source As String, Dest As String)

Dim lenFileop As Long
Dim foBuf() As Byte

lenFileop = LenB(fileop)    ' double word alignment increase the
ReDim foBuf(1 To lenFileop) ' size of the structure.

'Set the attributes to normal first.
If Len(Dir$(Source)) Then SetAttr Source, vbNormal
If Len(Dir$(Dest)) Then SetAttr Dest, vbNormal

  'Set all of the copy parameters
   With fileop
    .hwnd = Form1.hwnd
    .wFunc = FO_COPY
    'Source file or directory.
    .pFrom = Source & vbNullChar & vbNullChar & vbNullChar
    'Target or Destination directory.
    .pTo = Dest & vbNullChar & vbNullChar
    .lpszProgressTitle = "Copying files to... " & Left$(Dest, Len(Dest) - 1) & vbNullChar & vbNullChar
   End With
    ' Now we need to copy the structure into a byte array
     Call CopyMemory(foBuf(1), fileop, lenFileop)
     ' Next we move the last 12 bytes by 2 to byte align the data
     Call CopyMemory(foBuf(19), foBuf(21), 12)
     If SHFileOperation(foBuf(1)) <> 0 Then    'Operation failed
      MsgBox "FILE COPY OPERATION FAILED! " & Chr$(13) & "ERROR CODE: " & Err.LastDllError, vbCritical Or vbOKOnly
      If fileop.fAnyOperationsAborted <> 0 Then
        MsgBox "FILE COPY Operation Failed", vbCritical Or vbOKOnly
      End If

    End If

End Function

now, use this in your form when you want to copy the file...
shellcopyfile "path and filename of file to copy", "path and filename to copy to"


Expert Comment

ID: 1456043
Each time a file is opened, sharing clauses are specified (or taken from default values) when open is executed. If Access opens the file in such a way it permits no further access (this is, file is not shareable), you have no way to gain access to it.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA loop through headers using value 3 56
DIR issue 7 50
Put text in a picture ASP.NET C# 2 50
How to Add / Edit Windows Menu 4 51
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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

25 Experts available now in Live!

Get 1:1 Help Now