这是我的一个窗口代码,想实现的功能:
首次进入是默认选择所有条码表中数据,进入界面后,发现表头显示不出来,然后要求可以动态的选择条码表中的列,再点击查询,可是发现每次都错误,执行不了,麻烦哪位高手帮我解决一下
Dim instorehouse As New ADODB.Connection
Dim rst As New ADODB.Recordset
Public list2count As Integer   '统计记录表中记录条数
Private Sub Command1_Click()
Dim list2SQL  As String
Dim finditem As String
Dim n, i, i1, i2  As Integer
 i1 = 0
 i2 = 0
 n = List1.ListCount - 1
 Do While i1 <= n
 If List1.Selected(i1) Then
  i2 = i2 + 1
 Else: i1 = i1 + 1
 End If
 Loop
 ReDim head(i2) As String
 i = 0
 Do While i <= n
 If List1.Selected(i) Then
  head(i) = List1.List(1)
  If finditem = "" Then
   finditem = finditem & List1.List(i)
  Else: finditem = finditem & "," & List1.List(i)
  End If
 Else: i = i + 1
 End If
 Loop If finditem = "" Then
    list2SQL = "select * from A "
 Else
   list2SQL = "select " & finditem & " from A "
  End If
 Call list2def(i2, head())
 Call list2disp(i2, list2SQL)
End SubPrivate Sub Command2_Click()
Me.Hide
End SubPrivate Sub Form_Load()
Dim head() As String
  
    List1.AddItem "111"
    List1.AddItem "222"
    List1.AddItem "333"
    List1.AddItem "444"
    List1.AddItem "555"
    List1.AddItem "666"
    List1.AddItem "777"
    List1.AddItem "888"
    List1.AddItem "999"
    List1.AddItem "000"
    
    instorehouse.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=tmbmxx.mdb;Persist Security Info=False"
    'Call clearzore
    Call list2def(0, head())
    Call list2disp(0, "select * from A")
End SubPrivate Sub list2disp(k1 As Integer, StrSQL As String)
  Dim roww As Integer     
  roww = 1
  list2.Clear
  list2.Rows = 1
  'Call list2def(k1, head())
  instorehouse.Open
  rst.Open StrSQL, instorehouse, 3, 2
  If rst.EOF = False Then
    rst.MoveFirst
    list2count = rst.RecordCount
    Else
    list2count = 0
  End If
  Do While rst.EOF = False
    list2.Rows = list2.Rows + 1
    If k1 = 0 Then
    list2.TextMatrix(roww, 0) = rst.Fields(0)
    list2.TextMatrix(roww, 1) = rst.Fields(1)
    list2.TextMatrix(roww, 2) = rst.Fields(2)
    list2.TextMatrix(roww, 3) = rst.Fields(3)
    list2.TextMatrix(roww, 4) = rst.Fields(4)
    list2.TextMatrix(roww, 5) = rst.Fields(5)
    list2.TextMatrix(roww, 6) = rst.Fields(6)
    list2.TextMatrix(roww, 7) = rst.Fields(7)
    list2.TextMatrix(roww, 8) = rst.Fields(8)
    list2.TextMatrix(roww, 9) = rst.Fields(9)      
    Else
    For tt = 0 To k1
    list2.TextMatrix(roww, tt) = rst.Fields(tt)
    Next tt
    End If
    roww = roww + 1
    rst.MoveNext
  Loop
  instorehouse.Close
End SubPrivate Sub list2def(k As Integer, head() As String) '将表头初始化入库
  Dim kk As Integer
  If k = 0 Then
  list2.TextMatrix(0, 0) = "111"
  list2.TextMatrix(0, 1) = "222"
  list2.TextMatrix(0, 2) = "333"
  list2.TextMatrix(0, 3) = "444"
  list2.TextMatrix(0, 4) = "555"
  list2.TextMatrix(0, 5) = "666"
  list2.TextMatrix(0, 6) = "777"
  list2.TextMatrix(0, 7) = "888"
  list2.TextMatrix(0, 8) = "999"
  list2.TextMatrix(0, 9) = "000"
  Else
  For kk = 0 To k
  list2.TextMatrix(0, kk) = head(kk)
  Next kk
  End If  
End Sub数据库为 access,表为A(111,222,333,444,555,666,777,888,999,000)
急用,麻烦各位了

