我用的是VB,SQL server 2005,做的简单的学生管理系统,在登陆页面就有错误,可能是连接数据库有错,我自学的VB,很多东西不懂,希望指教、、、您的举手之劳,使我们初学者最大的荣幸,非常感谢。。
模块的代码是:
    Public mnusertypr As Integer
    Public cn As New ADODB.Connection
    Public rs As New ADODB.Recordset
    Public admin As Boolean
    Public strconn As String
Sub main()
admin = True
 strconn = "Provider=SQLOLEDB.1;Persist Security Info=True;Initial Catalog=stu;Data Source=myhello"
cn.Open strconn
Form1.Show
End Sub
我建的数据库名字是“stu”,用的ODBC桥连接,数据源名称是“myhello”;form1的代码是:
Private Sub Command1_Click()
    Dim user As String, pwd As String, usertype As String
    Dim SQL As String
    Dim rs As ADODB.Recordset
    user = Trim(Text1.Text)
    pwd = Trim(Text2.Text)
    If Option1.Value = True Then
        usertype = Option1.Caption
    Else
       usertype = Option2.Caption
    End If
       SQL = "SELECT * FROM 用户表 WHERE username='" & user & "'and password ='" & pwd & "' and usertype='" & usertype & "'"
    Set rs = cn.Execute(SQL)
    If rs.EOF Then
        MsgBox "密码错误", vbCritical
        Exit Sub
        cn.Close
    Else
        MsgBox "密码正确 ", vbExclamation
        If Option1.Value = True Then
           Form2.Show
        Else
           Form3.Show
        End If
        cn.Close
     End If
End Sub
数据库里建了一个用户表,里面有username和password。界面有用户选择,可以选择管理员身份或学生身份。
输入用户名和密码后点确定就会出现“事实错误“3704”对象关闭时不允许操作”进行调试时,显示 Set rs = cn.Execute(SQL)有问题。

解决方案 »

  1.   

    rs.CursorLocation = adUseClient
    rs.Open SQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText
      

  2.   

    lz这个问题应该是启动对象错了
    在工程属性中的启动对象设置为sub main
      

  3.   

    'password是系统关键字,要用方括号
    SQL = "SELECT * FROM 用户表 WHERE username='" & user & "' and [password] ='" & pwd & "' and usertype='" & usertype & "'"
      

  4.   

      SQL = "SELECT * FROM 用户表 WHERE username='" & user & "' &"  and password ='" & pwd & "'" & "  and usertype='" & usertype & "'"
      

  5.   

    rs.CursorLocation = adUseClient
    rs.Open SQL, cn, adOpenForwardOnly, adLockReadOnly, adCmdText