在VB6中连接和断开Access数据库时出现问题!!请帮忙!! [英] Problems When Connecting and Disconnecting to an Access Database in VB6!! HELP PLEASE!!

查看:122
本文介绍了在VB6中连接和断开Access数据库时出现问题!!请帮忙!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我在使用我的小程序时遇到了麻烦,我连接到了一个访问数据库。它似乎连接正常,断开连接很好,但

然后它不再重新连接,我得到错误操作不是

当对象打开时 ;所以我拿出代码行:


BookDetails.Connection1.Open


它出现错误操作不是当对象

被关闭时允许


我举手说明我不是这方面的专家(因为你
可能会在代码中看到!!)所以我很可能会使用错误的

代码!但现在这变得相当令人沮丧,因为我似乎无法在任何地方找到任何帮助!


该程序用于输入书籍的详细信息通过使用用VB6创建的更加用户友好的GUI,进入Access数据库




以下代码来自两个表格,用于输入书籍详细信息和

一个主菜单。这个问题在程序中经常发生,

但是如果我能从这个问题得到一些帮助那么我可以用我学到的b $ b来解决其他问题!我要给

的例子是填写表格按钮和删除记录按钮。


当你加载程序时你会得到主菜单如果你点击

转到书籍表格然后你点击填写表格按钮并输入

参考编号,然后输入记录数量。这填补了所有

refs所以你不必手动输入它们,这很好用!然后

当你点击关闭按钮并返回主菜单然后点击

删除记录按钮它会删除所有记录(到目前为止

一切都很好)然后当你回到书籍表格并再次点击

填充参考按钮时,当你收到我上面提到的错误时!

我做错了什么???


代码:


在frmBooks:


Private Sub cmdFillForms_Click()

错误GoTo错误

MsgBox请注意:您必须清除所有以前使用的书籍

这个功能和参考。编号字段应为空。


Dim CurrentRef,NumberRecords,NumberCount As Integer

NumberCount = 0

CurrentRef = InputBox(请输入您的第一个参考编号。)

如果CurrentRef =""然后

MsgBox"抱歉,您输入的号码无效!请再次尝试

。",vbExclamation

退出Sub

否则

NumberRecords = InputBox(" Please输入参考金额

要添加的数字。)


frmPleaseWait.Show

对于i = 0到20

DoEvents

下一个我

Do NumberCount< = NumberRecords - 1


BookDetails。 rstblBooks.AddNew

txtRef.Text = CurrentRef

NumberCount = NumberCount + 1

CurrentRef = CurrentRef + 1

循环

BookDetails.rstblBooks.UpdateBatch

卸载frmPleaseWait

BookDetails.rstblBooks.MoveFirst

结束如果

退出Sub


错误:

MsgBox err.Description

卸载frmPleaseWait

结束子


关于frmMainMenu:


私人子cmdClear_Click()

错误GoTo错误

昏暗的回应

Dim rs As Recordset


Respons e = MsgBox(警告!没有办法撤消此行动

请确保您确定要清除数据!,4 +

48,警告! ;)

如果响应= vbNo那么

退出Sub

结束如果


