+ -
当前位置:首页 → 问答吧 → VB6.0 连接无法用于执行此操作。在此上下文中它可能已被关闭或无效

VB6.0 连接无法用于执行此操作。在此上下文中它可能已被关闭或无效

时间:2011-09-09

来源:互联网

连接无法用于执行此操作。在此上下文中它可能已被关闭或无效
Private Sub Command1_Click()
If User.Text = "" Then
  MsgBox "请输入用户名。", vbOKOnly + vbExclamation, "系统提示"
  User.SetFocus
  Exit Sub
End If
Call OpenCNN '打开数据库
Set RS = Nothing
RS.Open "select * form User where 用户编号= '" & User.Text & "'And 密码 = '" & MiMa.Text & "',con,1,1"
If RS.EOF = True Then
  MsgBox "请重新输入学号或密码!"
  User.Text = ""
  MiMa.Text = ""
  User.SetFocus
Else
  MsgBox "登录成功"
End If
  RS.Close
  Zhu.Show
  Unload Me
End Sub
以上是写在 登陆 按键里的
下面是写在模块里的
Global con As New ADODB.Connection
Global RS As New ADODB.Recordset

Public Sub OpenCNN()
  Set con = Nothing
  con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\物流管理.mdB"
  con.ConnectionTimeout = 20 '打开等待时间
  con.CursorLocation = adUseClient '设置或返回游标服务的位置
  con.Open
End Sub
Public Sub CloseCNN()
  If con.State Then con.Close
  Set con = Nothing
End Sub

运行的时候总是提示 连接无法用于执行此操作。在此上下文中它可能已被关闭或无效
错误代码在这句:RS.Open "select * form User where 用户编号= '" & User.Text & "'And 密码 = '" & MiMa.Text & "'"
哪们高手帮忙看一下,为了这个东西,我找了很多资料。谢谢!

作者: puhong923   发布时间: 2011-09-09

看看我空间,有相应的文章,数据库的

作者: xiaoyao961   发布时间: 2011-09-09

http://blog.csdn.net/xiaoyao961/article/details/6762423

在一个“成员查询”界面上要输入一个人的姓名来查询数据库中的这个人的全部信息并显示在界面上[答疑]

作者: xiaoyao961   发布时间: 2011-09-09

con连接要公共变量,如果模块变量不行

作者: mmxx0212   发布时间: 2011-09-09

你的句子中主要错误在这里:
RS.Open "select * form User where 用户编号= '" & User.Text & "'And 密码 = '" & MiMa.Text & "',con,1,1"

要改成:
RS.Open "select * form User where 用户编号= '" & User.Text & "'And 密码 = '" & MiMa.Text & "'",con,1,1

不用模块或函数的方式,很容易搞错

要是用我那个函数:Function OpenAccess(Mdbfilepath As String, Optional Mdbpassword As String, Optional usemdw As Boolean) As Boolean  

再取一下数据就肯定不会错。
VB code
'┏〓〓〓〓〓〓〓〓〓 GetRs,start 〓〓〓〓〓〓〓〓〓┓
Function GetRs(Sql, Optional ErrInfo As String) As ADODB.Recordset
   '[mycode_id:1843],edittime:2011-1-17 下午 08:34:21
   On Error GoTo Err
   Dim Rs As New ADODB.Recordset
   'Set Rs = CreateObject("adodb.recordset")
   Rs.open Sql, Conn, 1, 3
   Set GetRs = Rs
   Exit Function
   Err:
   ErrInfo = Err.Description
End Function
'┗〓〓〓〓〓〓〓〓〓  GetRs,end  〓〓〓〓〓〓〓〓〓┛
SUB TEST()

Dim Rs As New ADODB.Recordset
SET RS=GETRS("select * form User where 用户编号= '" & User.Text & "'And 密码 = '" & MiMa.Text & "'")
MSGBOX RS(0)

END SUB



作者: xiaoyao961   发布时间: 2011-09-09

作者: Veron_04   发布时间: 2011-09-09

1,检查con是否已经连接数据库
2,user是系统保留字,不建议作为表名与字段名使用,一定要用则需要用方括号括起来
3,最后一个双引号的位置应该在con的逗号之前
RS.Open "select * form [User] where 用户编号= '" & User.Text & "'And 密码 = '" & MiMa.Text & "'",con,1,1

作者: Leftie   发布时间: 2011-09-09

热门下载

更多