Solved

Highlight a record

Posted on 2000-04-12
6
185 Views
Last Modified: 2010-05-02
As a beginner, I am having trouble getting the following to work

This works fine and lists all the Records I require

Private Sub Form_Load()
  Data1.DatabaseName = PathToDB
  Set Database = OpenDatabase(PathToDB)
  Set rs = Database.OpenRecordset("SELECT * FROM Fleet")
    'if no records,leave the sub
    If rs.EOF Then Exit Sub
   
    With rs
    .MoveLast
     MyTotalCount = .RecordCount
    .MoveFirst
   
 
   For iVal = 0 To MyTotalCount - 1
 
      lstFleet.AddItem "Registration  " + !RegNo
      .MoveNext
   Next iVal
End With
rs.MoveFirst

End Sub

However, I now want to click on one of the records and delete it.
I would also have expected the first record to be highlighted but no records are.
Also how do I get the record that is clicked, to highlight ?
0
Comment
Question by:kennys
6 Comments
 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
the items in the listbox arent highlighting when you click them?
0
 

Author Comment

by:kennys
Comment Utility
yes, if I click them the item is highlighted.

I need it to respond to the click.

Also, I would like the first item highlighted when the items fill the list box to start with
0
 
LVL 3

Accepted Solution

by:
g_jones021500 earned 100 total points
Comment Utility
To highlight the first record you need to add the following line to the end of your form load:

lstFleet.listindex = 1 (or 0 not 100%sure which is the first item)

To respond to a click on the list write code in the lstFleet_Click event.

When you want to delete an item use:

lstFleet.RemoveItem IndexOfItemToRemove

where IndexOfItemToRemove is the item (usually the current one) you want to delete. You will probably get this index number using lstFleet.ListIndex. I would put this code in the click of a button titled "Delete Item", or a right click menu if you get real ambitious :)

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:pmcgivern
Comment Utility
At the end of your Form_Load put

lstFleet.Selected(0) = True

This will select the first item on your list box.

To Delete records I assume you want to click on the item to delete, highlighting it, and then click on a button:

<---code begins--->

Private Sub cmdDelete_Click()
   
    For wsCount = 0 To lstFleet.ListCount - 1
        If lstFleet.Selected(wsCount) = True Then
            'Get the RegNo to delete by removing the "Registration  " from the list entry
            RemoveIt = Right(lstFleet.List(wsCount), (Len(lstFleet.List(wsCount))) - 14)
            'Search for the item to delete
            While Not rs.EOF
                If rs!RegNo = CDbl(RemoveIt) Then
                    rs.Delete
                    GoTo Removed
                Else
                    rs.MoveNext
                End If
            Wend
        End If
Removed:
    Next wsCount
    'Reset the list
    lstFleet.Clear
    If rs.RecordCount = 0 Then Exit Sub
    With rs
    .MoveLast
     MyTotalCount = .RecordCount
    .MoveFirst
     
   
    For iVal = 0 To MyTotalCount - 1
 
      lstFleet.AddItem "Registration  " + CStr(!RegNo)
      .MoveNext
    Next iVal
    .MoveFirst
    End With
    lstFleet.Selected(0) = True
End Sub

<---code ends--->
0
 
LVL 2

Expert Comment

by:pmcgivern
Comment Utility
g_jones answer will remove the item from the list but will not remove the record from the database.  You have to use a rs.Delete to remove the record.
0
 
LVL 2

Expert Comment

by:pmcgivern
Comment Utility
Note:  change the line -

If rs!RegNo = CDbl(RemoveIt) Then

to

If rs!RegNo = RemoveIt Then

if RegNo is a string field - in my code it is a numeric field.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now