?
Solved

Delete data record

Posted on 2004-09-17
5
Medium Priority
?
208 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 53

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 53

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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 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…
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

764 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