Solved

Highlight a record

Posted on 2000-04-12
6
187 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
ID: 2706977
the items in the listbox arent highlighting when you click them?
0
 

Author Comment

by:kennys
ID: 2707093
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
ID: 2707738
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 2

Expert Comment

by:pmcgivern
ID: 2707933
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
ID: 2707943
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
ID: 2707953
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 / DAO 3.6 / run time error: Couldn't find installable ISAM 7 94
VBS file using code from 2nd file (txt or vbs) 4 36
Sending a email via excel using vba 6 79
Copy a row 12 61
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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

806 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