■■■■■■■■■■模块代码:■■■■■■■■■■ 
Public conn As New ADODB.Connection 
Sub main() 
conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=wenxian;Initial Catalog=student" 
conn.Open 
Form1.Show 
End Sub 
■■■■■■■■■■form1.frm代码如下■■■■■■■■■■ 
Public rs As New ADODB.Recordset 
Public sql As String 
Private Sub XPButton1_Click() 
sql = "select * from studentinfo where 学号='" & Trim(text1.Text) & "' and 密码='" & Trim(text2.Text) & "'" 
conn.CursorLocation = adUseClient
rs.Open sql, conn, 3, 3 
If Not rs.EOF Then 
Form2.Show 
Unload Me 
Form2.Frame1.Caption = rs.Fields("名字") & "的个人资料" 
Form2.text1.Text = rs.Fields("名字") 
Set Form2.DataGrid1.DataSource = rs 
Else 
a = MsgBox("您输入的用户名或密码有误", 1 + 16, "错误") 
End If 
rs.Close 
End Sub 
-----------运行后提书行集合不能作为标签---------- 
■■■■■■■■■■ 
■■■■■■■■■■ 
Set Form2.DataGrid1.DataSource = rs 
//行集合不能作为标签
这句错在哪?

解决方案 »

  1.   

    Public rs As New ADODB.Recordset 
    Public sql As String 
    你這個定義在form1中form2是不能引用它的
    要在sub main()
    定義為全局變量才行了
    sub main()
    public rs as new adodb.recordset'把此處加到全局定義
    ..........
    ...........
    end sub
    ■■■■■■■■■■模块代码:■■■■■■■■■■ 
    Public conn As New ADODB.Connection 
    Sub main() 
    Public rs As New ADODB.Recordset
    conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=wenxian;Initial Catalog=student" 
    conn.Open 
    Form1.Show 
    End Sub 
    ■■■■■■■■■■form1.frm代码如下■■■■■■■■■■ 
     
    Dim sql As String 
    Private Sub XPButton1_Click() 
    sql = "select * from studentinfo where 学号='" & Trim(text1.Text) & "' and 密码='" & Trim(text2.Text) & "'" 
    conn.CursorLocation = adUseClient
    rs.Open sql, conn, 3, 3 
    If Rs.recoredcount>0 Then 
    Form2.Show 
    Unload Me 
    Form2.Frame1.Caption = rs.Fields("名字") & "的个人资料" 
    Form2.text1.Text = rs.Fields("名字") 
    Set Form2.DataGrid1.DataSource = rs 
    Else 
    a = MsgBox("您输入的用户名或密码有误", 1 + 16, "错误") 
    End If 
    rs.Close 
    End Sub 
    -----------运行后提书行集合不能作为标签---------- 
    ■■■■■■■■■■ 
    ■■■■■■■■■■ 
    Set Form2.DataGrid1.DataSource = rs 
    你的代碼比較亂,不想改了
      

  2.   

    ■■■■■■■■■■模块代码:■■■■■■■■■■ 
    Public conn As New ADODB.Connection 
    Public rs As New ADODB.RecordsetSub main() 
    conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Password=wenxian;Initial Catalog=student" 
    With Conn
            .CursorLocation = adUseClient
            .ConnectionString = cnn
            .ConnectionTimeout = 10
            .Open
    End With
    Form1.Show 
    End Sub 
    ■■■■■■■■■■form1.frm代码如下■■■■■■■■■■ Dim sql As String 
    Private Sub XPButton1_Click() 
    sql = "select * from studentinfo where 学号='" & Trim(text1.Text) & "' and 密码='" & Trim(text2.Text) & "'" 
    conn.CursorLocation = adUseClient
    rs.Open sql, conn, 3, 3 
    If rs.RecordCount>0 Then 
    Form2.Frame1.Caption = rs.Fields("名字") & "的个人资料" 
    Form2.text1.Text = rs.Fields("名字") 
    With Frame2.DataGrid1
            .Columns(0).Width = 800
            .Columns(0).Caption = "姓名"
            .Columns(1).Width = 800
            .Columns(1).Caption = "密碼"
    …..
    …..
    ‘有多少行加多少行
    End With
    Set Form2.DataGrid1.DataSource = rs 
    Form2.Show 
    Unload Me 
    Else 
    a = MsgBox("您输入的用户名或密码有误", 1 + 16, "错误") 
    End If 
    set rs=nothing
    End Sub 
    ‘記得給分,還有問題請提出,謝謝
      

  3.   

    gxqianqin你的答案不行呀,提示未设置变量对象或WITH BLOCK对象
      

  4.   

    模塊
    Option Explicit
    Public conn As New ADODB.Connection
    Public rs As New ADODB.RecordsetSub main()
    conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=;Password=;Initial Catalog="
    With conn
            .CursorLocation = adUseClient
            .ConnectionString = conn
            .ConnectionTimeout = 10
            .Open
    End With
    Form1.Show
    End Subform1---------------------Private Sub Command1_Click()
        Dim sql As String
        sql = "select userid,paswad from userinfo where userid='" & Trim(Text1.Text) & "' "
        Set rs = conn.Execute(sql)
        If rs.RecordCount > 0 Then
            Form2.Show
            Unload Me
        Else
            MsgBox "11", 1 + 16, "11"
        End If
        Set rs = Nothing
    End Sub
    ---------------------------form2---------------Private Sub Form_Load()
        Frame1.Caption = rs.Fields("userid") & "的資料"
        Text1.Text = rs.Fields("userid")
        With DataGrid1
                .Columns(0).Width = 800
                .Columns(0).Caption = "姓名"
                .Columns(1).Width = 800
                .Columns(1).Caption = "密碼"
        End With
        Set Form2.DataGrid1.DataSource = rs
    End Sub
    -------------------------------
    你照著這個樣了改就可以了,我調試過了,可以用!
      

  5.   

    不行呀,我按你的方法。提示对象关闭时不允许操作,问题出在Set rs = conn.Execute(sql)
      

  6.   

    不好意思
    你把Form1了的set rs=nothing去掉嚴謹起見
    在If rs.RecordCount > 0 Then
    加上rs.MoveFrist
      

  7.   

    或者你把Set rs = conn.Execute(sql)
    改為rs.Open sql, conn, 3, 3
    我這個運行正常了
    你看看有沒有別的錯誤?
      

  8.   

    呵呵,可以了,谢谢啦
    刚学VB,很多地方没熟悉
    这个RS是全集定义的记录集吧
    虽然现在FORM2可以调用到RS
    那会不会影响其他窗体使用rs.fields("")呢?
    还有set rs=nothing换成rs.close好象就不行?这是为什么