Results 1 to 6 of 6

Thread: database mdb to listbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    137

    database mdb to listbox

    help with database items shown in listbox when i click list items then that database data should show up

    i want to show all records in list1 and when i click list1 then the data should show in textboxes
    Last edited by bogaa; Nov 17th, 2012 at 09:35 AM.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: database mdb to listbox

    The logic would be to first select whatever field (assuming one??) you want to add to your listbox, connect to your db, and return a recordset. Run throught the recordset and add (additem) that field to your listbox.

    Then, in the click event of the the listbox, use that text in a query like:

    select * from yourTable where youfieldname = list1.text

    THEN, in each textbox put each field from this query (like text1.text = rs!item1, text2.text = rs!item2, etc)

    If you don't know how to use ado, a google search of "visual basic 6" "ado tutorial" will give you many to choose from.

    Get lost? come back for guidance

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    37

    Re: database mdb to listbox

    Hello,
    May I suggest to get this tool from : http://download.cnet.com/Database-Fu...ntBody;sideBar.
    Then pass the select statement to this function you will get the recordset of the select statement. do a do while loop till the end of recordset is reached . Inside the loop fill the listbox with the item. Then in the listbox_Click event write txtbox1=listbox1.text.
    I hope your problem is solved.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2012
    Posts
    137

    Re: database mdb to listbox

    naveed am not paying 1 penny for it

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,176

    Re: database mdb to listbox

    Here is an example--first, this code is my main form.....I have 3 textboxes (text1, text2, and text3). One Command button (Command1) and one listbox (list1):

    Code:
    Option Explicit
    Private Sub Command1_Click()
    Dim cmd As New ADODB.Command
    Dim rs As New Recordset
    Dim x As Integer
    Call dbConnection 'located in Module1
    Set cmd = New ADODB.Command
    Set cmd.ActiveConnection = cnn
    cmd.CommandText = "select question from FF"
    Set rs = cmd.Execute()
    For x = 0 To rs.recordcount - 1
    List1.AddItem (rs!question)
    rs.MoveNext
    Next x
    End Sub
    Private Sub List1_Click()
    Dim myQuestion As String
    myQuestion = List1.Text
    Dim cmd As New ADODB.Command
    Dim rs As New Recordset
    Dim x As Integer
    Call dbConnection 'located in Module1
    Set cmd = New ADODB.Command
    Set cmd.ActiveConnection = cnn
    cmd.CommandText = "select * from FF where question = "
    cmd.CommandText = cmd.CommandText + "'" + myQuestion + "'"
    Set rs = cmd.Execute()
    Text1.Text = rs!ans01
    Text2.Text = rs!ans02
    Text3.Text = rs!ans03
    End Sub
    Then, in my module (module1) I have the following to connect to my access db (db name = Feud, tablename = ff)

    Code:
    Public cnn As ADODB.Connection
    
    Public Function dbConnection()
    
    Set cnn = New ADODB.Connection
    With cnn
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "User ID=Admin;password= ;" & " Data Source=" & App.Path & "\Feud.mdb;"
    
    .CursorLocation = adUseClient
    .Open
    End With
    End Function
    I added the following reference to my project: Microsoft Active X Data Objects 2.0 Library
    (Project, References)

    This example should explain all you need to know....

    NOW, there are MANY ways to query DBs...this is my favorite...but probably not the BEST, easiest, or most efficient....I learned it this way initially, so, I use it a lot. I like using a Module with my DB connection cuz all I have to do is add the Module to any new project and its all there, except changing the db name.

    Good Luck

  6. #6
    Member
    Join Date
    Oct 2012
    Posts
    37

    Re: database mdb to listbox

    oh! . Sorry about that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width