本人是生手,所以手生.请教以下问题,有想法但是写不出来.在论坛里问过,但是本人基础较差,写不出代码,呵呵!!! 我在窗体中有几个相关的复选框,我想把选中的多选框内容添加到一条记录中.
如: 有 绘画,互联网,旅游,音乐,篮球 等等选项 我要实现可插入也可以返回相关数据,请问我的数据表是就一个"爱好"属性,
还是建 "绘画,互联网,旅游,音乐,篮球'属性呢?
怎么实现保存和取出呢? 各位大大有代码能贴上来吗 ? tks!!如贴不上 一样的给分
如: 有 绘画,互联网,旅游,音乐,篮球 等等选项 我要实现可插入也可以返回相关数据,请问我的数据表是就一个"爱好"属性,
还是建 "绘画,互联网,旅游,音乐,篮球'属性呢?
怎么实现保存和取出呢? 各位大大有代码能贴上来吗 ? tks!!如贴不上 一样的给分
用T、F表示。
选了就T,没选就F。
很简单
Dim Cn As New ADODB.Connection
Private Sub Form_Load()
Cn.ConnectionString = "Driver={SQL Native Client};Server=localhost;Database=tmpDB;Uid=aaa;Pwd=bbb;"
Cn.Open
End SubPrivate Sub Command3_Click()
Dim Rst As New ADODB.Recordset
Dim Sql As String
Sql = "select * from tmpLove where uid=1"
Rst.CursorLocation = adUseClient
Rst.Open Sql, Cn, adOpenStatic, adLockPessimistic
If Rst.RecordCount > 0 Then
Call FillList(Rst.Fields("myLove"))
End If
End SubPrivate Sub Command2_Click()
Dim Cm As New ADODB.Command
Dim Sql As String
Dim nRow As Integer
Cn.CursorLocation = adUseClient
Sql = "select * from tmpLove where uid=1"
Cm.ActiveConnection = Cn
Cm.CommandText = Sql
nRow = Cm.Execute.RecordCount
If nRow > 0 Then
Sql = "update tmpLove set myLove=N'" & JoinStr & "' where uid=1"
Else
Sql = "insert into tmpLove(uid,myLove) values(1,N'" & JoinStr & "')"
End If
Cm.CommandText = Sql
Cm.Execute
End SubPrivate Function JoinStr() As String
Dim i As Integer
Dim tmpStr As String
For i = 0 To List1.ListCount - 1
If List1.Selected(i) = True Then
tmpStr = tmpStr & List1.List(i) & ","
End If
Next
tmpStr = Left(tmpStr, Len(tmpStr) - 1)
JoinStr = tmpStr
End Function
Private Sub FillList(tmpStr As String)
Dim i As Integer
tmpStr = "," & tmpStr & ","
For i = 0 To List1.ListCount - 1
If InStr(tmpStr, "," & List1.List(i) & ",") > 0 Then
List1.Selected(i) = True
End If
Next
End Sub
例如,用Access创建一个db1数据库,表名为t1,表中假设只有一个ah(爱好的拼音缩写,因为本人英语较弱,忽忽!)字段。
表结构:t1(ah varchar(50))在VB中的操作步骤是:
1、引用ADO库
(1)选择“工程”|“引用”菜单项。
(2)在打开的“引用”对话框中选择“Microsoft ActiveX Data Objects 2.8 Library”。如果列表中没有“Microsoft ActiveX Data Objects 2.8 Library” 可以使用“Microsoft ActiveX Data Objects 2.6 Library”或更低版本的类型库。2、在窗体上绘制两个复选框,分别用默认的Name属性,check1和check2,再将其caption属性分别设置为“足球”和“篮球”。还有就是画一个按钮。如下面的图。
双击按钮,编写如下代码:
Private Sub Command1_Click()
Dim cn As ADODB.Connection
Dim str1 As String
Set cn = New ADODB.Connection
cn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=c:\db1.mdb;"
str1 = ""
If Check1.Value = 1 Then
str1 = str1 & Check1.Caption
End If
If Check2.Value = 1 Then
str1 = str1 & Check2.Caption
End If
cn.Execute "INSERT INTO t1(ah) VALUES ('" & str1 & "') "
MsgBox ("添加数据成功!")
cn.Close
Set cn = Nothing
End Sub其实,这个程序如果上面的xd熟悉控件数组的话,用控件数组做那就更完美了!呵呵呵呵!接分啊!
呵呵!不好意思,我用的是check控件, 4楼兄弟的好象不是用List控件吧!
哦!补充下,我连接的是Access数据库先点散分,不结帖.
爱好字段保存各个check的值(不是caption),check是控件数组
Private Sub Command1_Click()'保存
Dim s As String
Dim SQL As String
Dim i As Long
For i = 0 To Check1.UBound
s = s & CStr(Check1(i).Value)
Next
SQL = "UPDATE 表1 SET 爱好='" & s & "' where id='001'"
cn.Execute SQL
End SubPrivate Sub Command2_Click()'读取
Dim s As String
Dim rs As New ADODB.Recordset
Dim i As Long
rs.Open "select 爱好 from 表1 where id='001'", cn, adOpenForwardOnly, adLockReadOnly, adCmdText
s = rs.Fields(0)
For i = 0 To Check1.UBound
Check1(i).Value = CLng(Mid(s, i + 1, 1))
Next
End Sub