我写了这样一个小程序,代码如下:
Dim recsADO As New ADODB.Recordset
Dim recsLine As New ADODB.Recordset
Private Function GetLine(ByVal StationName1 As String, ByVal StationName2 As String) As Boolean  Dim cnn As New ADODB.Connection
  Dim count1, count2 As Integer
  Dim L1(10), L2(10) As String
  Dim i, j, k, l As Integer  cnn.ConnectionString = "DSN=Article;UID=sa;PWD=sa"
  cnn.Open
  recsLine.Open "select  * from 车站路线表", cnn, 3, 2  i = 0
  count1 = 0
  recsLine.MoveFirst  While Not recsLine.EOF
    If recsLine.Fields(1).Name = StationName1 Then
      L1(i) = recsLine.Fields(0).Value
      i = i + 1
      count1 = i
    End If
    recsLine.MoveNext
  Wend  recsLine.MoveFirst
  j = 0
  count2 = 0
  While Not recsLine.EOF
    If recsLine.Fields(1).Name = StationName2 Then
      L2(i) = recsLine.Fields(0).Value
      j = j + 1
      count2 = j
    End If
    recsLine.MoveNext
  Wend
  GetLine = False
  For k = 0 To count1
    For l = 0 To count2
      If L1(k) = L2(l) Then GetLine = True
    Next l
  Next k
  End FunctionPrivate Sub Command1_Click()
  Dim i, j, k, m, n, p, q, a, b As Integer
  Dim station(1000), Line1(), Line2() As String
  Dim h(100, 100) As Integer
  Dim flag As Boolean
  Dim cnn As New ADODB.Connection  cnn.ConnectionString = "DSN=Article;UID=sa;PWD=sa"
  cnn.Open
  recsADO.Open "select  distinct 车站 from 车站路线表", cnn, 3, 2
  List1.Clear
  recsADO.MoveFirst  k = 0
  Do Until recsADO.EOF
    List1.AddItem recsADO.Fields(0).Value
      station(k) = recsADO.Fields(0).Value
    Debug.Print station(k)
    k = k + 1
    recsADO.MoveNext
  Loop
  Debug.Print List1.ListCount
  recsADO.Close
  cnn.Close  flag = 0
  For i = 0 To 93
    For j = 0 To 93
      h(i, j) = 0
      flag = GetLine(station(i), station(j))
      If flag = True Then h(i, j) = 1
    Next j
  Next i
  For a = 0 To 93
    For b = 0 To 93
      Debug.Print h(a, b)
    Next b
  Next a
End Sub
运行程序时,执行GetLine函数中“ recsLine.Open "select  * from 车站路线表", cnn, 3, 2”语句时提示错误:对象打开时,不执行操作。请高手帮我修改一下吧,谢谢

解决方案 »

  1.   

    前面加一句:
        If recsLine.State = adStateOpen Then recsLine.Close
        
      

  2.   

    recsLine.Open "select  * from 车站路线表", cnn, 3, 2
    最好写成
      recsLine.Open "select  * from 车站路线表", cnn, 1,3
    而且
      recsADO用完要
      recsADO.close
      set recsADO=nothing
      

  3.   

    cnn.ConnectionString = "DSN=Article;UID=sa;PWD=sa"
    cnn.Open
    if recsline.state=adstateopen then recsline.close          '<------加上这句
    recsLine.Open "select  * from 车站路线表", cnn, 3, 2
      

  4.   

    对象打开时,不允许操作
    -----------------------
    这种错误,一般都是因为数据库连接或记录集打开后,还没有关闭又打开而引起的,解决这种问题只需要在打开之前先关闭就好了
    加上一个判断语句:if recsLine.state=adstateopen then recsLine.close
      

  5.   

    你的GetLine()过程好象有问题:
    ......
    While Not recsLine.EOF
        If recsLine.Fields(1).Name = StationName1 Then
            L1(i) = recsLine.Fields(0).Value-----------初始化L1()
        .....While Not recsLine.EOF
        If recsLine.Fields(1).Name = StationName2 Then
            L2(i) = recsLine.Fields(0).Value-----------初始化L2()
        .....
    For k = 0 To count1
        For l = 0 To count2
          If L1(k) = L2(l) Then GetLine = True
    ......
      
    其中的recsLine.Fields(1).Name的值只有一个,或者是StationName1,或者是StationName2,或者两者都不是,如果是StationName1,那么L1()数组会被初始化,而L2()则不会被初始化,而下边又使用L1()和L2()进行比较,那样是不是程序写错了?
    我猜楼主是想写成这样吧(把recsLine.Fields(1).Name 改为:recsLine.Fields(1).Value ):
    While Not recsLine.EOF
        If recsLine.Fields(1).Value= StationName1 Then
            L1(i) = recsLine.Fields(0).Value-----------初始化L1()
        .....While Not recsLine.EOF
        If recsLine.Fields(1).Value= StationName2 Then
            L2(i) = recsLine.Fields(0).Value-----------初始化L2()
        ......
    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    如果真像我说的那样,代码倒是可以简化一点:
    ---------------------------------
    Dim recsADO As New ADODB.Recordset
    Private Function GetLine(ByVal StationName1 As String, ByVal StationName2 As String) As Boolean
        Dim cnn As New ADODB.Connection
        Dim recsLine1 As New ADODB.Recordset
        Dim recsLine2 As New ADODB.Recordset    cnn.ConnectionString = "DSN=Article;UID=sa;PWD=sa"
        cnn.Open
        recsLine1.Open "select  * from 车站路线表", cnn, 3, 2
        recsLine2.Open "select  * from 车站路线表", cnn, 3, 2    recsLine1.MoveFirst
        recsLine2.MoveFirst
        GetLine = False
        While Not recsLine1.EOF
            While Not recsLine2.EOF
                If recsLine1.Fields(0).Value = recsLine2.Fields(0).Value Then
                    GetLine = True
                End If
                recsLine2.MoveNext
            Wend
            recsLine1.MoveNext
        Wend
    End Function
    '------------------------------------
    Private Sub Command2_Click()
        Dim i, j, k  As Integer
        Dim station(1000), Line1(), Line2() As String
        Dim h(100, 100) As Integer
        Dim flag As Boolean
        Dim cnn As New ADODB.Connection
        
        cnn.ConnectionString = "DSN=Article;UID=sa;PWD=sa"
        cnn.Open
        recsADO.Open "select  distinct 车站 from 车站路线表", cnn, 3, 2
        List1.Clear
        recsADO.MoveFirst
        
        k = 0
        Do Until recsADO.EOF
            List1.AddItem recsADO.Fields(0).Value
              station(k) = recsADO.Fields(0).Value
            Debug.Print station(k)
            k = k + 1
            recsADO.MoveNext
        Loop
        Debug.Print List1.ListCount
        recsADO.Close
        cnn.Close
        
        flag = 0
        For i = 0 To 93
            For j = 0 To 93
                h(i, j) = 0
                flag = GetLine(station(i), station(j))
                If flag = True Then h(i, j) = 1
                Debug.Print h(i, j)
            Next j
        Next i    
    End Sub
    ------------------------------------
    不过还是不知道楼主的代码是什么意思
      

  6.   

    谢谢了,我就是在计算得到一个矩阵,是不是有高招,请faysky赐教!