How do you make drag & drop possible for pictures?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bobthemoose
    New Member
    • Jan 2014
    • 2

    How do you make drag & drop possible for pictures?

    Brief introduction: I'm pretty comfortable with programming in other languages, but am 100% new to VBA and Access so I welcome all comments.

    I want to be able to plug in my camera and drag a picture from the camera (DCIM folder) into a form and have access move the file to the images folder, rename it according to the names on the form, store the relative path in the form, and show the picture. I am making a directory while learning how to use access.

    Right now I am doing a more basic version where I put the file in the correct 'images' location and then drag it into the hyperlink text field. The program then, "_AfterUpdate() " renames the picture according to the form. The Form (and picture) then update according to the link. My problem is that "Picture_Link_A fterUpdate()" seems to be called before a link is created in the table. Is this a good way to go about this? Is there a better way? What do you suggest? Thank you!

    Here's my code:
    Code:
    Option Compare Database
    
    Private Sub Picture_Link_AfterUpdate()
        Dim oldPath As String, newPath As String, fileName As String
        oldPath = GetFullImagePath
        fileName = Me!Last & "_" & Me!First & ".jpg"
        newPath = GetImagePath & fileName
        If oldPath <> "" Then
            Name oldPath As newPath
            Me!PicturePath = "#\" & fileName
        End If
    End Sub
    
    Private Sub Form_Current()
        Image.Picture = GetFullImagePath
    End Sub
    
    Private Function GetFullImagePath() As String
        Dim ImagePath As String
        ImagePath = ""
        If IsNull(Me!PicturePath) Then
        Else
            ImagePath = GetImagePath & GetFileName(Me!PicturePath)
            If Len([PicturePath]) > 0 And Len(Dir(ImagePath)) > 0 Then
            Else
                ImagePath = ""
            End If
             
        End If
        GetFullImagePath = ImagePath
    End Function
    
    Private Function GetImagePath() As String
         GetImagePath = GetDBPath & "pictures\"
    End Function
    
    Private Function GetDBPath() As String
         GetDBPath = CurrentProject.path & "\"
    End Function
    
    Private Function GetFileName(path As String) As String
        Dim StartPos As Long
        Dim StrLen As Long
        
        StartPos = InStrRev(path, "\") + 1             '+1 is to not include the slash
        StrLen = InStrRev(path, "#") - StartPos        'A link will end in a #
        GetFileName = Mid(path, StartPos, StrLen)
    End Function
    Last edited by bobthemoose; Jan 13 '14, 10:00 AM. Reason: added code
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Unless ADezii, TheSmileyCoder, NeoPa, or Rabbit knows better:
    There's no drag and drop event in the image control.

    TheSmileyCoder might have a trick with the treeview control.

    You might be able to open a file dialog and be able to do the drag and drop within the dialog box, returning the folder and items logging: 32.Select a File or Folder using the FileDialog Object

    Otherwise I think you are out of the normal scope.
    Last edited by zmbd; Jan 14 '14, 06:15 PM.

    Comment

    • bobthemoose
      New Member
      • Jan 2014
      • 2

      #3
      Thanks for the quick reply. I think I will, for the time being, just have the user place the picture in the correct location manually and then drag and drop creating a hyperlink. Maybe I will add code later to move and rename pictures, but this will work for now.

      Thanks!

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        Very well, might I suggest again that you go over the link I posted?

        By opening the file dialog box, the user should be able to do the drag and drop within the dialog, select all of the files and then press OK, you can then use the code given to loop thru and pull the file locations into the database.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          You can Drag-n-Drop Files from any Folder to selective Nodes of a TreeView Control using its OLEDragDrop() Event. The general idea, as well as supporting Code, would be:
          1. Have the TreeView Control active on a Form.
          2. Open Windows Explorer and select a single or multiple Files. Explorer can be opened directly from the Form itself.
            Code:
            Dim varRetVal
            
            varRetVal = Shell("Explorer.exe", vbNormalFocus)
          3. Prior to the Drag-n-Drop Operation, hold down the Shift Key (not necessary - Code Line# 6)).
          4. Check the Format of the Selection to make sure that they are Files (Code Line# 7). If they are not in the desired Format, set the Effect Argument to the desired setting (Code Line# 16) and do not execute.
          5. Loop through the Selected Files (Code Lines 8-14) and add the Base File Name as Child Nodes to the Treeview ActiveX Control (Code Line# 13).
          6. Each Unique Key for each Node will be the Filename's Absolute PATH (Code Lines 12 and 13).
          7. Hope this helps.
            Code:
            Private Sub TreeView1_OLEDragDrop(Data As Object, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
            Dim nodX As Node
            Dim varFileName As Variant
            Dim strKey As String
            
            If Shift = 1 Then       'only if Shift is held, Drag-N-Drop
              If Data.GetFormat(ccCFFiles) Then
                For Each varFileName In Data.Files
                 '1-Relative, tvwChild-Relationship, "g"-Key, "George"-Text
                 'The Unique Key will be the Absolute Path to the File without the Extension
                 'The Displayed Text will be the Absolute Path to the File with the Extension
                 strKey = Left$(varFileName, Len(varFileName) - 4)
                 Set nodX = TreeView1.Nodes.Add(1, tvwChild, strKey, varFileName)
                Next
              Else    'Data is not in the desired Format
                Effect = ccOLEDropEffectNone
              End If
            Else
            End If
            End Sub

          Comment

          Working...