response = MsgBox (最终警告!您即将删除所有

数据!无法撤消此操作!您确定吗?,4 + 48,

最终警告!

如果回复= vbNo那么

退出Sub

否则

frmPleaseWait 。显示

适用于i = 0至20

DoEvents

下一页我


BookDetails.comClear

BookDetails.Connection1.Close


For i = 0 to 20

DoEvents

Next i

''现在运行数据库紧凑程序

Dim JRO作为JRO.JetEngine

设置JRO =新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")

名称" c:\IB\IBook1 .MDB"作为c:\IB\IBook.mdb

设置JRO = Nothing

卸载frmPleaseWait

MsgBox("所有数据已被删除!)

BookDetails.Connection1.Open

BookDetails.rstblBooks.Open


结束如果

退出Sub

错误:

卸载frmPleaseWait

MsgBox err.Description

MsgBox"请注意:您的数据库没有被清除!

结束子


您将给予的任何帮助将不胜感激!!


谢谢,


Ben

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

推荐答案

" Joner" <是********* @ hotmail.com> schrieb
"Joner" <be*********@hotmail.com> schrieb
我在使用我的小程序时遇到了麻烦,我将其连接到访问数据库。似乎连接正常,断开连接,但是在它不能重新连接之后,我得到错误当对象打开时不允许操作。所以我拿出代码行:
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:




这个组(micrsoft.public.dotnet.languages.vb)是关于VB.NET(dotnet,

见组名称)。较旧的VB版本在

microsoft.public.vb。*组之一处理。

-

Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/ news / learn2quote.html



This group (micrsoft.public.dotnet.languages.vb) is about VB.NET ("dotnet",
see group name). Older VB versions are handled at one of the
microsoft.public.vb.* groups.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


* 是********* @ hotmail.com (Joner)破旧:

[...]


这是一个VB.NET语言组。你可能想转向

microsoft.public.vb.data。*组之一。


-

Herfried K. Wagner [MVP]

< http://www.mvps.org/dotnet>
* be*********@hotmail.com (Joner) scripsit:
[...]

This is a VB.NET language group. You may want to turn to one of the
microsoft.public.vb.data.* groups.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>


Heh heh。


似乎正在发生的事情是:


在您的原件中,您可能没有正确断开连接

(连接.Close),这意味着当你试图重新连接时,你得到了

当对象打开时不允许操作错误。然后你拿出了

" Connection.Open"方法,和其他东西(我猜一个记录集

操作或类似)抱怨连接关闭。


长期和短期它是把Connection1.Open放在一起。回来了,

找出断线无法正常工作的原因。

希望这会有所帮助,


Sparky。

" Joner" <是********* @ hotmail.com>在消息中写道

news:43 ************************** @ posting.google.c om ...
Heh heh.

What seems to be happening is this:

In your original, you might not have been disconnecting properly
(Connection.Close), which meant that when you tried to reconnect you got the
"operation is not allowed when object is open" error. Then you took out the
"Connection.Open" method, and something else (I''m guessing a recordset
operation or similar) is complaining that the connection closed.

The long and the short of it is to put the "Connection1.Open" back in, and
find out why the disconnect doesn''t work properly.

Hope this helps,

Sparky.
"Joner" <be*********@hotmail.com> wrote in message
news:43**************************@posting.google.c om...
你好,

我在使用我的小程序时遇到了麻烦,我连接到了一个访问数据库。似乎连接正常,断开连接很好,但是当它不能重新连接之后,我得到错误当对象打开时,操作不允许。所以我拿出了一行代码:

BookDetails.Connection1.Open

它出现错误当对象是
时,不允许操作关闭了

我举手说明我不是这方面的专家(因为你可能会在代码中看到!!)所以我很可能会使用错了
代码!!!但现在这变得非常令人沮丧因为我无法在任何地方找到任何帮助!!!

该程序用于将书籍的详细信息输入Access数据库
通过使用VB6创建的用户友好的GUI!

下面的代码来自两个表单,一个用于输入书籍详细信息,一个用于主菜单。这个问题在程序中经常发生,但是如果我可以帮助解决这个问题,那么我可以使用我学到的东西来纠正其他问题!我要给的例子是填写表格按钮和删除记录按钮。

当你加载程序时,你得到主菜单,你点击它
转到书籍表格,然后单击填写表格按钮并键入
参考编号,然后输入记录数量。这填写了所有的
参考,所以你不必手动输入它们,这很好!然后
当你点击关闭按钮并返回主菜单然后点击删除记录按钮它会删除所有记录(到目前为止
一切都很好)然后当你去回到书本表格并再次单击
填写参考按钮,当你收到我上面提到的错误时!
我做错了什么???

代码:

关于frmBooks:

Private Sub cmdFillForms_Click()
On Error GoTo err
MsgBox"请注意:你必须清除以前的所有内容书籍使用
这个功能和参考。编号字段应为空。

Dim CurrentRef,NumberRecords,NumberCount As Integer
NumberCount = 0
CurrentRef = InputBox(请输入您的第一个参考编号。 ;)
如果CurrentRef =""然后
MsgBox抱歉,您输入的号码无效!请再次尝试
。",vbExclamation
退出Sub
否则
NumberRecords = InputBox(请输入参考数量
要添加的数字。 )

frmPleaseWait.Show
对于i = 0到20
DoEvents
下一个我
做NumberCount< = NumberRecords - 1

BookDetails.rstblBooks.AddNew
txtRef.Text = CurrentRef
NumberCount = NumberCount + 1
CurrentRef = CurrentRef + 1
循环
BookDetails.rstblBooks.UpdateBatch
卸载frmPleaseWait
BookDetails.rstblBooks.MoveFirst
结束如果
退出Sub

错误:
MsgBox err.Description
卸载frmPleaseWait
结束子

关于frmMainMenu:

私人子cmdClear_Click()
错误GoTo错误
昏暗的回应
昏暗rs作为记录集

响应= MsgBox(&#;警告!没有办法撤消这个动作
请确保您确定要清除数据!,4 +
48,警告!
如果响应= vbNo那么退出Sub
结束如果

响应= MsgBox(最终警告!您即将删除所有
数据!没有办法撤消此操作!你确定吗?",4 + 48,
最终警告!
如果回复= vbNo那么
退出Sub
否则
frmPleaseWait.Show
对于i = 0到20
DoEvents
下一页

BookDetails.comClear
BookDetails.Connection1.Close

i = 0到20
DoEvents
下一步我现在运行数据库紧凑程序
Dim JRO作为JRO.JetEngine
设置JRO =新JRO.JetEngine
Source = c:\IB\IBook.mdb",_
" Provider = Microsoft.Jet.OLEDB .4.0;数据
Source = c:\IB\IBook1.mdb; Jet OLEDB:Engine Type = 4"

Kill(" c:\IB\IBook。 mdb")
名称" c:\IB \IBook1.mdb"作为c:\IB\IBook.mdb
设置JRO = Nothing
卸载frmPleaseWait
MsgBox(所有数据已被删除!)
BookDetails .Connection1.Open
BookDetails.rstblBooks.Open

结束如果
退出Sub
错误:
卸载frmPleaseWait
MsgBox err.Description <消息:请注意:您的数据库没有被清除!
End Sub

您可以给予的任何帮助都将不胜感激!!
谢谢,

Ben
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



这篇关于在VB6中连接和断开Access数据库时出现问题!!请帮忙!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