Upload Image Challenge

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello Geniuses,
I have this challenge here and I need you all to help me nail it down.
I want to do something cooler with a userform by clicking a button which will open a dialogue box where I choose the picture I wanna upload. I started this and suspended for a while and wanna get buck to it again.
Now here is the actual challenge:
I want to have these buttons : add data, delete data, edit data on the form. I will need also a listbox so that the edit and delete will be possible.
So when I upload the image and send the data to the database, the image should be asigned to the item in a textbox called Reg1 which contains an ID.
So if I want to edit , it should be possible for me to also change the image if I want to and this then should override the old image.
Then when I delete the data too, that image and its ID must be deleted too.
It sounds like impossible to me but I have the feeling there are bigger brains here who can adapt to this for me.
So far, I have this code which I got from @MickG some time ago when I was not really sure what I wanted to achieve. But now the picture is clear, what I want.


Rich (BB code):
Option Explicit
Dim Dic As Object
Private Sub ComboBox1_Change()
 Me.Image1.Picture = LoadPicture(Dic(ComboBox1.Value))
      Image1.PictureSizeMode = fmPictureSizeModeStretch
      ActiveSheet.Image1.Picture = LoadPicture(Dic(ComboBox1.Value))
End Sub
Private Sub CommandButton1_Click()
Dim Fd As Office.FileDialog
Dim txtFileName As String
  Set Fd = Application.FileDialog(msoFileDialogFilePicker)
    With Fd
      .AllowMultiSelect = False
      ' Set the title of the dialog box.
      .Title = "Please select the file."
       ' Clear out the current filters, and add our own.
      .Filters.Clear
      .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
     ' .Filters.Add "Excel 2003", "*.xls"
      '.Filters.Add "All Files", "*.*"


      ' Show the dialog box. If the .Show method returns True, the
      ' user picked at least one file. If the .Show method returns
      ' False, the user clicked Cancel.
      If .Show = True Then
        txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox
        Call AddPth(txtFileName)
      End If
         Me.Image1.Picture = LoadPicture(txtFileName)
            Image1.PictureSizeMode = fmPictureSizeModeStretch
            ActiveSheet.Image1.Picture = LoadPicture(txtFileName)
                
End With
End Sub
Sub AddPth(pth)
Dim TextFile As Integer, Dn As Range, Fd As Boolean, Filecontents As String
Dim FilePath As String, Filecontent As String


'What is the file path and name for the new text file?
  FilePath = ThisWorkbook.Path & "\nDoc.txt"


'Determine the next file number available for use by the FileOpen function
  TextFile = FreeFile
If Not FileExists(FilePath) Then 'new line
   Open FilePath For Output As TextFile
   Print #TextFile , pth
Else
    Open FilePath For Input As TextFile
        Filecontent = Input(LOF(TextFile), TextFile)
    If InStr(Filecontent, pth) = 0 Then
        Close TextFile
        Open FilePath For Append As TextFile
        Print #TextFile , pth
    End If
End If
'Store file content inside a variable
  
Close TextFile
Call Checkpics
End Sub
Sub Checkpics()
Dim Sp As Variant, S As Variant, K As Variant, Temp As String
Dim TextFile As Integer, FilePath As String, Filecontent As String
Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
    FilePath = ThisWorkbook.Path & "\nDoc.txt"
        If Not FileExists(FilePath) Then Exit Sub
'File Path of Text File
  FilePath = ThisWorkbook.Path & "\nDoc.txt"
'Determine the next file number available for use by the FileOpen function
  TextFile = FreeFile
'Open the text file
  Open FilePath For Input As TextFile
'Store file content inside a variable
  Filecontent = Input(LOF(TextFile), TextFile)
  Sp = Split(Filecontent, vbCrLf)
  Dim c As Long
  c = 2000
  For Each S In Sp
        If Not S = "" Then
            c = c + 1
            Dic.Item("WK" & c) = S
            Temp = "WK" & c
        End If
 Next S
 With ComboBox1
    .Clear
    .List = Dic.keys
     Range("A1").Value = Temp
 End With
 
'Delete File
Close TextFile
Kill FilePath
Open FilePath For Output As TextFile
For Each K In Dic.keys
    Print #TextFile , Dic(K)
Next K
Close TextFile
End Sub
Public Function FileExists(ByVal FileName As String) As Boolean
    Dim Attr As Long
    On Error Resume Next
    Attr = GetAttr(FileName)
    FileExists = (Err.Number = 0) And ((Attr And vbDirectory) = 0)
    On Error GoTo 0
End Function
Private Sub Reg1_Change()
Dim n As Long
    With ComboBox1
    For n = 0 To .LineCount - 1
    If .List(n) = Me.Reg1.Value Then .Value = .List(n)
    Next n
    End With
End Sub
Private Sub UserForm_Initialize()
Call Checkpics
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Bump :

Can someone tell me if what I am requesting is valid and possible?

Thanks
 
Upvote 0
Where do you want to 'upload' the image to?
 
Upvote 0
Where do you want to 'upload' the image to?

To that text file nDoc so I can get it display in the image control on the userform when I click the item from the listbox.

Thanks
Kelly
 
Upvote 0
Kelly

I'm confused, you can't store an image in a text file.:eek:
 
Upvote 0
Okay but look at the code I have above there to see if you can understand it.

MickG gave me that some time ago. It did work to upload the image and saved it to that text file called nDoc.

Now I am trying to make it better so when I edit data, I can edit the image too.

I just have the feeling this is possible yet I don't know how

Regards
Kelly
 
Upvote 0
Kelly

You definitely can't 'upload' an image to a text file, are you sure it wasn't just the path to the image that was being stored in the textbox?
 
Upvote 0
Kelly

You definitely can't 'upload' an image to a text file, are you sure it wasn't just the path to the image that was being stored in the textbox?


Oh okay sure that was the path stored in the text file. Yeah missed that step.

So now how do I get the image path overwritten anytime I edit data?

I just hope there is a way
 
Upvote 0
Still want to get this challenge down . Any challenger here?
Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top