Adding an image to data - Access VBA (1 Viewer)

vagues0ul

Registered User.
Local time
Yesterday, 18:07
Joined
Sep 13, 2018
Messages
103
i have an access code which is working fine on one database but when i apply that code on other database it gives an error

You Can't Assign Value To This Object

Here is the code i applied on click to a button

Code:
On Error GoTo Err_Handler
    
    Dim strPath As String
    
    ' open 'file open' dialogue and get path to selected file
    strPath = GetFilePath()
    
    If Len(strPath) > 0 Then
        Me.ImagePath = strPath
        Me.ctrlImage.Visible = True
        Me.lblNoImage.Visible = False
    End If
    
    If Len(strPath) < 0 Then
    Me.ctrlImage.Visible = True
    Me.lblNoImage.Visible = False
    
    End If
    
Exit_here:
    Exit Sub
    
Err_Handler:
    MsgBox Err.Description, vbExclamation, "Error"
    Resume Exit_here

how to fix this
 

isladogs

MVP / VIP
Local time
Today, 01:07
Joined
Jan 14, 2017
Messages
18,186
It would help us help you if you said where the error occurs!

It seems likely that ImagePath is a bound control so you can't set its value to strPath. Change it to an unbound control

Also your code has 2 identical If sections!
The second one is never going to happen as Len(strPath) can NEVER be less than zero.
Perhaps you mean this

Code:
On Error GoTo Err_Handler
    
    Dim strPath As String
    
    ' open 'file open' dialogue and get path to selected file
    strPath = GetFilePath()
    
    If Len(strPath) > 0 Then
        Me.ImagePath = strPath
        Me.ctrlImage.Visible = True
        Me.lblNoImage.Visible = False
    Else
        Me.ctrlImage.Visible = False
    End If
    
Exit_here:
    Exit Sub
    
Err_Handler:
    MsgBox Err.Description, vbExclamation, "Error"
    Resume Exit_here
 

vagues0ul

Registered User.
Local time
Yesterday, 18:07
Joined
Sep 13, 2018
Messages
103
but this same code is running on my second database without any errors... you can check the attached db file
 

Attachments

  • Images_Simple.accdb
    544 KB · Views: 49

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 21:07
Joined
May 21, 2018
Messages
8,463
You cannot set a label that has focus to visible = false, likely the error. Change the focus first.
 

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 21:07
Joined
May 21, 2018
Messages
8,463
Imagepath can be bound, not sure what is being suggested. If it is calculated you will get that error.
 

vagues0ul

Registered User.
Local time
Yesterday, 18:07
Joined
Sep 13, 2018
Messages
103
You cannot set a label that has focus to visible = false, likely the error. Change the focus first.

but how come it runs on one db but not other ?:confused::banghead:
 

isladogs

MVP / VIP
Local time
Today, 01:07
Joined
Jan 14, 2017
Messages
18,186
but this same code is running on my second database without any errors... you can check the attached db file

As I said it would help if you stated where exactly the error occurred.
The code in the attached database isn't identical as you don't have the second If section I remarked on. However I agree that the image code works.

The reason I indicated does apply when you try and assign a value to a bound control. Perhaps something different in the non working one.

It would be more useful to see the database that doesn't work

MajP - labels cannot have focus.
I agree that you wouldn't be able to hide a textbox that has focus.
However it wouldn't cause the error quoted by the OP
 

vagues0ul

Registered User.
Local time
Yesterday, 18:07
Joined
Sep 13, 2018
Messages
103
As I said it would help if you stated where exactly the error occurred.
The code in the attached database isn't identical as you don't have the second If section I remarked on. However I agree that the image code works.

The reason I indicated does apply when you try and assign a value to a bound control. Perhaps something different in the non working one.

It would be more useful to see the database that doesn't work

MajP - labels cannot have focus.
I agree that you wouldn't be able to hide a textbox that has focus.
However it wouldn't cause the error quoted by the OP



I have attached the database on which the code is not working. it on the form named Client Details
 

Attachments

  • testdb.accdb
    1.7 MB · Views: 46

moke123

AWF VIP
Local time
Yesterday, 21:07
Joined
Jan 11, 2013
Messages
3,852
imagepath has a control source of Imagepath which is not in the underlying table.

you either need to remove the controlsource or add the field to the underlying table

also the controlsource of the image control should be set to " =[ImagePath]"

edit: I replaced the image control with a new one, so dont know whether your original one was corrupt.
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 21:07
Joined
May 21, 2018
Messages
8,463
Code:
MajP - labels cannot have focus
Sorry I meant the cntrlImage. But I agree that the error would be something different, I believe that error states something like" you cannot hide a control that has focus"
 

moke123

AWF VIP
Local time
Yesterday, 21:07
Joined
Jan 11, 2013
Messages
3,852
Your MainTable does not have a field- ImagePath.
Your control "ImagePath" on the form is bound to the field ImagePath.
The error is "You cant assign a value to this object"
If you add the field to the table or make the control unbound it works fine.
 

Users who are viewing this thread

Top Bottom