Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Runtime error 91 Object vairable or with block variable not set 1

Status
Not open for further replies.

n1a2v3i3n

Programmer
Jan 19, 2009
18
I am writing a code to input image to a access datebase where in the path of the image is stored in the database.

Below code gives me a runtime error 91 Object variable or with block variable not set.

Private Sub cmdAddImage_Click()
Dim objDB As Database
Dim rsImage As New adodb.Recordset
Dim lOffset As Long
Dim lSize As Long
Dim sPath As String
Dim nHandle As Integer
Dim lSubChunks As Long
Dim Chunk() As Byte
Dim nFragmentOffset As Integer
Dim i As Integer
Dim lChunks As Long
Dim lKey As Long
Dim sSQL As String
'On Error GoTo cmdAddImage_Click_Error

frmFileDialog.Show vbModal

If gsFileName <> "" Then
'lKey = InputBox("Please enter a Number (1..n) as a key to the image")
'i f lKey > 0 Then

'check if unique Key
' sSQL = "select * from images"
' sSQL = sSQL & " where myKey=" & lKey

' Set rsImage = objDB.OpenRecordset(sSQL, dbOpenDynaset)
' If Not rsImage.EOF Then
' MsgBox "Key is already in use"
' GoTo Exit_cmdAddImage_Click
' End If

Set Picture1.Picture = LoadPicture(gsFileName)

Set rsImage = objDB.OpenRecordset("Images", dbOpenDynaset) <------- Error pointing to this line

nHandle = FreeFile
Open gsFileName For Binary Access Read As nHandle

lSize = LOF(nHandle)
If nHandle = 0 Then
Close nHandle
End If

lChunks = lSize \ conChunkSize
nFragmentOffset = lSize Mod conChunkSize

rsImage.AddNew
rsImage("Description") = gsFileName
rsImage("MyKey") = lKey

ReDim Chunk(nFragmentOffset)
Get nHandle, , Chunk()
rsImage("A_Image").AppendChunk Chunk()
ReDim Chunk(conChunkSize)
lOffset = nFragmentOffset
For i = 1 To lChunks
Get nHandle, , Chunk()
rsImage("A_Image").AppendChunk Chunk()
lOffset = lOffset + conChunkSize
txtByteCount = lOffset
DoEvents
Next

rsImage.Update
End If
' End If

Exit_cmdAddImage_Click:

Exit Sub

cmdAddImage_Click_Error:

#If gnDebug Then
Stop
Resume
#End If

HandleError "cmdAddImage_Click", Err.Description, Err.Number, gErrFormName
Resume Exit_cmdAddImage_Click
End Sub


Could any one explain me on what could be the solution for this.

 

You are obviously in over your head here, and it looks like the code was originally copied from another source, and therefore you need to first learn some ADO/DAO basics.
It looks like the code originally called for DAO.

There are two problems here, along with wrongly mixing ADO and DAO. (...As New adodb.Recordset, and the objDB object is DAO)

If you want to use ADO, then you need a connection object instead of the DAO database object, and use an ADO recordset object on it.

If you want to use DAO then:
1. The DAO Database object needs to be opened, which obviously it isn't, and produces the mentioned error:

Set objDB = DBEngine.Workspaces(0).OpenDatabase(...)

2. You need to use a DAO recordset object to get a recordset from a DAO Database object.

Dim rsImage As DAO.Recordset

Otherwise, the next error will be Error 13.



 
Thanks SBerthold.
As you say u have copied this code. I am new to VB Porgramming. But this code works fine on it own and don't get any error. I start getting error only i copied it to my project.

So could you please guide me on where should the changes effect
 
Is there a reason why you are just starting with an out-of-date language? Or is this a homework assignment?

These forums aren't really geared up for beginners so I suggest you start here:

then google for VB6 tutorials



If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
>But this code works fine on it own and don't get any error. I start getting error only i copied it to my project.

That is because you have changed the code under point #2 of my previous post, and because where the code was copied from has the objDB object already opened.

So, make the change I mentioned under point #2, and open the objDB object, using a valid db path and name, prior to using it.
 
Sorry strongm, but much as I love it, I wouldn't recommend it as first choice for an obvious (very) beginner!

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
SBerthold,

