Load image in a form

erutherford

Active Member
Joined
Dec 19, 2016
Messages
402
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
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
3,951
Can you post your workbook for download ? No confidential information to be included.
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,477
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

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
 
Solution

erutherford

Active Member
Joined
Dec 19, 2016
Messages
402
Works perfect! I also see where my problems were in creating this. Many thanks Dave!
 

Watch MrExcel Video

Forum statistics

Threads
1,127,103
Messages
5,622,742
Members
415,925
Latest member
Ryle23

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
Top