为什么无法取得Combo或List控件的值,老提示“无效属性数组索引”,ListIndex老是-1

cfzx 2013-10-18 12:08:26
环境如下
表格1:门店表jxc_deptcat
表格2:客户表jxc_customer
3个combo,一堆文本框,保存和修改两种按钮。

使用datagrid控件读出数据,然后选择某一行的时候,将该行数据各项赋值给文本框和combo、list等控件。
不管是只有1个combo还是多个combo的窗体,读写数据都会时灵时不灵。或者其中一个combo的值能写入数据库,但另两个combo又变空白了。
本窗体的所属门店在数据库中专门有一个表格,里面有deptcat_id(int),deptcat_name(字符串)。我在combo的list中显示中文,然后再itemData中存储相应的id数字,客户表jxc_customer只存储deptcat_id。
客户类型则是直接在combo的list属性,加了“个人客户”、“单位客户”两种。
希望在修改时,可以自动将combo变为原来存储的数据(DataGrid1_RowColChange,怀疑是这里有问题,大家帮我看下改combo的语句有无错误)

Private Sub Command2_Click()
'修改
Set Rs = Adodc1.Recordset
Conn.BeginTrans
Rs("客户姓名") = Text2.Text
MsgBox ("性别:第" & List2.ListIndex & "项:" & List2.List(List2.ListIndex))
For i = List2.ListCount - 1 To step - 1
If List2.Selected(i) = True Then
Rs("性别") = List2.List(List2.ListIndex) '###20131016严重bug:ListIndex无法取值,总是-1
'Rs("性别") = List2.List(i)
End If
Next i
Rs("客户描述") = Text4.Text
Rs("送货地址") = Text5.Text
Rs("所属门店") = Combo1.ItemData(Combo1.ListIndex)
MsgBox ("客户类型:第" & Combo2.ListIndex & "项:" & Combo2.List(Combo2.ListIndex))
Rs("客户类型") = Combo2.List(Combo2.ListIndex)
Rs("户口类型") = Combo3.List(Combo3.ListIndex)
Rs("手机") = Text6.Text
Rs.Update
'Rs.Close
If MsgBox("你确认修改吗?", vbQuestion + vbYesNo, "确认更改") = vbYes Then
Conn.CommitTrans
Else
Conn.RollbackTrans
End If

End Sub

Private Sub Command4_Click()
'新增
Set Rs = Adodc1.Recordset
Rs.AddNew
Rs("客户姓名") = Text2.Text
Rs("性别") = List2.ItemData(ListIndex)
Rs("客户描述") = Text4.Text
Rs("送货地址") = Text5.Text
Rs("所属门店") = Combo1.ItemData(Combo1.ListIndex)
Rs("客户类型") = Combo2.List(Combo2.ListIndex)
Rs("户口类型") = Combo3.List(Combo3.ListIndex)
Rs("手机") = Text6.Text
Rs.Update
End Sub

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
'Text1.Text = DataGrid1.Columns("客户ID").CellValue(DataGrid1.Bookmark)
Text2.Text = "" & DataGrid1.Columns("客户姓名").CellValue(DataGrid1.Bookmark)
Text4.Text = "" & DataGrid1.Columns("客户描述").CellValue(DataGrid1.Bookmark)
Text5.Text = "" & DataGrid1.Columns("送货地址").CellValue(DataGrid1.Bookmark)
Text3.Text = DataGrid1.Bookmark

If ("" & DataGrid1.Columns("所属门店").CellValue(DataGrid1.Bookmark)) <> "" Then
For i = 0 To Combo1.ListCount - 1
If ("" & DataGrid1.Columns("所属门店").CellValue(DataGrid1.Bookmark)) = Combo1.ItemData(i) Then
Combo1.Text = Combo1.List(i)
Combo1.ItemData(0) = Combo1.ItemData(i)
MsgBox (Combo1.ItemData(i))
Exit For
Else
Combo1.Text = ""
End If
Next
End If

For i = 0 To Combo2.ListCount - 1
If ("" & DataGrid1.Columns("客户类型").CellValue(DataGrid1.Bookmark)) = Combo2.List(i) Then
Combo2.Text = Combo2.List(i)
Combo2.ItemData(0) = Combo1.ItemData(i)
Exit For
Else
Combo2.Text = ""
End If
Next

