明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2480|回复: 3

[求助]请问在vba里listbox控件怎么总是有问题?

[复制链接]
发表于 2007-4-26 15:46:00 | 显示全部楼层 |阅读模式

下面这段代码我在vb6.0里运行没问题怎么到vba里就不行了呢?总是提示:“无法获取list属性、属性阵列索引无效

Private Sub UserForm_Layout()
Dim sjk As New ADODB.Connection
Dim sjklj As String
Dim dwzd As New ADODB.Recordset
Dim dwjs As Double
Dim zd() As String
sjklj = "Provider=MSDASQL.1ersist Security Info=False;Data Source=hbcad"
sjk.Open sjklj
dwzd.Open "select aa=count(dwid) from tyname", sjk, adOpenDynamic, adLockBatchOptimistic
If Not dwzd.EOF Then
  dwjs = dwzd.Fields("aa")
End If
dwzd.Close
dwzd.Open "select * from tyname", sjk, adOpenDynamic, adLockBatchOptimistic
ReDim zd(dwjs) As String
i = 0
Do While Not dwzd.EOF
  i = i + 1
  zd(i) = dwzd.Fields("dwname")
  dwzd.MoveNext
Loop
For j = 0 To i
  ListBox1.List(j) = zd(j + 1) '此处总是提示“无法获取list属性、属性阵列索引无效
Next j

End Sub

发表于 2007-5-1 14:49:00 | 显示全部楼层
去到最后一位循环时,J+1已经大于I了,肯定会无效。
发表于 2007-10-23 12:49:00 | 显示全部楼层
本帖最后由 作者 于 2007-10-23 12:58:06 编辑

程序报错

实时错误[Microsoft][ODBC 驱动程序管理器]未发现数据源名称并且未指定默认驱动程序

程序出错的位置

sjklj = "Provider=MSDASQL.1ersist Security Info=False;Data Source=hbcad"
sjk.Open sjklj------------------------------错在这里

连接库Data Source=hbcad?????????????????

---------------------

Private Sub UserForm_Layout()
Dim sjk As New ADODB.Connection
Dim sjklj As String
Dim dwzd As New ADODB.Recordset
Dim dwjs As Double
Dim zd() As String
sjklj = "Provider=MSDASQL.1ersist Security Info=False;Data Source=hbcad"
sjk.Open sjklj
dwzd.Open "select aa=count(dwid) from tyname", sjk, adOpenDynamic, adLockBatchOptimistic
If Not dwzd.EOF Then
  dwjs = dwzd.Fields("aa")
End If
dwzd.Close
dwzd.Open "select * from tyname", sjk, adOpenDynamic, adLockBatchOptimistic
ReDim zd(dwjs) As String
i = 0
Do While Not dwzd.EOF
  i = i + 1
  zd(i) = dwzd.Fields("dwname")
  dwzd.MoveNext
Loop
For j = 0 To i
  ListBox1.List(j) = zd(j + 1) '此处总是提示“无法获取list属性、属性阵列索引无效
Next j

End Sub

发表于 2013-3-7 10:43:01 | 显示全部楼层
二楼正解,我试过类似问题对的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 16:01 , Processed in 0.159302 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表