Help, How to save picture to specific folder with customized name (1 Viewer)

Louislam

Registered User.
Local time
Today, 03:10
Joined
Feb 7, 2014
Messages
13
Hi,

I have below module to save picture under named folder:
Code:
Option Compare Database

Public Enum acFileType
    acPicture = 1
    acFiles = 2
End Enum

Public Function UploadFile(strDestinationPath As String, _
                            FileType As acFileType, _
                            Optional bIsUpload As Boolean = True) As String
On Error GoTo ErrorHandler
    Dim strSourceFile As String
    Dim strDestinationFile As String
    Dim strFullFileName As String
    Dim FSO As Object
    

    If Right$(strDestinationPath, 1) <> "\" Then
        strDestinationPath = strDestinationPath & "\"
    End If

    Set FSO = CreateObject("Scripting.FileSystemObject")
    If Not FSO.FolderExists(strDestinationPath) Then
        MsgBox "Folder [" & strDestinationPath & "] can not be found", vbCritical, "Message!"
        Set FSO = Nothing
        Exit Function
    End If
    
    With Application.FileDialog(3)
        .title = "Choose File"
        .InitialFileName = ""
        .Filters.Clear
        Select Case FileType
        Case 1
            .Filters.Add "Graphics Files", "*.jpg;*.bmp;*.gif;*.png"
        Case 2
            .Filters.Add "Files", "*.pdf;*.doc;*.docx;*.xls;*.rar"
        Case Else
            .Filters.Add "All Files", "*.*"
        End Select
        .AllowMultiSelect = False
        If .Show Then
            strSourceFile = .SelectedItems.Item(1)
            strDestinationFile = strDestinationPath & Mid$(strSourceFile, InStrRev(strSourceFile, "\") + 1)
            strFullFileName = Mid$(strSourceFile, InStrRev(strSourceFile, "\") + 1)
            
            If bIsUpload = True Then
                If FSO.FileExists(strDestinationFile) = True Then
                    If MsgBox("File already exit, do you want to cover", vbOKCancel + vbInformation, "Confirm!") = vbOK Then
                        FSO.CopyFile strSourceFile, strDestinationFile, True
                    Else
                        Exit Function
                    End If
                Else
                    FSO.CopyFile strSourceFile, strDestinationFile
                End If
                Set FSO = Nothing
                UploadFile = strDestinationFile
            Else
                UploadFile = strSourceFile
            End If
            
        End If
    End With
    
ExitHere:
    Set FSO = Nothing
    Exit Function
    
ErrorHandler:
    MsgBox Err.Description, vbInformation, "Promt"
    Resume ExitHere
End Function

To use it, just put below code in an event:
Code:
Private Sub Change_Click()
  
    Dim strDestinationPath As String
    Dim strReturnFile As String
    
    strDestinationPath = CurrentProject.Path & "\Picture"
    
    If Dir(strDestinationPath, vbDirectory) = "" Then
        MkDir (strDestinationPath)
    Else
    End If
    
    strReturnFile = UploadFile(strDestinationPath & "\", acPicture, True)

    If Len(strReturnFile) > 0 Then
        Me.Sketch = strReturnFile
        Me.imgPicture.Picture = strReturnFile
    End If

End Sub

But how to save the picture with customized name, say "Materials 0001.Jpg"
Thanks
 

MarkK

bit cruncher
Local time
Today, 03:10
Joined
Mar 17, 2004
Messages
8,179
What if you open file dialog in SaveAs mode . . .
Code:
Application.FileDialog([B][COLOR="Red"]2[/COLOR][/B])
Does that solve that problem?
 

Louislam

Registered User.
Local time
Today, 03:10
Joined
Feb 7, 2014
Messages
13
Hi Lagbolt,
It doesn't work, it say the object doesn't apply to such method when i change 3 to 2

By the way, here is a screen cut when i click the button using Application.FileDialog(3) 1.jpg

Still not solve the problem!
 

MarkK

bit cruncher
Local time
Today, 03:10
Joined
Mar 17, 2004
Messages
8,179
Do you want the user to supply the name or do you want to save the file programmatically?
 

Louislam

Registered User.
Local time
Today, 03:10
Joined
Feb 7, 2014
Messages
13
Hi lagbolt, User will have no chance to name the picture, it will save programmatically with related unique number like "0001" and so on
 

MarkK

bit cruncher
Local time
Today, 03:10
Joined
Mar 17, 2004
Messages
8,179
So what is wrong exactly? You have posted code. What part of that code fails? Why do you open a file dialog file picker if you name and save the file programmatically? I don't understand the problem. :confused:

More info: Application.FileDialog(3) is a file picker dialog. Take a look at the object browser here in the uploaded .jpg. See how 3, msoFileDialogFilePicker opens a file picker? If you want to save a file programmatically, this step doesn't make sense.
 

Attachments

  • ss_FileDialogObject.jpg
    ss_FileDialogObject.jpg
    34.2 KB · Views: 142

Louislam

Registered User.
Local time
Today, 03:10
Joined
Feb 7, 2014
Messages
13
Hi lagbolt, I have made a demo for you review,
My logic is: first, user will choose a picture, second, the vba will save the chosen picture as a copy to the stipulated folder with related unique number "Materials 0000" as name View attachment Save Picture.accdb , Help to see what problem with this, many thanks
 

MarkK

bit cruncher
Local time
Today, 03:10
Joined
Mar 17, 2004
Messages
8,179
Is that better? It seems that form was corrupt. Was that the problem you were having?
 

Attachments

  • Save Picture.accdb
    496 KB · Views: 147

Users who are viewing this thread

Top Bottom