select file, copy file, rename file

Elsw

New member
Local time
Today, 15:17
Joined
Jul 13, 2012
Messages
4
Hi Guys, I am sure this was posted before but as I am a newbie do not know how to search, I will have to ask again.
Would be great if someone can just post me a link to the answer.

I want to add a button on the form to open a dialogue box where I can select files, once selected it should store the file name and copy the file to a specific directory.
This is to add a picture of every member to the database (storing the images in a directory, not in access)

Thank you for your time.
 
There are various file commands available directly in Access.
This function will allow you to open a file picker (with optional file type filters) And return the complete filepath as a string
Code:
Public Function strGetFileFolderName(Optional strInitialDir As String = "", Optional lngType As Long = 4, Optional strPattern As String = "All Files,*.*") As String
'StrInitialDir = where the filedialog starts browsing
'lngType e.g. 'msoFileDialogFilePicker = 3, msoFileDialogFolderPicker =4, msoFileDialogOpen=1,msoFileDialogSaveAs=2

    Dim fDialog As Object
    Dim vFile As Variant, varEntry As Variant


    strGetFileFolderName = ""

    Set fDialog = Application.FileDialog(lngType)

    With fDialog
        .Title = "Browse for "
        Select Case lngType
        Case 1          'msoFileDialogOpen
            .Title = .Title & "File to open"
        Case 2          'msoFileDialogSaveAs
            .Title = .Title & "File to SaveAs"
        Case 3          'msoFileDialogFilePicker
            .Title = .Title & "File"
        Case 4          'msoFileDialogFolderPicker
            .Title = .Title & "Folder"
        End Select

        Select Case strPattern
        Case "Excel"
            strPattern = "MS Excel,*.XLSX; *.XLSM; *.XLS"
        Case "Access"
            strPattern = "MS Access,*.ACCDB"
        Case "PPT"
            strPattern = "MS Powerpoint,*.PPTX; *.PPTM"
        End Select


        If lngType <> 4 Then
            'Reset then add filter patterns separated by tildes (~) where
            '  multiple extensions are separated by semi-colons (;) and the
            '  description is separated from them by a comma (,).
            '  Example strPattern :
            '  "MS Access,*.ACCDB; *.MDB~MS Excel,*.XLSX; *.XLSM; *.XLS"
            Call .Filters.Clear
            For Each varEntry In Split(strPattern, "~")
                Call .Filters.Add(Description:=Split(varEntry, ",")(0), _
                                  Extensions:=Split(varEntry, ",")(1))
            Next varEntry
        End If
        'Set some default settings
        .InitialFileName = strInitialDir
        .AllowMultiSelect = False
        .InitialView = 2        'msoFileDialogViewDetails
        'Only return a value from the FileDialog if not cancelled.
        If .Show Then strGetFileFolderName = .SelectedItems(1)

    End With

ExitHere:
    Exit Function

HandleErrors:
    MsgBox "Error: " & Err.Description & " (" & Err.Number & ")"
    Resume ExitHere
End Function


You can then use the inbuilt Copy, Kill etc functions - and example is here : https://www.devhut.net/2010/09/29/ms-access-vba-copy-a-file/
 
can you use a demo?
 

Attachments

Users who are viewing this thread

Back
Top Bottom