这是我的一个窗口代码,想实现的功能:
首次进入是默认选择所有条码表中数据,进入界面后,发现表头显示不出来,然后要求可以动态的选择条码表中的列,再点击查询,可是发现每次都错误,执行不了,麻烦哪位高手帮我解决一下
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)
急用,麻烦各位了
首次进入是默认选择所有条码表中数据,进入界面后,发现表头显示不出来,然后要求可以动态的选择条码表中的列,再点击查询,可是发现每次都错误,执行不了,麻烦哪位高手帮我解决一下
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)
急用,麻烦各位了
解决方案 »
- listbox中保存多个网址,如何将每个网址的标题都提取出来,然后把取得的标题和网址一一对应写入到listview中
- vb打开access的语句如果记录不存在会出错,该怎么解决。
- 有哪位高手知道 contextmenustrip里添加的子选项用的是那个词的?
- 求助 fpspread问题,如何设置根据数据内容自动调整行高
- 水晶报表
- 用Crystal report加每页统计公式
- vb+sql server动态连接服务器?(急!···)
- 关于mediaplayer控件为什么播放只有右声道?
- 如何截获鼠标左右键同时按下的消息?
- 执行 Automation Manager 是必须的吗?
- 书目marc的导入导出,高手帮帮
- 请教一个有关文本的基础问题。
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) & ""
......其他问题有待研究
--------------------------------
怎么没看到代码的 head() 在什么地方定义呀
你在多个过程里用到了head()数组,应该把它定义在通用里
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
选择几项之后点击查询,弹出框提示:
实时错误 ‘3625’:
在对应所需名称和序数集合中,未找到项目
然后点 调试,代码指向:
list2.TextMatrix(roww, tt) = rst.Fields(tt) & ""而且界面上会出来一行数据库中数据,不能全部显示,界面停止,不能跳别的地方去,也动不了
list2.TextMatrix(roww, tt) = rst.Fields(tt) & ""
Next tt
还有下面这里的kk的取值也注意看一下:For kk = 0 To k - 1
list2.TextMatrix(0, kk) = head(kk)
Next kk