PDA

View Full Version : Picture shows in userform with search



Neverine
11-07-2022, 11:12 AM
Hello,

I have to make userform with working search and shows me data from excel.
But still i have problem load img.
For example i write r, all names with "r" are shows me all names with where is "r" after click on name, i want to load picture in the imagebox.


Private Sub ListBox1_Click()
Image2.Picture = LoadPicture(ListBox1.List(ListBox1.ListIndex, 2))
End Sub

Private Sub TextBox1_Change()
Dim i As Long
Me.TextBox1 = Format(StrConv(Me.TextBox1, vbLowerCase))
Me.ListBox1.Clear
Me.ListBox1.AddItem "Name"
Me.ListBox1.List(0, 1) = "Picture Position on hdd"
Me.ListBox1.List(0, 2) = "Name 2"
Me.ListBox1.List(0, 3) = "Code"
Me.ListBox1.List(0, 4) = "EAN"
Me.ListBox1.Selected(0) = True
For i = 2 To List4.Range("A2000").End(xlUp).Row
For X = 1 To Len(List4.Cells(i, 1))
a = Me.TextBox1.TextLength
If LCase(Mid(List4.Cells(i, 1), X, a)) = Me.TextBox1 And Me.TextBox1 <> "" Then
Me.ListBox1.AddItem List4.Cells(i, 1)
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = "0" & List4.Cells(i, 2)
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = "0" & List4.Cells(i, 3)
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = "0" & List4.Cells(i, 4)
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = "0" & List4.Cells(i, 5)
End If
Next X
Next i
End Sub

June7
11-07-2022, 01:34 PM
What happens - error message, wrong result, nothing?

Neverine
11-08-2022, 04:41 AM
Runtime error "53"
File not found
on listbox1 at possition 2 is C:\1.jpg with picture on possition but loading doesnt work for me

June7
11-08-2022, 10:43 AM
Rather explicit error message. Files are located at the root directory?

Neverine
11-11-2022, 03:50 AM
Yes, picture in directory. Maybe problem shoud be, during search, when i start searching at the list box is autoselected first line, it is my listbox1.list 0, x

Aussiebear
11-11-2022, 04:26 AM
What June7 was referring to is that C:\1.jpg as you indicated is in the Root directory of your system. Can you recheck where your 1.jpg actually resides?

Neverine
11-11-2022, 05:36 AM
What June7 was referring to is that C:\1.jpg as you indicated is in the Root directory of your system. Can you recheck where your 1.jpg actually resides?
in excel cell is writen right locacion of image.

Dave
11-11-2022, 06:36 AM
Is "List4" a sheet Name? Dave

Neverine
11-11-2022, 07:33 AM
Is "List4" a sheet Name? Dave
List4 is (name) Sheet1 is name

Dave
11-11-2022, 08:32 AM
Name of what? You need to qualify your reference....

Sheets("List4").Cells(i, 1)
'or
Sheets("Sheet1").Listobjects("List4").Cells(i, 1)
'etc.
Dave

Neverine
11-11-2022, 08:51 AM
Name of what? You need to qualify your reference....

Sheets("List4").Cells(i, 1)
'or
Sheets("Sheet1").Listobjects("List4").Cells(i, 1)
'etc.
Dave

This will copy from excel the directory route and show route in listbox1
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = List4.Cells(i, 6)

When i select in listbox1 (something), listbox will displayed c:\a\1.jpg
in future every subject will have different jpg.

This will take from listbox1 route and display image
Me.Image2.Picture = LoadPicture(Me.ListBox1.ListIndex, 6)

In textboxs work everything fine. But i still have problem load image.

Dave
11-11-2022, 09:03 AM
Again, could you please indicate what "List4" is? A name for what? Dave

Aussiebear
11-11-2022, 01:30 PM
I'm with Dave here. Recently a number of threads have shown a poor coding habit of just naming objects as a running number (Listbox1, Listbox2, Listbox3, Listbox4, List1, List2, List3, List4, Image1, Image2.... etc). For the original creator of the workbook/worksheet this may have some relevance but for others looking at the workbook/worksheet for the first time it becomes confusing.


Name of what? You need to qualify your reference....

Neverine, earlier a query was raised about the location of an image file, to which you stated it was C:\1.jpg, later on in another post, its now stated at C:\a\1.jpg

Neverine
11-14-2022, 11:48 PM
Again, could you please indicate what "List4" is? A name for what? Dave
List4 is worksheet

In worksheet i wrote possition of image on disk, but i still dont know how to load direction from listbox witch contains actual possition on disk.

Dave
11-15-2022, 07:05 AM
Hi Neverine. You didn't really give us much to go on here. I'm going to make some assumptions. On a sheet named "List4" you have file addresses in "A"; file names in "B"; Code numbers in "C"; and EAN in "D" starting on row 2 down. You have a userform, which I named Userform1, which has an image control named Image2, a testbox named Textbox1, and a listbox named Listbox1. What you are trying to achieve is: as you are typing in textbox1, the available files become available in Listbox1 based on Textbox1. As you add more text, the files available become more limited ie if you type "A" all files starting with "A" are listed. When you type "AC", all files starting with "AC" are available etc. etc. Selecting on the file in Listbox1 will display the file in Image2. You will need to change the code to suit your userform name. HTH. Dave

Private Sub ListBox1_Click()If UserForm1.ListBox1.ListIndex <> 0 Then
UserForm1.Image2.Picture = LoadPicture(UserForm1.ListBox1.List(UserForm1.ListBox1.ListIndex, 0))
End If
End Sub


Private Sub TextBox1_Change()
Dim i As Long, X As Integer, a As Integer
UserForm1.TextBox1 = Format(StrConv(UserForm1.TextBox1, vbLowerCase))
UserForm1.ListBox1.Clear
With UserForm1.ListBox1
.ColumnCount = 4
.AddItem
UserForm1.ListBox1.List(0, 0) = "Picture Position on hdd"
UserForm1.ListBox1.List(0, 1) = "Name 2"
UserForm1.ListBox1.List(0, 2) = "Code"
UserForm1.ListBox1.List(0, 3) = "EAN"
For i = 2 To Sheets("List4").Range("A2000").End(xlUp).Row
a = UserForm1.TextBox1.TextLength
For X = 1 To a
If LCase(Mid(Sheets("List4").Cells(i, 2), X, a)) = LCase(UserForm1.TextBox1) And _
UserForm1.TextBox1 <> "" Then
.AddItem
UserForm1.ListBox1.List(ListBox1.ListCount - 1, 0) = Sheets("List4").Cells(i, 1)
UserForm1.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheets("List4").Cells(i, 2)
UserForm1.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheets("List4").Cells(i, 3)
UserForm1.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheets("List4").Cells(i, 4)
End If
Next X
Next i
End With
End Sub