Upload Image Challenge

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,629
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
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,629
Bump :

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

Thanks
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
Where do you want to 'upload' the image to?
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,629
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
Kelly

I'm confused, you can't store an image in a text file.:eek:
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,629
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
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?
 

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,629
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
 

Forum statistics

Threads
1,082,269
Messages
5,364,148
Members
400,783
Latest member
sambills

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top