J
Joner
Hello,
I'm having trouble with a little programme of mine where I connect to
an access database. It seems to connect fine, and disconnect fine, but
then after it won't reconnect, I get the error "operation is not
allowed when object is open" so I take out the line of code:
BookDetails.Connection1.Open
and it comes up with the error "operation is not allowed when object
is closed"
I put my hands up to the fact that I'm not an expert at this (as you
may see in the code!!) so I could quite possibly be using the wrong
code!!! But this is becoming quite frustrating now coz I can't seem to
find any help anywhere!!!
The programme is for entering details of books into an Access database
by using a more user friendly GUI created with VB6!
The code below is from two forms one for entering the book details and
one for a main menu. The problem occurs quite often in the programme,
but if I can get some help with this problem then I can use what I've
learnt to correct the other problems!! The example I'm going to give
is for the fill forms button and delete records button.
When you load the programme you get the main menu where you click to
go to the books form then you click the fill forms button and type the
reference number and then the amount of records. This fills all the
refs so you don't have to type them in manually, this works fine! then
when you click the close button and go back to the main menu and click
the delete records button it deletes all the records (up to this point
all is well) then when you go back into the books form and click the
fill refs button again that's when you get the error I stated above!
What am I doing wrong???
The Code:
On frmBooks:
Private Sub cmdFillForms_Click()
On Error GoTo err
MsgBox "PLEASE NOTE: You MUST clear out all previous books to use
this function and the Ref. No. fields should be empty."
Dim CurrentRef, NumberRecords, NumberCount As Integer
NumberCount = 0
CurrentRef = InputBox("Please enter your first reference number.")
If CurrentRef = "" Then
MsgBox "Sorry the number you entered is invalid! Please try
again.", vbExclamation
Exit Sub
Else
NumberRecords = InputBox("Please enter the amount of reference
numbers to be added.")
frmPleaseWait.Show
For i = 0 To 20
DoEvents
Next i
Do While NumberCount <= NumberRecords - 1
BookDetails.rstblBooks.AddNew
txtRef.Text = CurrentRef
NumberCount = NumberCount + 1
CurrentRef = CurrentRef + 1
Loop
BookDetails.rstblBooks.UpdateBatch
Unload frmPleaseWait
BookDetails.rstblBooks.MoveFirst
End If
Exit Sub
err:
MsgBox err.Description
Unload frmPleaseWait
End Sub
On frmMainMenu:
Private Sub cmdClear_Click()
On Error GoTo err
Dim response
Dim rs As Recordset
response = MsgBox("WARNING! There is NO way to undo this action
please make sure that you are sure you want to clear your data!", 4 +
48, "WARNING!")
If response = vbNo Then
Exit Sub
End If
response = MsgBox("FINAL WARNING! You are about to delete all
data!! There is no way to undo this action! Are you sure?", 4 + 48,
"FINAL WARNING!")
If response = vbNo Then
Exit Sub
Else
frmPleaseWait.Show
For i = 0 To 20
DoEvents
Next i
BookDetails.comClear
BookDetails.Connection1.Close
For i = 0 To 20
DoEvents
Next i
'Now run DB compact routine
Dim JRO As JRO.JetEngine
Set JRO = New JRO.JetEngine
JRO.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\IB\IBook.mdb", _
"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\IB\IBook1.mdb;Jet OLEDB:Engine Type=4"
Kill ("c:\IB\IBook.mdb")
Name "c:\IB\IBook1.mdb" As "c:\IB\IBook.mdb"
Set JRO = Nothing
Unload frmPleaseWait
MsgBox ("All data has been deleted!")
BookDetails.Connection1.Open
BookDetails.rstblBooks.Open
End If
Exit Sub
err:
Unload frmPleaseWait
MsgBox err.Description
MsgBox "PLEASE NOTE: YOUR DATABASE HAS NOT BEEN CLEARED!"
End Sub
Any help you can give would be greatly appreciated!!
Thanks,
Ben
I'm having trouble with a little programme of mine where I connect to
an access database. It seems to connect fine, and disconnect fine, but
then after it won't reconnect, I get the error "operation is not
allowed when object is open" so I take out the line of code:
BookDetails.Connection1.Open
and it comes up with the error "operation is not allowed when object
is closed"
I put my hands up to the fact that I'm not an expert at this (as you
may see in the code!!) so I could quite possibly be using the wrong
code!!! But this is becoming quite frustrating now coz I can't seem to
find any help anywhere!!!
The programme is for entering details of books into an Access database
by using a more user friendly GUI created with VB6!
The code below is from two forms one for entering the book details and
one for a main menu. The problem occurs quite often in the programme,
but if I can get some help with this problem then I can use what I've
learnt to correct the other problems!! The example I'm going to give
is for the fill forms button and delete records button.
When you load the programme you get the main menu where you click to
go to the books form then you click the fill forms button and type the
reference number and then the amount of records. This fills all the
refs so you don't have to type them in manually, this works fine! then
when you click the close button and go back to the main menu and click
the delete records button it deletes all the records (up to this point
all is well) then when you go back into the books form and click the
fill refs button again that's when you get the error I stated above!
What am I doing wrong???
The Code:
On frmBooks:
Private Sub cmdFillForms_Click()
On Error GoTo err
MsgBox "PLEASE NOTE: You MUST clear out all previous books to use
this function and the Ref. No. fields should be empty."
Dim CurrentRef, NumberRecords, NumberCount As Integer
NumberCount = 0
CurrentRef = InputBox("Please enter your first reference number.")
If CurrentRef = "" Then
MsgBox "Sorry the number you entered is invalid! Please try
again.", vbExclamation
Exit Sub
Else
NumberRecords = InputBox("Please enter the amount of reference
numbers to be added.")
frmPleaseWait.Show
For i = 0 To 20
DoEvents
Next i
Do While NumberCount <= NumberRecords - 1
BookDetails.rstblBooks.AddNew
txtRef.Text = CurrentRef
NumberCount = NumberCount + 1
CurrentRef = CurrentRef + 1
Loop
BookDetails.rstblBooks.UpdateBatch
Unload frmPleaseWait
BookDetails.rstblBooks.MoveFirst
End If
Exit Sub
err:
MsgBox err.Description
Unload frmPleaseWait
End Sub
On frmMainMenu:
Private Sub cmdClear_Click()
On Error GoTo err
Dim response
Dim rs As Recordset
response = MsgBox("WARNING! There is NO way to undo this action
please make sure that you are sure you want to clear your data!", 4 +
48, "WARNING!")
If response = vbNo Then
Exit Sub
End If
response = MsgBox("FINAL WARNING! You are about to delete all
data!! There is no way to undo this action! Are you sure?", 4 + 48,
"FINAL WARNING!")
If response = vbNo Then
Exit Sub
Else
frmPleaseWait.Show
For i = 0 To 20
DoEvents
Next i
BookDetails.comClear
BookDetails.Connection1.Close
For i = 0 To 20
DoEvents
Next i
'Now run DB compact routine
Dim JRO As JRO.JetEngine
Set JRO = New JRO.JetEngine
JRO.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\IB\IBook.mdb", _
"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\IB\IBook1.mdb;Jet OLEDB:Engine Type=4"
Kill ("c:\IB\IBook.mdb")
Name "c:\IB\IBook1.mdb" As "c:\IB\IBook.mdb"
Set JRO = Nothing
Unload frmPleaseWait
MsgBox ("All data has been deleted!")
BookDetails.Connection1.Open
BookDetails.rstblBooks.Open
End If
Exit Sub
err:
Unload frmPleaseWait
MsgBox err.Description
MsgBox "PLEASE NOTE: YOUR DATABASE HAS NOT BEEN CLEARED!"
End Sub
Any help you can give would be greatly appreciated!!
Thanks,
Ben