Dim Friends(0 To 10) As String

Private Sub Command1_Click()
If Friends(any) = text1.text Then   '(what should I put here instead of any?)
MsgBox "You already have " &  text1.text " in the list 
else

end if
End Sub

In above code
I have an empty array of 10 friends
I have a button,
on pressing it I can add a new friend in any array

I want such a code:

While adding a new friend, it should prompt if the friend is already available in array.

Please Help! :icon_confused:

Recommended Answers

All 2 Replies

You will need a variable to keep track of how many friends have been entered.

Dim MyFriendCount As Integer, LoopCounter As Integer

Then you will need to initialize your friend counter

MyFriendCount = -1

Then you will need to test to see if you haved entered any friends yet...

Dim FoundFriend As Boolean
If MyFriendCount = -1 Then 'no friend have been entered

But if they have then you need to test if you have reached the limit...

ElseIf MyFriendCount > 10 Then 'too many friends or you need to redimension array so notify user...

Then of course if you have not exceeded the friend count...

Else 'need to check to see if you have already entered friend
  For LoopCounter = 0 To MyFriendCount
    If UCase(Text1.Text) = UCase(Friends(LoopCount)) Then
      FoundFriend = True
    Else
      FoundFriend = False
    End If
  Next LoopCount
  
  If FoundFriend = True Then
    'notify user
  Else
    MyFriendCount = MyFriendCount + 1
    Friends(MyFriendCount) = Text1.Text
  End If
End If

Good Luck

Dear Friend
Thanks!

I will surely add you as a friend in my array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.