Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete 1 element from Array

Status
Not open for further replies.

tyedyejenn

Programmer
Jun 14, 2000
35
US
My program is written in VB6. I set up an array for batch customers. When a receipts rep attaches payments customers are added to the batch customer array.
What I am having problems with is if the user decides they want to unattach a payment("delete" the customer from the array)

I want to say is:
delete udtBatchCustomer(strDeleteThisCustomer)

strDeleteThisCustomer = index in array to delete

Any Suggestions are greatly needed and appreciated
thanks-Jenn [sig][/sig]
 
Jenn,

Actually deleting the record/element in a memory array is not the method I would suggest. Add an element to the array to use as a flag for the "deletion", and check this flag prior to processing the item. The "direct" approach would invlove building a second array from the parts above/below the element you are removing, then replacing the original with the modified copy.

[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
If you do not want to add an element to the array that you have, you will have to loop through the whole array, checking to see if you have the customer you want to 'delete'. When you have found the customer to delete, move the next customer into this element and then continue moving the customers up one position. Afterwards Redim Preserve the array to get rid of the unwanted element from the end of the array.

EG
Create a project and put a text box and button on a form. Paste the following code into the general declarations of the form:

Dim arrCustomers() As String

Private Sub Command1_Click()

Dim strCustomer As String
Dim i As Integer
Dim sMsg As String
Dim bFound As Boolean

strCustomer = &quot;Cust&quot; & Val(Text1.Text)

MsgBox &quot;Delete &quot; & strCustomer

For i = LBound(arrCustomers) To UBound(arrCustomers)
If i = UBound(arrCustomers) Then
If bFound Or strCustomer = arrCustomers(i) Then
arrCustomers(i) = &quot;&quot;
bFound = True
End If
Else
If Not bFound Then
If arrCustomers(i) = strCustomer Then
arrCustomers(i) = arrCustomers(i + 1)
bFound = True
End If
Else
arrCustomers(i) = arrCustomers(i + 1)
End If
End If
Next i

If bFound Then
ReDim Preserve arrCustomers(UBound(arrCustomers) - 1)
End If

For i = LBound(arrCustomers) To UBound(arrCustomers)
sMsg = sMsg & Chr(13) & i & &quot;, &quot; & arrCustomers(i)
Next i

MsgBox sMsg

End Sub

Private Sub Form_Load()

Dim i As Integer
Dim sMsg As String

For i = 0 To 10
ReDim Preserve arrCustomers(i)
arrCustomers(i) = &quot;Cust&quot; & i
Next i

For i = LBound(arrCustomers) To UBound(arrCustomers)
sMsg = sMsg & Chr(13) & i & &quot;, &quot; & arrCustomers(i)
Next i

MsgBox sMsg

End Sub

To use the program, type a number (initially 0 to 10) into the text box and click the button. The code behind the button does the removing part.

Hope this helps,

Simon [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top