动态添加数据ComboBox的问题有代码 (急,在线等)
各位,我有一个问题。我想将某个表(Dept)里面某个字段(DeptName)的动态值添加到一个ComboBox的List属性里面去。
(就是说当我添加完数据后再关闭那个数据库后,还可以在Combobox的下拉列表当中看到刚刚添加的数据).
然后我再选择学生所属的系别(所属的系别对应的控件就是Combobox).点击 "添加 "按钮时.刚刚在Combobox当中选择的系别就保存到另一个表(Class)的字段(ClassDept)当中.
烦请各位说的详细一些.谢谢了..如果还有不明白的,我再给各位解释了.我的邮箱: 520herenfeng@163.com
下面的代码是解决问题的方法,但是combobox的第一项为空白,我希望combobox的第一项就是数据库(ACCESS)里面的数据,那应该怎么做呢。烦请各位解释的详细一些。谢谢了。
=====================================
Public Function func_product_md1(combo_md As ComboBox)
sqlr = "select username,userid from membermaster "
rs.Open sqlr, Conn, adOpenKeyset, adLockReadOnly
If Not rs.EOF Then
For i = 0 To rs.RecordCount - 1
combo_md.List(0) = " "
combo_md.List(i + 1) = Trim(rs( "username "))
rs.MoveNext
Next
End If
rs.Close
Conn.Close
Set rs = Nothing
Set Conn = Nothing
---------------------------
[解决办法]
If Not rs.EOF Then
For i = 0 To rs.RecordCount - 1
combo_md.List(i ) = Trim(rs( "username "))
rs.MoveNext
Next
End If
[解决办法]
Public Function func_product_md1(combo_md As ComboBox)
sqlr = "select username,userid from membermaster "
rs.Open sqlr, Conn, adOpenKeyset, adLockReadOnly
If Not rs.EOF Then
For i = 0 To rs.RecordCount - 1
combo_md.List(0) = " "
combo_md.List(i + 1) = Trim(rs( "username "))
rs.MoveNext
Next
End If
rs.Close
Conn.Close
Set rs = Nothing
Set Conn = Nothing
1 你的这个函数不好,太笨重了
应该这样做
Public Function func_product_md1(combo_md As ComboBox)
sqlr = "select username,userid from membermaster "
rs.Open sqlr, Conn, adOpenKeyset, adLockReadOnly
do while not rs.eof '当没有到达记录集地末尾
combo_md.additem Trim(rs( "username ")) '增加用户名字
rs.MoveNext '取下一行
End If
rs.Close
Conn.Close
Set rs = Nothing
Set Conn = Nothing
on error resume next '防止combo_md.listindex=0出错,让任何错误出现时都继续
combo_md.listindex=0 '让combo控件显示第一项
2 加入即可 on error resume next '防止combo_md.listindex=0出错,让任何错误出现时都继续
combo_md.listindex=0 '让combo控件显示第一项
如果还不明白,qq397667325问我