Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Highlight a record

Posted on 2000-04-12
6
Medium Priority
?
195 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 300 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
Industry Leaders: 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

Industry Leaders: 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!

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

670 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