Solved

Highlight a record

Posted on 2000-04-12
6
188 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

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