I tried it again but still get the same error,
Below is the original code from where i copied. When is copy that again and use it, it fails

Dim rsImage As Recordset
Dim lOffset As Long
Dim lSize As Long
Dim sPath As String
Dim nHandle As Integer
Dim lSubChunks As Long
Dim Chunk() As Byte
Dim nFragmentOffset As Integer
Dim i As Integer
Dim lChunks As Long
Dim lKey As Long
Dim sSQL As String
On Error GoTo cmdAddImage_Click_Error

frmFileDialog.Show vbModal

If gsFileName <> "" Then
lKey = InputBox("Please enter a Number (1..n) as a key to the image")
If lKey > 0 Then

'check if unique Key
sSQL = "select * from images"
sSQL = sSQL & " where myKey=" & lKey

Set rsImage = objDB.OpenRecordset(sSQL, dbOpenDynaset)
If Not rsImage.EOF Then
MsgBox "Key is already in use"
GoTo Exit_cmdAddImage_Click
End If

Set Picture1.Picture = LoadPicture(gsFileName)

Set rsImage = objDB.OpenRecordset("Images", dbOpenDynaset)

nHandle = FreeFile
Open gsFileName For Binary Access Read As nHandle

lSize = LOF(nHandle)
If nHandle = 0 Then
Close nHandle
End If

lChunks = lSize \ conChunkSize
nFragmentOffset = lSize Mod conChunkSize

rsImage.AddNew
rsImage("Description") = gsFileName
rsImage("MyKey") = lKey

ReDim Chunk(nFragmentOffset)
Get nHandle, , Chunk()
rsImage("A_Image").AppendChunk Chunk()
ReDim Chunk(conChunkSize)
lOffset = nFragmentOffset
For i = 1 To lChunks
Get nHandle, , Chunk()
rsImage("A_Image").AppendChunk Chunk()
lOffset = lOffset + conChunkSize
txtByteCount = lOffset
DoEvents
Next

rsImage.Update
End If
End If

Exit_cmdAddImage_Click:

Exit Sub

cmdAddImage_Click_Error:

#If gnDebug Then
Stop
Resume
#End If

HandleError "cmdAddImage_Click", Err.Description, Err.Number, gErrFormName
Resume Exit_cmdAddImage_Click


I still m failing to understand it...
 
You are not studying what I have written.
Have you looked at the links johnwm has posted?

Again, you need to open the objDB database object.
I showed you that in a previous post:

1. The DAO Database object needs to be opened, which obviously it isn't, and produces the mentioned error:

>Set objDB = DBEngine.Workspaces(0).OpenDatabase(...)
and in my next post:
>open the objDB object, using a valid db path and name, prior to using it.

Type in the following line before using the objDB object, and place the cursor on OpenDatabase. Then press F1.
Code:
Set objDB = DBEngine.Workspaces(0).OpenDatabase([b][i]HereTheDBPathAndName[/i][/b])
To make it easier for you, you could also just use this instead:
Code:
Set objDB = DAO.OpenDatabase([b][i]HereTheDBPathAndName[/i][/b])

If you would just to this, first open the database, then you can move on.

I do not know what else to tell you.
And I cannot find a [create-understanding] tag.

 
I tried as you told me as below
Set objDB = DBEngine.Workspaces(0).OpenDatabase("D:\Flash\Backup\Navin VB\Expriment\Vechdetail.mdb")

Now i get another error as Unrecognized database format..
 
On the VB IDE menu, click on "Project" then on "References"

Look for a checked item called "Microsoft DAO 3.51 Object Library"
and remove the Check.

Now, look further down for a reference called
"Microsoft DAO 3.60 Object Library"
and check it.


Also, as I have also already mentioned, change the line
Dim rsImage As Recordset
to
Dim rsImage As DAO.Recordset


Also, you need at least VB6 SP3, and the latest version available, which you should be using, is VB6 SP6.
You can see which Service Pack under the menu item ? and Info (or About)
 
Hi thanks,

Finally with your help i was able to do it thanks a lot.
This was to save image to database. I still remain to retrive image from database if there is any further query will come back
Thanks a lot SBerthold
 
lastly i have picture box. when i load picture in the box the image being large does not fit in. Is there a way to minimize that image in this code it self.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top