首先,如果是一行行的,不如用listbox,然后每次插入记录时,先循环判断是否存在 dim I as long for i=0 to list1.listcount-1 if list1.list(i)=插入的值 then msgbox "记录已经存在' exit for else list1.additem 插入的值 end if next i
很简单, 哈哈 只要判断一下就可以了,先读出所有的内容,然后在这些内容中查找是否有该插入的字符串,找到有的话,就不插入就可以了,前提是插入字符串之后,插入一个 [Enter] 值 :Chr(13) 举例:(TextBox名:Text1 ; strInsertText 为需要插入的字符串) Dim strText As String,strInsertText As String strText = Text1.Text If Instr(1,strText,Chr(13) & strInsertText & Chr(13),vbTextCompare) = 0 Then Text1.Text = strText & strInsertText & Chr(13) End If
如果是从数据库查找数据,可以在查找的时候就过滤。用select Distinct(字段) from Table .
select distinct * into tab from tb delete from tb insert into tb select * from tab drop table tab
对,数据库的话用一个 Distinct 就可以了SQL语句:select distinct * from 表名称
Private Sub Command1_Click() Dim s() As String, t As New Collection, i As Long s = Split(Text1.Text, vbCrLf) On Error Resume Next For i = 0 To UBound(s) t.Add s(i), s(i) Next ReDim s(1 To t.Count) For i = 1 To t.Count s(i) = t(i) Next Text1.Text = Join(s, vbCrLf) Erase s End Sub
http://www.netyi.net/in.asp?id=wuyaxlz
dim I as long
for i=0 to list1.listcount-1
if list1.list(i)=插入的值 then
msgbox "记录已经存在'
exit for
else
list1.additem 插入的值
end if
next i
只要判断一下就可以了,先读出所有的内容,然后在这些内容中查找是否有该插入的字符串,找到有的话,就不插入就可以了,前提是插入字符串之后,插入一个 [Enter] 值 :Chr(13)
举例:(TextBox名:Text1 ; strInsertText 为需要插入的字符串)
Dim strText As String,strInsertText As String
strText = Text1.Text
If Instr(1,strText,Chr(13) & strInsertText & Chr(13),vbTextCompare) = 0 Then
Text1.Text = strText & strInsertText & Chr(13)
End If
delete from tb
insert into tb select * from tab
drop table tab
Dim s() As String, t As New Collection, i As Long
s = Split(Text1.Text, vbCrLf)
On Error Resume Next
For i = 0 To UBound(s)
t.Add s(i), s(i)
Next
ReDim s(1 To t.Count)
For i = 1 To t.Count
s(i) = t(i)
Next
Text1.Text = Join(s, vbCrLf)
Erase s
End Sub
没看到?