Office中国论坛/Access中国论坛

标题: (已解决)为什么以下语句老报错? [打印本页]

作者: dreamice01    时间: 2008-2-4 10:50
标题: (已解决)为什么以下语句老报错?
那个RX.OPEN语句老报错,即使不用where子句也报错了,是么回事?

Dim cn As ADODB.Connection
Dim rx As New ADODB.Recordset
'user_online = 1
Set cn = GetNewConnection
'On Error GoTo Err_Command6_Click
    If IsNull(Text2) Or IsNull(Text4) Then
       MsgBox "没有输入用户名或密码,请重新输入", vbInformation, "系统提示"
       Exit Sub
    End If
   
    rx.Open "select * from user_tbl where [user_id]='Me.Text2.Value' and [user_password]='Me.Text4.Value'"

'连接函数
Public Function GetNewConnection() As ADODB.Connection
    Dim oCn As New ADODB.Connection
    Dim sCnStr As String
   
    sCnStr = "Provider='sqloledb';Data Source='wangqiang';" & _
        "Initial Catalog='accesstata';Integrated Security='SSPI';user_id='sa';password='120979120979'"
   
    oCn.Open sCnStr
   
    If oCn.State = adStateOpen Then
        Set GetNewConnection = oCn
    End If
End Function

[ 本帖最后由 dreamice01 于 2008-2-21 16:11 编辑 ]
作者: dreamice01    时间: 2008-2-4 11:11
原帖由 cuxun 于 2008-2-4 11:02 发表
这是肯定的:)
rx.Open "select * from user_tbl where =" & Me.Text2.Value & " and ='" & Me.Text4.Value & "'"



不是呀,按你的改了后,还是没用,仍提示:连接无法用于执行此操作,在此上下文中他可以已被关闭或无效

这是怎么回事?
作者: dreamice01    时间: 2008-2-4 11:21
标题: 回复 4# 的帖子
怎么改,因为我的后台数据库是SQL 2000,就是不知道怎么用这个数据的连接,麻烦你多多指教。

看了书上的很多资料改来改去都没有效果
作者: dreamice01    时间: 2008-2-4 11:27
标题: 回复 6# 的帖子
公司内网上不了,只能上MSN
作者: dreamice01    时间: 2008-2-4 11:28
dreamice01@hotmail.com
作者: dreamice01    时间: 2008-2-4 11:37
标题: 回复 9# 的帖子
像这种问题,有其它解决办法吗?
作者: dreamice01    时间: 2008-2-4 14:05
标题: 回复 9# 的帖子
'连接数据库的函数
Public Function GetNewConnection() As ADODB.Connection
    Dim oCn As New ADODB.Connection
    Dim sCnStr As String   
    sCnStr = "Provider='sqloledb';Data Source='wangqiang';" & _
        "Initial Catalog='accesstata';Integrated Security='SSPI';user_id='sa';password='120979120979'"   
    oCn.Open sCnStr   
    If oCn.State = adStateOpen Then
        Set GetNewConnection = oCn
    End If
End Function

'登录按钮代码

Private Sub Command6_Click()
Dim cn As ADODB.Connection
Dim rx As ADODB.Recordset
user_online = 1
Set cn = GetNewConnection
    If IsNull(Text2) Or IsNull(Text4) Then
       MsgBox "没有输入用户名或密码,请重新输入", vbInformation, "系统提示"
       Exit Sub
    End If   
    rx.Open "select * from user_tbl where [user_name]='" & Text2 & " ' and [user_password]=' " & Text4 & "'", cn, adOpenKeyset, adLockBatchOptimistic   
    If rx.EOF Then
       MsgBox "你输入的用户名或密码错误,请重新输入!" & Chr(10) & Chr(13) & "如果您还没有注册,请点击注册按钮!", vbCritical, "系统提示"
       Me.Text2.Value = ""
       Me.Text4.Value = ""
       Me.Text2.SetFocus
       Exit Sub
    Else
      user_name = Me.Text2.Value
      user_id = rx(user_id)
      user_level = rx(user_level)
      user_online = 2
      DoCmd.Close
      DoCmd.OpenForm "网络留言簿系统", acNormal
    End If
  rx.Close
  Set rx = Nothing
End Sub
我的后台数据库是SQL SEVER 2000
上午试了一上午,在接近中午的时候没有问题,但一冲动就忘记保存代码了,下午一写,又提示出错了,这次的提示是:对象变量或WITH块变量未设置
晕倒了




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3