2
Answers

Object variable or with block variable not set

Photo of Ramco Ramco

Ramco Ramco

Mar 20
115
1

Hi

  I am getting error - Object variable or with blockvariable not set

picoriginal.Picture = LoadPicture("c:\image.jpg")

Dim picoriginal As PictureBox
Call SaveImageFromAPI(apiUrl, UsrName, Pword, tno, picoriginal)

Public Sub SaveImageFromAPI(apiUrl As String, UserName As String, Pwd As String, TrNo As String, picoriginal As PictureBox)

picoriginal.Picture = LoadPicture("c:\image.jpg")
Thanks
VB.Net

Thanks

Answers (2)

0
Photo of Sangeetha S
253 7.4k 320.1k Mar 21

The error "Object variable or With block variable not set" typically occurs when you try to use an object that hasn't been properly initialized. In your code, the picoriginal object needs to be initialized before you can use it.

Here's how you can modify your code to ensure picoriginal is properly initialized:

Dim picoriginal As PictureBox
Set picoriginal = New PictureBox ' Initialize the PictureBox object

Call SaveImageFromAPI(apiUrl, UsrName, Pword, tno, picoriginal)

Public Sub SaveImageFromAPI(apiUrl As String, UserName As String, Pwd As String, TrNo As String, picoriginal As PictureBox)
    picoriginal.Picture = LoadPicture("c:\image.jpg")
End Sub

In this code:

  • Set picoriginal = New PictureBox initializes the picoriginal object before passing it to the SaveImageFromAPI subroutine.
0
Photo of Sophia Carter
Tech Writer 1.2k 0 Mar 20

The "Object variable or with block variable not set" error typically occurs in Visual Basic when you are trying to access properties or methods of an object that has not been initialized or set to an instance. In your provided code snippet, the issue lies in the `picoriginal As PictureBox` declaration in the beginning, where you declare `picoriginal` as type `PictureBox` but do not assign an actual `PictureBox` object to it before trying to access its properties.

To resolve this error, you need to ensure that `picoriginal` is properly initialized with a `PictureBox` object before accessing its properties like `Picture`. In your specific code, you should create a new instance of `PictureBox` and assign it to `picoriginal` before calling the `SaveImageFromAPI` function. Here's an example of how you can modify your code:


Dim picoriginal As New PictureBox ' Initialize the PictureBox object

' Call the function and pass the initialized PictureBox object
Call SaveImageFromAPI(apiUrl, UsrName, Pword, tno, picoriginal)

' Your SaveImageFromAPI function remains the same
Public Sub SaveImageFromAPI(apiUrl As String, UserName As String, Pwd As String, TrNo As String, picoriginal As PictureBox)
    picoriginal.Picture = LoadPicture("c:\image.jpg")
End Sub

By initializing `picoriginal` as a new `PictureBox`, you ensure that it is no longer a null object, and you should be able to set properties like `Picture` without encountering the "Object variable or with block variable not set" error.

This correction should help you resolve the error you are facing. If you encounter any further issues or have additional questions, feel free to ask!