[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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

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?
0
hoosang
Asked:
hoosang
1 Solution
 
obregoruCommented:
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.
0
 
hoosangAuthor Commented:
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)


0
 
subzero011399Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
hoosangAuthor Commented:
I tried FileCopy and Shell but neither worked.
0
 
cantrellCommented:
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.
 Private Type SHFILEOPSTRUCT
  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
Dim fileop As SHFILEOPSTRUCT

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
    .fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION
    .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
     Else
      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"


0
 
vikiingCommented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now