Solved

Delete data record

Posted on 2004-09-17
5
185 Views
Last Modified: 2010-05-02
How do I delete a DataRecord from the following:

Private Type DataRecord
    ID As Long
    Name As String
end type

Dim Clients(10) As DataRecord

'Add DataRecord
Clients(1).ID=456
Clients(1).Name="John"

'Delete Data Record
?????
[Other than Clients(1).ID="", etc]





0
Comment
Question by:adorenbaum
  • 2
  • 2
5 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 12090141
Private Type DataRecord
    ID As Long
    Name As String
    IsDeleted As Boolean
end type

then

Clients(1).IsDeleted = True

?
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 12090148
or

Private Type DataRecord
    ID As Long
    Name As String
    Status As String
end type

then

Clients(1).Status = "Deleted" 'etc...
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12090267
If you don't want to flag a record as deleted as ryancys has suggested (either in the structure itself or by keeping a seperate array), then you really are only left with the option of shifting all the data down to fill the hole left behind when you delete a record:

Option Explicit

Private Type DataRecord
    ID As Long
    Name As String
End Type

Private Clients() As DataRecord

Private Sub Form_Load()
    Dim i As Integer
   
    ReDim Clients(10)
    For i = LBound(Clients) To UBound(Clients)
        Clients(i).ID = i
        Clients(i).Name = "Client" & i
    Next i
    listClients

    deleteClient 4
    listClients
End Sub

Private Sub deleteClient(ByVal Index As Integer)
    If Index >= LBound(Clients) And Index <= UBound(Clients) Then
        Dim i As Integer
        For i = Index To UBound(Clients) - 1
            Clients(i) = Clients(i + 1)
        Next i
        ReDim Preserve Clients(UBound(Clients) - 1)
    Else
        Dim msg As String
        msg = "LBound(Clients) = " & LBound(Clients) & vbCrLf
        msg = msg & "Index = " & Index & vbCrLf
        msg = msg & "UBound(Clients) = " & UBound(Clients)
        MsgBox msg, vbCritical, "Index Out of Range"
    End If
End Sub

Private Sub listClients()
    Dim i As Integer
   
    For i = LBound(Clients) To UBound(Clients)
        Debug.Print Clients(i).ID, Clients(i).Name
    Next i
    Debug.Print "------------------------------"
End Sub
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 12090282
An alternative approach is to use a Class instead of a Type, and then store the instances of your class in a Collection using the ID as the key for fast retrieval.

Create a new project and add a Class Module.  Rename the Name property of your class module from Class1 to DataRecord.  The form code has several examples of how you can add/modify a record, retrieve a record by ID, and delete a record.

Regards,

Idle_Mind

' --------------------------
' Class DataRecord
' --------------------------
Public id As Long
Public Name As String

' --------------------------
' Form1
' --------------------------
Option Explicit

Private Clients As Collection

Private Sub Form_Load()
    Dim i As Long
    Dim dr As DataRecord
   
    Set Clients = New Collection
    For i = 1 To 5
        Set dr = New DataRecord
        dr.id = i
        dr.Name = "Client" & i
        addModifyClient dr
    Next i
    listClients
End Sub

Private Sub Command1_Click()
    Dim dr As DataRecord
    Set dr = New DataRecord
    dr.id = 3
    dr.Name = "John"
    addModifyClient dr
    listClients
End Sub

Private Sub Command2_Click()
    Dim dr As DataRecord
    Dim id As Long
   
    id = 1
    Set dr = getClient(id)
    If dr.id = id Then
        dr.Name = "Jane"
        addModifyClient dr
        listClients
    Else
        MsgBox "ID: " & id, vbInformation, "Client Not Found"
    End If
End Sub

Private Sub Command3_Click()
    deleteClient 2
    listClients
End Sub

Private Sub addModifyClient(ByVal dr As DataRecord)
    On Error Resume Next
    Clients.Remove CStr(dr.id)
    Clients.Add dr, CStr(dr.id)
End Sub

Private Function getClient(ByVal id As Long) As DataRecord
    On Error Resume Next
    Dim dr As DataRecord
    Set dr = New DataRecord
    Set getClient = dr ' will return blank class if not found
    Set getClient = Clients.Item(CStr(id))
End Function

Private Sub deleteClient(ByVal id As Long)
    On Error Resume Next
    Clients.Remove CStr(id)
End Sub

Private Sub listClients()
    Dim dr As DataRecord

    For Each dr In Clients
        Debug.Print dr.id, dr.Name
    Next
    Debug.Print "------------------------"
End Sub
0
 

Author Comment

by:adorenbaum
ID: 12097965
Idle_Mind:

Great answer.  As a follow-up, do you have any thoughts on how to save/load data in Datarecord to file.  For additional points, please see my thread on this topic here:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21136697.html

Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question