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"
Msgbox "Good"
End If
Next ctr

where i have these values stored in the listbox

i have a textbox where if it has the same value like for example 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 it produces the msgbox "error duplicate" where the result should be "Good" can someone help me out thanks.


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
MsgBox "Good", vbInformation
End If
End Sub