解决方案 »

  1.   

    list2.TextMatrix(roww, 0) = rst.Fields(0)
    list2.TextMatrix(roww, 1) = rst.Fields(1)
    list2.TextMatrix(roww, 2) = rst.Fields(2)
    ......
    ------------------------------------
    如果rst.Fields(X)里的是Null值,这样赋值会出错的,最好写成list2.TextMatrix(roww, 0) = rst.Fields(0) & ""
    list2.TextMatrix(roww, 1) = rst.Fields(1) & ""
    list2.TextMatrix(roww, 2) = rst.Fields(2) & ""
    ......其他问题有待研究
      

  2.   

    应该是固定行没设置,FixedRows你的list2是表格控件,怎么这样命名呢,会误导人.
      

  3.   

    我的list2是msflexgrid控件啊,每次提示那个什么head()类型不匹配,估计是我不晓得如何去传值吧,或许是其它的原因。我把表结构给你们了,可以建个表调试下,谢谢各位了
      

  4.   

    list1是一个带复选框的列表,作用是可以任意选择要在界面上显示出来的列
      

  5.   

    每次提示那个什么head()类型不匹配
    --------------------------------
    怎么没看到代码的 head() 在什么地方定义呀
      

  6.   

    看到了,在Form_Load里定义了
    你在多个过程里用到了head()数组,应该把它定义在通用里
      

  7.   

    我刚开始是定义在通用里面的,不行我才转到FORM_load里面的,能不能帮我解决一下啊
      

  8.   

    改了多个地方,改后的代码如下,自己改了什么地方:Dim head() As StringDim instorehouse As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    Public list2count As Integer   '统计记录表中记录条数Private Sub Command1_Click()
        Dim list2SQL  As String
        Dim finditem As String
        Dim n As Long, i As Long, j As Long, selCount As Long
        i2 = 0
        n = List1.ListCount - 1
        For i = 0 To n
            If List1.Selected(i) = True Then
                selCount = selCount + 1 '得到选择的项数
            End If
        Next
        ReDim head(selCount) As String
        i = 0: j = 0
        For i = 0 To n
            If List1.Selected(i) = True Then
                head(j) = List1.List(i)
                j = j + 1
                finditem = finditem & ",[" & List1.List(i) & "]"
            End If
        Next
        finditem = Mid(Trim(finditem), 2)
      
        If finditem = "" Then
            list2SQL = "select * from A "
        Else
            list2SQL = "select " & finditem & " from A "
        End If
        Call list2def(selCount, head())
        Call list2disp(selCount, list2SQL)
    End SubPrivate Sub Command2_Click()
        Me.Hide
    End SubPrivate Sub Form_Load()
        list2.Cols = 10
        List1.AddItem "111"
        List1.AddItem "222"
        List1.AddItem "333"
        List1.AddItem "444"
        List1.AddItem "555"
        List1.AddItem "666"
        List1.AddItem "777"
        List1.AddItem "888"
        List1.AddItem "999"
        List1.AddItem "000"
        
        instorehouse.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\tmbmxx.mdb;Persist Security Info=False"
        'Call clearzore
        Call list2def(0, head())
        Call list2disp(0, "select * from A")
    End SubPrivate Sub list2disp(ByVal k1 As Integer, StrSQL As String)
        Dim roww As Integer
        roww = 1
        list2.Clear
        list2.Rows = 1
        Call list2def(k1, head())
        instorehouse.Open
        rst.Open StrSQL, instorehouse, 3, 2
        If rst.EOF = False Then
            rst.MoveFirst
            list2count = rst.RecordCount
        Else
            list2count = 0
        End If
        Do While rst.EOF = False
            list2.Rows = list2.Rows + 1
            If k1 = 0 Then
                list2.TextMatrix(roww, 0) = rst.Fields(0) & ""
                list2.TextMatrix(roww, 1) = rst.Fields(1) & ""
                list2.TextMatrix(roww, 2) = rst.Fields(2) & ""
                list2.TextMatrix(roww, 3) = rst.Fields(3) & ""
                list2.TextMatrix(roww, 4) = rst.Fields(4) & ""
                list2.TextMatrix(roww, 5) = rst.Fields(5) & ""
                list2.TextMatrix(roww, 6) = rst.Fields(6) & ""
                list2.TextMatrix(roww, 7) = rst.Fields(7) & ""
                list2.TextMatrix(roww, 8) = rst.Fields(8) & ""
                list2.TextMatrix(roww, 9) = rst.Fields(9) & ""
            Else
                For tt = 0 To k1 - 1
                    list2.TextMatrix(roww, tt) = rst.Fields(tt) & ""
                Next tt
            End If
            roww = roww + 1
            rst.MoveNext
        Loop
        instorehouse.Close
    End SubPrivate Sub list2def(ByVal k As Integer, head() As String) '将表头初始化入库
        Dim kk As Integer
        If k = 0 Then
            list2.TextMatrix(0, 0) = "111"
            list2.TextMatrix(0, 1) = "222"
            list2.TextMatrix(0, 2) = "333"
            list2.TextMatrix(0, 3) = "444"
            list2.TextMatrix(0, 4) = "555"
            list2.TextMatrix(0, 5) = "666"
            list2.TextMatrix(0, 6) = "777"
            list2.TextMatrix(0, 7) = "888"
            list2.TextMatrix(0, 8) = "999"
            list2.TextMatrix(0, 9) = "000"
        Else
            For kk = 0 To k - 1
                list2.TextMatrix(0, kk) = head(kk)
            Next kk
        End If
    End Sub
      

  9.   

    好象还有点问题,
    选择几项之后点击查询,弹出框提示:
    实时错误 ‘3625’:
    在对应所需名称和序数集合中,未找到项目
    然后点 调试,代码指向:
     list2.TextMatrix(roww, tt) = rst.Fields(tt) & ""而且界面上会出来一行数据库中数据,不能全部显示,界面停止,不能跳别的地方去,也动不了
      

  10.   

    For tt = 0 To k1 - 1  '<----------注意看这里 ,tt是从0 到k1-1的
        list2.TextMatrix(roww, tt) = rst.Fields(tt) & ""
     Next tt
    还有下面这里的kk的取值也注意看一下:For kk = 0 To k - 1
        list2.TextMatrix(0, kk) = head(kk)
    Next kk
      

  11.   

    你可以借助打印控件或其他软件来做,比如DataReport,ActiveReports,水晶报表,也可以导出到Excel去打印,还可以用Printer对象来打印资料在VB板块上就有