ComboBox问题

Accelerator 2005-09-17 03:47:54
ComboBox中的值是从数据库中读取的
数据库表中包含name和id两个字段,ComboBox显示的是name的内容,但之后我要保存的是name对应的id,而不是name文本
谢谢
...全文
166 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
VBDN 2005-09-17
  • 打赏
  • 举报
回复
'Option Explicit

Private Sub Form_Load()
List1.AddItem "张三" 'Name字段
List1.ItemData(List1.ListCount - 1) = 3 'ID字段
List1.AddItem "李四"
List1.ItemData(List1.ListCount - 1) = 4
List1.AddItem "王五"
List1.ItemData(List1.ListCount - 1) = 5
List1.AddItem "赵六"
List1.ItemData(List1.ListCount - 1) = 6
End Sub
Private Sub Command2_Click(Index As Integer)
Select Case Index
Case 0 '将左列表框选种项目移到右列表框
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) Then
List2.AddItem (List1.List(i))
List2.ItemData(List2.ListCount - 1) = List1.ItemData(i)
List1.RemoveItem (i)
End If
Next
Case 1 '将右列表框选种项目移到左列表框
For i = List2.ListCount - 1 To 0 Step -1
If List2.Selected(i) Then
List1.AddItem (List2.List(i))
List1.ItemData(List1.ListCount - 1) = List2.ItemData(i)
List2.RemoveItem (i)
End If
Next
Case 2 '将左列表框全部项目移到右列表框
For i = List1.ListCount - 1 To 0 Step -1
List2.AddItem (List1.List(i))
List2.ItemData(List2.ListCount - 1) = List1.ItemData(i)
List1.RemoveItem (i)
Next
Case 3 '将右列表框全部项目移到左列表框
For i = List2.ListCount - 1 To 0 Step -1
List1.AddItem (List2.List(i))
List1.ItemData(List1.ListCount - 1) = List2.ItemData(i)
List2.RemoveItem (i)
Next
End Select
End Sub

Private Sub Command1_Click()
'将选种的人员ID列出来,这就是你想要的东西
For i = 0 To List2.ListCount - 1
Debug.Print List2.ItemData(i)
Next i
End Sub

Private Sub List1_Click()
Debug.Print "ID:" & List1.ItemData(List1.ListIndex), "姓名:" & List1.Text
End Sub
Private Sub List2_Click()
Debug.Print "ID:" & List2.ItemData(List2.ListIndex), "姓名:" & List2.Text
End Sub
VBDN 2005-09-17
  • 打赏
  • 举报
回复
to:tembo(继续做程序员)
既然要用ID做主键,那么肯定是有原因的了,比如在大点的单位很有可能有人同名。
VBDN 2005-09-17
  • 打赏
  • 举报
回复
用ItemData寸放id,并且Sorted 属性设为 False。
jxgzay 2005-09-17
  • 打赏
  • 举报
回复
Combo1.Item存name
Combo1.ItemData存id
tembo 2005-09-17
  • 打赏
  • 举报
回复
写一个过程
'Name转换ID
Public Function mGetID(ByVal mStr As String) As Long
On Error GoTo mErr
Dim mRst As New ADODB.Recordset
mRst.Open "SELECT * FROM 表 WHERE Name='" & mStr & "'", mConnectstring, adOpenKeyset, adLockOptimistic, adCmdText
If mRst.BOF And mRst.EOF Then
mGetID = 0
Else
mGetID = mRst("Id")
End If
mRst.Close
Set mRst = Nothing
Exit Function
mErr:
MsgBox Err.Number & "," & Err.Description, vbCritical, mTitle
End Function

保存时:mGetID(ComboBox.text)
shierren 2005-09-17
  • 打赏
  • 举报
回复
保存name的同时可以Combo1.ItemData(Combo1.NewIndex)=id
Accelerator 2005-09-17
  • 打赏
  • 举报
回复
怎么做呢?没有用过。
conrad_wan 2005-09-17
  • 打赏
  • 举报
回复
用collection缓存

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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