Solved

Delete data record

Posted on 2004-09-17
5
189 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 51

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 51

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel - Save a copy of work book 13 89
Sorting multiple rows and columns, and count duplicates in Excel 2013 4 88
Visual Studio 2005 text editor 10 44
vbModal 12 57
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

830 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