Waiting for Godot..er..copy
Richard Deeming
They also serve who only stand wait. This tip, from reader Richard Deeming, shows another method for allowing a program to stand and wait for a copy operation to complete, and adds some enhancements to the basic idea, which we have discussed in the past, as well.
There is a simple way to copy files and wait for the copy to complete. This method will also (optionally) show the standard Windows file-copy progress dialog box.
Type SHFILEOPSTRUCTStr
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
Enum FO_Func
FO_Move = 1
FO_Copy = 2
FO_Delete = 3
FO_Rename = 4
End Enum
Enum FO_Flags
FOF_MultiDestFiles = &H1
FOF_ConfirmMouse = &H2
FOF_Silent = &H4
FOF_RenameOnCollision = &H8
FOF_NoConfirmation = &H10
FOF_WantMappingHandle = &H20
FOF_AllowUndo = &H40
FOF_FilesOnly = &H80
FOF_SimpleProgress = &H100
FOF_NoConfirmMkDir = &H200
FOF_DeleteFlags = &H154
FOF_CopyFlags = &H3DD
End Enum
Private Declare Function SHFileOperationStr Lib "shell32.dll" "
"Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCTStr) As Long
Function DoFileOp(Operation As FO_Func, sFrom As String, _
Optional sTo As String = vbNullString, _
Optional Flags As FO_Flags, _
Optional SimpleProgressTitle As String = vbNullString, _
Optional hWnd As Long) As Boolean
Dim SHFOS As SHFILEOPSTRUCTStr
With SHFOS
.hWnd = hWnd
.pFrom = From
.pTo = To
.wFunc = Operation
.fFlags = Flags
.lpszProgressTitle = SimpleProgressTitle
End With
SHFileOperationStr SHFOS
If SHFOS.fAnyOperationsAborted Then
DoFileOp = False
Else
DoFileOp = True
End If
End Function
Thus, the function in the tip ("Transfer Downloads Automatically," mailed Nov. 7, 2000) would become:
Public Function importData()
Dim srcFle As String, txtFle As String
Dim tmpFle As String, Rslt As Boolean
Const dwnPth="bdc_serverDL"
On Error Goto impErr
' Set up file path/name variables
tmpFle = Environ("temp")
srcFle = dwnPth & "import.txt"
txtFle = tmpFle & "newdata.txt"
' Copy the file
Rslt = DoFileOp(Operation := FO_Copy, From:= srcFle, _
To := txtFle, Flags := FOF_Silent)
' Import the text file to a database table
DoCmd.TransferText acImportDelim, "NewData import specification", _
"tblImport", txtFle, False
' Delete the text data file
Kill(txtFle)
Exit Sub
impErr:
Msgbox "Error importing data: " & Err.Number & " - " &
Err.Description
End Sub
Thanks, Richard, and for your interest in SearchVB we will be forwarding you a SearchVB denim shirt.
Richard Deeming is a Software development manager for Trinet Ltd. He is from Shoreham-by-Sea, West Sussex, UK.
This was first published in November 2000