Vba file transfer help (1 Viewer)

Fikus01

New member
Local time
Today, 15:32
Joined
Aug 17, 2017
Messages
9
Hi everyone. I'm a bit of a newbie to vba and access in general but it is making sense and I've made good progress, I am however stuck on this point.

I'm trying to put a filecopy after something similar to the code I have used below and I was hoping to use the mydrive or mydrivedir to specify the from location being from a USB stick to he computer.

Any help would
Be appreciated and I will keep working on it in the meantime.

Sub Test1()
Dim MyDrive$, MyDriveDir$
MyDrive = "D"
On Error Resume Next
MyDriveDir = Dir(MyDrive & ":", 5)
If MyDriveDir = "" Or IsError(MyDriveDir) Then
MsgBox "No disk is in Drive " & MyDrive
Else
MsgBox "Drive " & MyDrive & " has a disk and is ready!"
End If
Err.Clear
End Sub
 

Fikus01

New member
Local time
Today, 15:32
Joined
Aug 17, 2017
Messages
9
Sorry, just read that back and I've been too brief, this is the full code I am running, probably an organisational nightmare to a pro.

I can successfully get it to work if I have a fixed file from location but as we use 2 machines with drives mapped differently here I added to code to look for a: if d: wasn't present and I'm trying to use that "drive" decision to do the "filecopy" from location

all I get is my error handler telling me there was an error


Private Sub data_backup_Click()

MsgBox "Please ensure USB stick is inserted"

Dim MyDrive$, MyDriveDir$
On Error Resume Next
MyDrive = "D"
MyDriveDir = Dir(MyDrive & "$")
TryAgain:
If MyDriveDir = "" Or IsError(MyDriveDir) Then GoTo NextDriveLetter




MsgBox "Drive " & MyDrive & " has a disk and is ready!"
Err.Clear


On Error GoTo ErrHandler1:
FileCopy " & MyDriveDir & DATA.xls", "F:\DATA.xls"
On Error GoTo ErrHandler2:
FileCopy " & MyDriveDir & DATA_QR.xls", "F:\DATA_QR.xls"


MsgBox "Backup Completed"
Exit Sub
ErrHandler1:
MsgBox "Error Copying DATA.xls"

Resume Next

ErrHandler2:
MsgBox "Error Copying DATA_QR.xls"


Exit Sub


NextDriveLetter:
If MsgBox("Drive " & MyDrive & " was not present, try another location?", vbOKCancel) = vbCancel Then
Exit Sub

Else

MyDrive = "A"
MyDriveDir = Dir(MyDrive & "$\GeneralUSB_sda1")

End If

GoTo TryAgain:

End Sub
 

jdraw

Super Moderator
Staff member
Local time
Today, 18:32
Joined
Jan 23, 2006
Messages
15,378
As your code shows, the actual copying of a file to another location can be done using

Sub Copy_One_File()
FileCopy "C:\Users\Bob\SourceFolder\Test.xls", "C:\Users\Bob\DestFolder\Test.xls"
End Sub

It seems your issue is dealing with different output locations???
Can you tell us more about the Access database, the users, the files etc.
Do you understand ms Access and multi-users?

Have you tried to diagram the logic you are trying to implement?
 

Fikus01

New member
Local time
Today, 15:32
Joined
Aug 17, 2017
Messages
9
our database isn't a multi user, it will only have 1 person at a time, but depending on where we are, the usb stick is mapped to either a:\ or d:\. I thought I would be able to use the top part of the code with the error handler to swap between them, and it works to a point, it wont let me use

&mydrive& instead of a:\ or d:\ at the beginning of the file copy

as the earlier code identifies the location of the usb drive we are getting our data from.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:32
Joined
May 7, 2009
Messages
19,230
here is a simple function that will return (in string) usb drive and capacity of the media.
it will return blank string if there is no media inserted on usb or other device is inserted (ie phone).
Code:
Public Function GetUSBDrives() As String
    Dim retString As String
    Dim strQuery As String
    Dim objWMI, objRef, Item, Property
    Dim i As Integer
    On Error Resume Next
    
    strQuery = "Select DriveType,DeviceID,Size " & _
                    "from Win32_LogicalDisk " & _
                    "Where " & _
                    "Drivetype = '2'"
                    ' & _
                    ''Switch(strpMedia = "CD", 5, strpMedia = "USB", 2, True, 3) & "'"
    
    Set objWMI = GetObject("winmgmts://")
    Set objRef = objWMI.ExecQuery(strQuery)
    For Each Item In objRef
        For Each Property In Item.Properties_
            'Debug.Print Property.Name & ": " & Property.Value
            If Property.Name = "DeviceID" Or Property.Name = "Size" Then
                retString = retString & Property.Value & ";"
            'Else
            '    retString = retString & IIf(Trim(Property.Value & "") = "", _
            '                "(no media)", Trim(Property.Value & "")) & ";"
            End If
        Next
    Next
    GetUSBDrives = retString
End Function

the return string is in format <driveLetter:>;<capacity>
so you have to use split to get the letter of drive available:
Code:
dim i as integer
dim strUsb as string
dim vUsb as variant

strUsb=GetUsbDrives()
If strUsb<>"" Then
	vUsb=Split(strUsb,";")
	For i = 0 To Ubound(vUsb) Step 2
		Debug.Print vUsb(i) 'This is the drive letter
	Next
End If
 

Users who are viewing this thread

Top Bottom