English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I've to :
Write a program to store product codes in an array. Product codes have 6 characters and each code must be different from all the other codes. After the user enters a product code check that the code has not been used already, and output an appropriate message if it has.

I've got it half done but can't figure out the code to check that it's not been put in twice. When I enter in a code I have it listed in a list box. If you have any ideas please let me know. Only genuine answers please!
Thanks!

2007-05-12 09:13:14 · 1 answers · asked by Anonymous in Computers & Internet Programming & Design

1 answers

Create a function called IsDuplicate which will accept a string and return a boolean. Ths will be used to test the entered text and will return true if a duplicate is found.

Private Function IsDuplicate(strTest as String) as Boolean

In this function you will loop through each item in your array and compare it to the strTest variable.

dim var as variant
dim strItem as string
dim rslt as Boolean

rslt = False
For each var in myArray

strItem = Cstr(var) 'convert to string

if strTest = strItem then
rslt = True ' a duplicate has been found
end if
Next

IsDuplicate = rslt ' return the result

end function


THis example uses a for each loop which will scan each item in your array.

You will use this function by calling it from an IF ELSE THEN within your command button click event

If IsDuplicate(Text1.Text) Then
'duplicate has been found

ELSE
'Not a dupe OK to Add to List

End IF

2007-05-12 10:36:12 · answer #1 · answered by MarkG 7 · 0 0

fedest.com, questions and answers