Results 1 to 13 of 13
  1. #1
    mgmercuio is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2020
    Posts
    27

    How to add a profile picture to a form with a command button using VBA

    First thing... I meant to include in the title of this post, how to capture the file path of the picture with the command button and place into a field of a table...as well...(it wouldn't let me edit the title)

    I apologize if this has already been answered however, I am looking for a very easy snippit of code that will allow the user of my database to click a command button on a form to browse to file location on the hard drive, "capture" the full file path to the image and not only place the actual picture in a control image placeholder, called "ctrlImage" on the form called "frmMyProfile", but also populate a field called "ImagePath" inside a table called "tblSellersProfile."

    Some notes of interest...
    • The form was created from the table - tblSellersProfile and named frmMyProfile.
    • I have a "hidden" field on that form called "ImagePath" which is the same field residing in the table.
    • I also have a delete image command button on the profile form so if the user wants to "not show" their picture on their profile, they simply delete it off. This is achieved by some vba code attached to the Delete command button that simply clears the "ImagePath" (sets to Null) inside the tblSellersProfile.
    • I also have a label box called "lblNoImage" which sits under the ctrlImage so when the picture is deleted, the end user will see "NO IMAGE" on his/her pofile.
    • Also, I am not wanting to store images inside the database. I want the actual picture to live in this filepath: c:\users\%username%\Documents\AccountModule\images \profilepictures.


    Can anyone help me out with this? I got to believe it is easy for the forum experts, but I am not a programmer, only a geek that is really good at reverse engineering some Access Database templates. I have a built this database by using examples from others and until this lil bump, I have been pretty successful at stumbling through the vba code to make it all work.

    Regardless, any advice or help is greatly appreciated.

    Thanks!


    -mgm

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Here is something that might help you. First to subs go into your frmMyProfile form code, the rest in a new standard module.

    Cheers,
    Vlad

    Code:
    Private Sub cmdAddImage_Click()
    Dim sOrigFile As String, sFilePath As String
    Dim sFileName as string,sLocalFolder as string
    Dim fDialog As Object 'FileDialog
    Set fDialog = Application.FileDialog(3) 'msoFileDialogFilePicker
     
    'Show the dialog. -1 means success!
    If fDialog.Show = -1 Then
      sOrigFile = fDialog.SelectedItems(1) 'The full path to the file selected by the user
    End If
    
    
    sFileName =SplitLast(sOrigFile)
    
    
    'check if folder exists and create it if not
    sLocalFolder= Environ$("USERPROFILE") & "\Documents\AccountModule\images
    If Len(Dir(sLocalFolder, vbDirectory)) = 0 Then MyMkDir sLocalFolder
     
    sFilePath = sLocalFolder & "\" & sFileName 
    'lets copy the file to the new location
    Call vcCopyFIle(sOrigFile, sFilePath)
    
    
    If Len(Dir(sFilePath)) > 0 Then
        Me.ImagePath = sFilePath 
       
    Else
        Me.ImagePath = Null
    End If
    Me.Dirty = False
    'now lets show the picture
    
    
    If Len(Dir(sFilePath)) > 0 Then    
        Me.ctrlImage.Picture = sFilePath
    Else    
        Me.ctrlImage.Picture = "(none)"
    End If
    
    
    End Sub
    
    
    Private Sub Form_Current()
    On Error Resume Next 'vlad
    if Not IsNull(Me.ImagePath) then
    'still check if file exists
    If Len(Dir(Me.ImagePath)) > 0 Then    
        Me.ctrlImage.Picture = Me.ImagePath
    Else    
        Me.ctrlImage.Picture = "(none)"
    End If
    
    
    Me.Dirty = False 'vlad
    End Sub
    
    
    
    
    '''<<<<<<<<<<<<<<<<<<<<<<<<this goes into a standard module >>>>>>>>>>>>>>>>>
    Option Compare Database
    
    
    Option Explicit
    Const FO_COPY = &H2
    Const FO_DELETE = &H3
    Const FO_MOVE = &H1
    Const FO_RENAME = &H4
    Const FOF_ALLOWUNDO = &H40
    Const FOF_SILENT = &H4
    Const FOF_NOCONFIRMATION = &H10
    Const FOF_RENAMEONCOLLISION = &H8
    Const FOF_NOCONFIRMMKDIR = &H200
    Const FOF_FILESONLY = &H80
    
    
    Private Type SHFILEOPSTRUCT
        hwnd      As LongPtr
        wFunc     As LongPtr
        pFrom     As String
        pTo       As String
        fFlags    As Integer
        fAborted  As Boolean
        hNameMaps As LongPtr
        sProgress As String
    End Type
        
    Private Declare PtrSafe Function SHFileOperation Lib "shell32.dll" _
      Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As LongPtr
    
    
        
    Public Function vcCopyFIle(sSource As String, sDest As String)
    Dim lFileOp  As LongPtr
    Dim lresult  As LongPtr
    Dim lFlags   As Long
    Dim SHFileOp As SHFILEOPSTRUCT
    lFileOp = FO_COPY
    lFlags = lFlags Or FOF_NOCONFIRMATION
    lFlags = lFlags Or FOF_SILENT
    With SHFileOp
        .wFunc = lFileOp
        .pFrom = sSource & vbNullChar & vbNullChar
        .pTo = sDest & vbNullChar & vbNullChar
        .fFlags = lFlags
    End With
    lresult = SHFileOperation(SHFileOp)
    
    
    End Function
    
    
    Public Function StripLast(Path As String) As String
    On Error Resume Next
        Dim x As Long
        Dim Y As Long
        Y = Len(Path) + 1
        x = 1
        x = InStr(x, Path, "\", vbDatabaseCompare)
        Do While x > 0
            Y = x
            x = InStr(x + 1, Path, "\")
        Loop
        StripLast = Right(Path, Len(Path) - Y)
    End Function
    
    
    Public Function vcGetPathFromFullName(strFullName As String) As String
    On Error Resume Next
    vcGetPathFromFullName = Left(strFullName, Len(strFullName) - Len(StripLast(strFullName)))
    End Function
    
    
    Public Sub MyMkDir(sPath As String)
        Dim iStart          As Integer
        Dim aDirs           As Variant
        Dim sCurDir         As String
        Dim i               As Integer
     
        If sPath <> "" Then
            aDirs = Split(sPath, "\")
            If Left(sPath, 2) = "\\" Then
                iStart = 3
            Else
                iStart = 1
            End If
     
            sCurDir = Left(sPath, InStr(iStart, sPath, "\"))
     
            For i = iStart To UBound(aDirs)
                sCurDir = sCurDir & aDirs(i) & "\"
                If Dir(sCurDir, vbDirectory) = vbNullString Then
                    MkDir sCurDir
                End If
            Next i
        End If
    End Sub
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    mgmercuio is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2020
    Posts
    27
    Thank you for the quick response! I appreciate the effort! I have two questions...

    Before the questions...let me explain what I did.

    I created a "Standard Module in VBA as indicated in the screenshot below, and pasted your code into the module.

    Click image for larger version. 

Name:	compile_error3.PNG 
Views:	23 
Size:	90.5 KB 
ID:	43788

    I also pasted your first sub for the cmdAddImage button in the "On Click" Event Procedure property of the cmdAddImage button.

    Finally, I pasted your second sub in the On_Current Event Procedure Property of the form called "frmSellerProfile." (not the frmMyProfile form) I incorrectly identified the wrong form name in my original question... it should be frmSellerProfile not frmMyProfile... sorry about that


    Now....

    1. I am getting the following compile error (I took two screen shots of the same error)
    Click image for larger version. 

Name:	compile_error1.PNG 
Views:	23 
Size:	59.8 KB 
ID:	43786
    Click image for larger version. 

Name:	compile_error2.PNG 
Views:	22 
Size:	56.8 KB 
ID:	43787

    2. The file path I have outlined in red is not the full path the images directory. Is that part of the issue? The full path should be c:\Users\%Username%\Documents\AccountModule\Images \profilepicture. In the snippet of code, the final directory "profilepicture" was left out of the code. Was this intentional? Sorry to be so dense...


    Again, Thank you for the advice!!

    -mgm

  5. #5
    mgmercuio is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2020
    Posts
    27
    Thank you very much! I will use that site to get some snipets of code and see what I can do to make it work! Thanks for the tip!!

  6. #6
    mgmercuio is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2020
    Posts
    27
    Vlad,

    Great news! I have worked out the error messages I was receiving earlier. SO with regards to my first reply to your post...please disregard because I was able to fix them. Also...your code is going to work. However, there is still one small issue that I am sure you will see is easily fixed...

    Look at your code below in my quote on this reply...notice the line in RED.... Is that an error? It appears to be defining the sFileName and I am thinking it should be included in the IF/ELSE statement directly below it...but it is out there on it's own orphaned line. Is that an error? Do I need to do something with that line?

    The really good news is, everything is working and it evens pulls up a browser box to select the image on my hard drive, but when I choose to select the image, the VBA interupts and throws an error stating it can't write the filename. I believe it is directly related to that orphaned statement I referenced above.

    Can you take a look?

    Thanks for everything...you have really helped me here


    Thanks!
    MgM



    Quote Originally Posted by Gicu View Post
    Here is something that might help you. First to subs go into your frmMyProfile form code, the rest in a new standard module.

    Cheers,
    Vlad

    Code:
    Private Sub cmdAddImage_Click()
    Dim sOrigFile As String, sFilePath As String
    Dim sFileName as string,sLocalFolder as string
    Dim fDialog As Object 'FileDialog
    Set fDialog = Application.FileDialog(3) 'msoFileDialogFilePicker
     
    'Show the dialog. -1 means success!
    If fDialog.Show = -1 Then
      sOrigFile = fDialog.SelectedItems(1) 'The full path to the file selected by the user
    End If
    
    
    sFileName =SplitLast(sOrigFile)
    
    
    'check if folder exists and create it if not
    sLocalFolder= Environ$("USERPROFILE") & "\Documents\AccountModule\images
    If Len(Dir(sLocalFolder, vbDirectory)) = 0 Then MyMkDir sLocalFolder
     
    sFilePath = sLocalFolder & "\" & sFileName 
    'lets copy the file to the new location
    Call vcCopyFIle(sOrigFile, sFilePath)
    
    
    If Len(Dir(sFilePath)) > 0 Then
        Me.ImagePath = sFilePath 
       
    Else
        Me.ImagePath = Null
    End If
    Me.Dirty = False
    'now lets show the picture
    
    
    If Len(Dir(sFilePath)) > 0 Then    
        Me.ctrlImage.Picture = sFilePath
    Else    
        Me.ctrlImage.Picture = "(none)"
    End If
    
    
    End Sub
    
    
    Private Sub Form_Current()
    On Error Resume Next 'vlad
    if Not IsNull(Me.ImagePath) then
    'still check if file exists
    If Len(Dir(Me.ImagePath)) > 0 Then    
        Me.ctrlImage.Picture = Me.ImagePath
    Else    
        Me.ctrlImage.Picture = "(none)"
    End If
    
    
    Me.Dirty = False 'vlad
    End Sub
    
    
    
    
    '''<<<<<<<<<<<<<<<<<<<<<<<<this goes into a standard module >>>>>>>>>>>>>>>>>
    Option Compare Database
    
    
    Option Explicit
    Const FO_COPY = &H2
    Const FO_DELETE = &H3
    Const FO_MOVE = &H1
    Const FO_RENAME = &H4
    Const FOF_ALLOWUNDO = &H40
    Const FOF_SILENT = &H4
    Const FOF_NOCONFIRMATION = &H10
    Const FOF_RENAMEONCOLLISION = &H8
    Const FOF_NOCONFIRMMKDIR = &H200
    Const FOF_FILESONLY = &H80
    
    
    Private Type SHFILEOPSTRUCT
        hwnd      As LongPtr
        wFunc     As LongPtr
        pFrom     As String
        pTo       As String
        fFlags    As Integer
        fAborted  As Boolean
        hNameMaps As LongPtr
        sProgress As String
    End Type
        
    Private Declare PtrSafe Function SHFileOperation Lib "shell32.dll" _
      Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As LongPtr
    
    
        
    Public Function vcCopyFIle(sSource As String, sDest As String)
    Dim lFileOp  As LongPtr
    Dim lresult  As LongPtr
    Dim lFlags   As Long
    Dim SHFileOp As SHFILEOPSTRUCT
    lFileOp = FO_COPY
    lFlags = lFlags Or FOF_NOCONFIRMATION
    lFlags = lFlags Or FOF_SILENT
    With SHFileOp
        .wFunc = lFileOp
        .pFrom = sSource & vbNullChar & vbNullChar
        .pTo = sDest & vbNullChar & vbNullChar
        .fFlags = lFlags
    End With
    lresult = SHFileOperation(SHFileOp)
    
    
    End Function
    
    
    Public Function StripLast(Path As String) As String
    On Error Resume Next
        Dim x As Long
        Dim Y As Long
        Y = Len(Path) + 1
        x = 1
        x = InStr(x, Path, "\", vbDatabaseCompare)
        Do While x > 0
            Y = x
            x = InStr(x + 1, Path, "\")
        Loop
        StripLast = Right(Path, Len(Path) - Y)
    End Function
    
    
    Public Function vcGetPathFromFullName(strFullName As String) As String
    On Error Resume Next
    vcGetPathFromFullName = Left(strFullName, Len(strFullName) - Len(StripLast(strFullName)))
    End Function
    
    
    Public Sub MyMkDir(sPath As String)
        Dim iStart          As Integer
        Dim aDirs           As Variant
        Dim sCurDir         As String
        Dim i               As Integer
     
        If sPath <> "" Then
            aDirs = Split(sPath, "\")
            If Left(sPath, 2) = "\\" Then
                iStart = 3
            Else
                iStart = 1
            End If
     
            sCurDir = Left(sPath, InStr(iStart, sPath, "\"))
     
            For i = iStart To UBound(aDirs)
                sCurDir = sCurDir & aDirs(i) & "\"
                If Dir(sCurDir, vbDirectory) = vbNullString Then
                    MkDir sCurDir
                End If
            Next i
        End If
    End Sub

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Hey,

    Great to hear you are getting close. The line in question (sFileName =SplitLast(sOrigFile) is supposed to strip out the file name (like in MyPhoto.jpg) from the full path selected by the user in the browser dialog. Looks like it needs a space between = and SplitLast()?

    It is in the right place as further down we use that variable to reassemble the new path :sFilePath = sLocalFolder & "" & sFileName


    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    mgmercuio is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2020
    Posts
    27
    Quote Originally Posted by Gicu View Post
    Hey,

    Great to hear you are getting close. The line in question (sFileName =SplitLast(sOrigFile) is supposed to strip out the file name (like in MyPhoto.jpg) from the full path selected by the user in the browser dialog. Looks like it needs a space between = and SplitLast()?

    It is in the right place as further down we use that variable to reassemble the new path :sFilePath = sLocalFolder & "" & sFileName

    Cheers,
    Vlad
    Vlad,

    Yep I saw that....I made the correction and added the space...saved the VBA and now I am getting a compile error when I press the cmdAddImage button - "sub is not defined"

    Sorry to be a pain...but I am soooooo close.

    Click image for larger version. 

Name:	compile_error4.PNG 
Views:	19 
Size:	90.7 KB 
ID:	43792

    Once again...many thanks!
    -mgm

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You have SplitLast, the function is StripLast.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Paul, you're awesome, I was typing that in Notepad trying to assemble all the different pieces....

    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  11. #11
    mgmercuio is offline Novice
    Windows 10 Access 2016
    Join Date
    Dec 2020
    Posts
    27
    YES! Vlad and Paul you guys ae freakin' awesome! It works!!! I have been battling this for four days now and it is all working.

    Vlad, to your point...I can't tell you how many times I deleted a comma or added a parenthesis or mispled (heh heh) a word....

    Good catch Paul!

    Thanks to both you guys.

    Cheers!
    mgm



    Quote Originally Posted by Gicu View Post
    Paul, you're awesome, I was typing that in Notepad trying to assemble all the different pieces....

    Cheers,
    Vlad

  12. #12
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Greta () news!
    Happy New Year!

    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help! I can find everybody's spelling goofs but my own.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 12-17-2017, 12:33 PM
  2. Changing Picture on Command Button
    By kdbailey in forum Access
    Replies: 9
    Last Post: 03-30-2017, 06:24 PM
  3. Replies: 6
    Last Post: 12-27-2010, 10:26 PM
  4. Replies: 11
    Last Post: 10-01-2010, 11:12 PM
  5. Replies: 1
    Last Post: 07-27-2010, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums