Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
| Search | Today's Posts | Mark Forums Read
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 21st, 2004, 07:27 PM
Authorized User
 
Join Date: Apr 2004
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default populating a list box using a DataReader

I'm hoping some very kind person can help me stop pulling my hair out. I've changed my approach to populating a list box by using a DataReader.

It displays the first match briefly then displays "Authenticate_User.MatchingRecord" for each instance of a match.

Authenticate_User is the name of the Program. MatchingRecord is a class.

I'm ready to give up. I've posted the code if you have time, I would buy you a big dinner.

The Public Class Works. I just included it in case you need it.
Public Class MatchingRecord
    Private cAccountName As String
    Public ReadOnly Property AccountName() As String
        Get
            Return cAccountName
        End Get
    End Property
    Private cAccountId As String
    Public ReadOnly Property AccountId() As String
        Get
            Return cAccountId
        End Get
    End Property
    Public Sub New(ByVal name As String, ByVal id As Integer)
        cAccountName = name
        cAccountId = id
    End Sub
End Class - This works

Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
   Dim SQLStmt As String
   Dim DisplayedInList As Integer
   Dim Cnxn As New OleDbConnection( _
   "Provider=Microsoft.Jet.OLEDB.4.0;" _
   & "Data Source=CommonAlgorithms.mdb")
   Cnxn.Open()
   If Cnxn.State <> ConnectionState.Open Then
            MsgBox("Unable to connect to the database...",
            MsgBoxStyle.Critical)
            End
    End If (My SQL statement Works)
        SQLStmt = String.Format( _
           "SELECT (LastName + ', ' + FirstName) as AccountName, Id from Accounts WHERE LastName like '{0}%' ORDER BY LastName, FirstName", _
           txtSearch.Text)
        'Debug.Write("SQLStmt=" & SQLStmt)
        Dim cmdSearch As New OleDbCommand(SQLStmt, Cnxn)
        Dim Matches As OleDbDataReader = cmdSearch.ExecuteReader

     Dim MatchingRecords As New ArrayList
     DisplayedInList = 0
     lbMatches.DisplayMember = "AccountName"
     lbMatches.ValueMember = "AcctId"
            Do
                MatchingRecords.Add(New MatchingRecord(Matches.Item
                ("AccountName"), Matches.Item("AcctId")))
                DisplayedInList += 1
            Loop Until (DisplayedInList > 50) Or Not Matches.Read

            lbMatches.DataSource = MatchingRecords
            lbMatches.Focus()
        End If
        Cnxn.Close()
        txtSearch.Focus()
    End Sub

Thank you.








Similar Threads
Thread Thread Starter Forum Replies Last Post
Populating a list box VBAbeginner Access VBA 1 April 8th, 2008 09:24 AM
multi-column list box values moved to 2nd list box sbmvr Access VBA 1 May 14th, 2007 01:58 PM
Binding a DataReader To A List Box testsubject ADO.NET 3 December 21st, 2005 06:41 AM
populating textboxes using list/Menu rylemer Javascript 0 November 23rd, 2005 10:05 PM
populating a box sporkman43 Classic ASP Basics 0 August 18th, 2003 02:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.