Solved

Delete data record

Posted on 2004-09-17
5
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 52

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 52

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 86

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 86

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

724 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