Load image in a form

erutherford

Active Member
Joined
Dec 19, 2016
Messages
449
Trying to load an image into a form. I thought ListIndex would do the trick. Images are stored in the data table (MyCars) col. "O". The code creates an error "Object error"

Code:
Private Sub cbxdrpdwn_Change()
    Dim lastrow As Long, ws As Worksheet
    Dim ans As String
    ans = Me.cbxdrpdwn.Value
    Set ws = Sheets("MyCars")
    lastrow = ws.Range("D" & Rows.Count).End(xlUp).Row

    '***** Start Code

    For i = 2 To lastrow
        If ws.Cells(i, "D").Value = ans Then
            MsgBox "Confirm" & Me.cbxdrpdwn.Value
            'Change back to white after selection
            Me.cbxdrpdwn.BackColor = vbWhite

            Me.tbxYear = ws.Cells(i, "A").Value
            Me.tbxMake = ws.Cells(i, "B").Value
            Me.tbxModel = ws.Cells(i, "C").Value
            Me.tbxLicense = ws.Cells(i, "E").Value
            Me.tbxColor = ws.Cells(i, "F").Value
            Me.tbxVIN = ws.Cells(i, "G").Value
            Me.tbxpdate = ws.Cells(i, "H").Value
            Me.tbxPAmt = ws.Cells(i, "I").Value
            Me.tbxPAmt = Format(tbxPAmt, "$#,##0.00")
            Me.tbxPMiles = ws.Cells(i, "J").Value
            Me.obYes = ws.Cells(i, "L").Value
            Me.obNo = ws.Cells(i, "M").Value
            Me.Image2.Picture = LoadPicture(MyCars.List(MyCars.ListIndex, 14))
            Me.TextBox2 = ws.Cells(i, "O").Value

            Exit Sub
        End If
    Next i
End Sub
any ideas?
thanks to all
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Can you post your workbook for download ? No confidential information to be included.
 
Upvote 0
Hi,
This is just a guess but see if this update to your code resolves the issue

VBA Code:
Private Sub cbxdrpdwn_Change()
    Dim lastrow     As Long
    Dim ws          As Worksheet
    Dim ans         As String, ImageFullFilePath As String
   
    ans = Me.cbxdrpdwn.Value
    If Len(ans) = 0 Then Exit Sub
   
    Set ws = Sheets("MyCars")
    lastrow = ws.Range("D" & Rows.Count).End(xlUp).Row
   
    'get full filepath (assumes table has header row)
    ImageFullFilePath = ws.Cells(Me.cbxdrpdwn.ListIndex + 2, 15).Text
   
    '***** Start Code
   
    For i = 2 To lastrow
        If ws.Cells(i, "D").Value = ans Then
            MsgBox "Confirm" & Me.cbxdrpdwn.Value
            'Change back to white after selection
            Me.cbxdrpdwn.BackColor = vbWhite
           
            Me.tbxYear = ws.Cells(i, "A").Value
            Me.tbxMake = ws.Cells(i, "B").Value
            Me.tbxModel = ws.Cells(i, "C").Value
            Me.tbxLicense = ws.Cells(i, "E").Value
            Me.tbxColor = ws.Cells(i, "F").Value
            Me.tbxVIN = ws.Cells(i, "G").Value
            Me.tbxpdate = ws.Cells(i, "H").Value
            Me.tbxPAmt = ws.Cells(i, "I").Value
            Me.tbxPAmt = Format(tbxPAmt, "$#,##0.00")
            Me.tbxPMiles = ws.Cells(i, "J").Value
            Me.obYes = ws.Cells(i, "L").Value
            Me.obNo = ws.Cells(i, "M").Value
           
            If Dir(ImageFullFilePath) <> "" Then
                'image file exists load picture
                Me.Image2.Picture = LoadPicture(ImageFullFilePath)
            Else
                'clear
                Me.Image2.Picture = Nothing
            End If
           
            Me.TextBox2 = ws.Cells(i, "O").Value
           
            Exit Sub
        End If
    Next i
   
End Sub

If still not working, as already requested, place copy of your workbook on a file sharing site like dropbox

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,214,571
Messages
6,120,302
Members
448,954
Latest member
EmmeEnne1979

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