For i = 0 To Combo3.ListCount - 1
If ("" & DataGrid1.Columns("户口类型").CellValue(DataGrid1.Bookmark)) = Combo3.List(i) Then
Combo3.Text = Combo3.List(i)
Combo3.ItemData(0) = Combo3.ItemData(i)
Exit For
Else
Combo3.Text = ""
End If
Next
Text6.Text = "" & DataGrid1.Columns("手机").CellValue(DataGrid1.Bookmark)
End Sub

Private Sub Form_Load()
'类别
Set Rs = Nothing
Rs.Open "select distinct deptcat_name ,deptcat_id from jxc_deptcat", Conn, adOpenKeyset, adLockOptimistic
If Not Rs.EOF Then
Rs.MoveFirst
'循环将每条记录值加入Combo控件
For i = 0 To Rs.RecordCount - 1
Combo1.AddItem CStr(Rs.Fields("deptcat_name")), i
Combo1.ItemData(i) = Rs.Fields("deptcat_id")
Rs.MoveNext
Next
End If

End Sub


...全文
910 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
worldy 2013-11-01
  • 打赏
  • 举报
回复
没有项目被选择的时候就是-1,使用之前,先判断是否是-1
cfzx 2013-10-31
  • 打赏
  • 举报
回复
再顶一下。 在修改记录的界面如何赋值给combo? 需要赋值两个,一个是类型名称(中文),一个是类型ID(数字)。 设想是类型名称存在.list属性内,ID存在.itemdata属性内 哪位大侠能给个例子。
of123 2013-10-31
  • 打赏
  • 举报
回复
Combo1.AddItem "类型名称1" Combo1.ItemData(Combo1.NewIndex) = 1 Combo1.AddItem "类型名称2" Combo1.ItemData(Combo1.NewIndex) = 2
of123 2013-10-21
  • 打赏
  • 举报
回复
Rs("性别") = List2.List(i) '###20131016严重bug:ListIndex无法取值,总是-1
cfzx 2013-10-18
  • 打赏
  • 举报
回复
01 , 01.txt 《 VB6.0中通过MSChart控件调用数据库 》 02 , 02.txt 《 用VB6实现动态增减控件 》 03 , 03.txt ActiveX控件的创建 04 , 04.txt ADO控件和DATA控件的冲突(不能共存)的解决方法 05 , 05.txt Combo的自动查询技术 06 , 06.txt DirectX7.0使用心得(1) 07 , 07.txt DirectX7.0使用心得(2) 08 , 08.txt DirectX7.0使用心得(3) 09 , 09.txt FSO对象模型在VB中的应用 10 , 10.txt MsComm 控件的文字传输范例 11 , 11.txt Office或IE4风格的ToolBar 12 , 12.txt Regsvr32.exe注册控件的具体用法 13 , 13.txt TextBox的自动调节 14 , 14.txt TextBox实现打印机效果 15 , 15.txt TreeView的基本操作 16 , 16.txt VB5中DBGRID控件在VB6中使用 17 , 17.txt VB6.0动态加载ActiveX控件漫谈 18 , 18.txt VB与MS-Draw开发通用作图软件 19 , 19.txt VB中APP对象及其应用 20 , 20.txt VB中list控件的功能扩充 21 , 21.txt VB中防止将重复项目添加到列表框控件中 22 , 22.txt VB中用Multimedia MCI控件开发多媒体应用 23 , 23.txt Win Api在VB中的妙用 24 , 24.txt WINDOWS SCRIPT HOST对象在VB中的使用 25 , 25.txt 安装向导生成程序组并建立多个程序项 26 , 26.txt 保存复选框选项 27 , 27.txt 不用OCX来创建自己的控件(一) 28 , 28.txt 成组更新控件属性 29 , 29.txt 创建数据驱动窗体 30 , 30.txt 得到鼠标位置 31 , 31.txt 调整 Combo 下拉部分的宽度 32 , 32.txt 动态加入控件到VB控件数组中 33 , 33.txt 对ListView中的列排序 34 , 34.txt 放一个Combo到Toolbar中 35 , 35.txt 改变 ListIndex而不发生 Click 事

1,217

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