Solved

Highlight a record

Posted on 2000-04-12
6
189 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

737 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