hey guys i have been troubled by this for quite some time now. i have this example code in the command button.
Dim ctr As Integer, ctr2 As String
ctr2 = lst1.ListCount
x = Val(txt1.Text)
For ctr = 0 To ctr2
If x = Val(lst1.List(ctr)) Then
MsgBox "Error Duplicate"
Else
Msgbox "Good"
End If
Next ctr

where i have these values stored in the listbox
12.2.23.32
34.78.88.99
12.2.45.67
67.77.89.00

i have a textbox where if it has the same value like for example 12.2.45.67 it will produce error duplicate as seen in my code else it will produce the message box "Good". but everytime i enter a text for example 12.2.89.88 it produces the msgbox "error duplicate" where the result should be "Good" can someone help me out thanks.

Hi,

Try the following code,it may compltes the expected operation...

all the best

With regards

Venkatramasamy SN

Your code contains some basic level errors

1.Why ctr2 is string type ?
2.You need to run the loop upto ctr2-1 not ctr2.
3.The values in the listbox are strings not numbers . Why using val to compare the values ?

Private Sub Command1_Click()
Dim ctr As Integer, ctr2 As Integer
Dim StrFound As Boolean
ctr2 = List1.ListCount
x = Text1.Text
StrFound = False
For ctr = 0 To ctr2 - 1
If StrComp(x, List1.List(ctr), vbTextCompare) = 0 Then
StrFound = True
End If

Next ctr
If StrFound = False Then
MsgBox "Error Duplicate", vbCritical
Else
MsgBox "Good", vbInformation
End If
End Sub

This article has been dead for over six months. Start a new discussion instead.