Solved

Delete data record

Posted on 2004-09-17
5
173 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 49

Expert Comment

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

then

Clients(1).IsDeleted = True

?
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now