Results 1 to 9 of 9

Thread: [RESOLVED] Add data to combo from other table

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    132

    Resolved [RESOLVED] Add data to combo from other table

    I have five tables in access database. and five types of data entry form in vb6.
    Table1 contains several fields. First field is "Name of the company"
    All tables have several fields but the first field of all the table is "Name of the company"
    Data to table1 is saved through form1
    Data to table to is saved through form 2 and so on.
    Name of the company can be entered through form 1 only ie if an new company is reqiured to be entered then the same can be done through form1 only.

    As other form also needs name of the company so I have added a combobox to every form for the field "Name of the company"

    Now I want to display the name of the company in the combox box1 of form 2 ( for all forms except form 1) from the field 0 (ie the first field) of table 1
    how can I do so?

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Add data to combo from other table

    Your database structure seems somewhat awkward.. But you want something like this:

    vb.net Code:
    1. Public Class Form1
    2.        
    3.     Dim ds As New DataSet()
    4.     'To connect to a .mdb file
    5.     Dim conn As New OleDb.OleDbConnection()
    6.     Dim da As New OleDb.OleDbDataAdapter()        
    7.        
    8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9.         'Creating and filling our dataset from the database
    10.         conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\*Your directory path*\*Database name*.mdb; User Id=admin;Password=;"
    11.         da.SelectCommand = New OleDb.OleDbCommand("SELECT Name_of_the_company FROM *Table name*")
    12.         da.SelectCommand.Connection = conn
    13.         da.Fill(ds)
    14.  
    15.         'If the dataset is not empty
    16.         If ds.Tables(0).Rows.Count > 0 Then
    17.             'Fill the combobox with the list of available records
    18.             BindCombo(combobox1, ds.Tables(0))
    19.         End If
    20.     End Sub
    21.  
    22.     Private Sub BindCombo(ByVal cbo As ComboBox, ByVal dt As DataTable)
    23.         With cbo
    24.             'Any quotation marks are required
    25.             .Items.Clear()
    26.             .DropDownStyle = ComboBoxStyle.DropDownList
    27.             .DataSource = dt
    28.             .DisplayMember = "Name_of_the_company"
    29.             .ValueMember = "Name_of_the_company"
    30.             '.SelectedIndex = 0
    31.         End With
    32.     End Sub
    Last edited by MaximilianMayrhofer; Nov 19th, 2007 at 04:47 AM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    132

    Re: Add data to combo from other table

    Thanks for the code. As i am learning VB6, so some more help is needed:
    Where to put tje code in a standard module or class module?
    At the top of the code vb.net code is written? So is it for vb.net or for vb6

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    132

    Re: Add data to combo from other table

    I have made a small sample file to explain my problem. Hope this will give more clarification.
    Attached Files Attached Files

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Add data to combo from other table

    Quote Originally Posted by sujittalukder
    At the top of the code vb.net code is written? So is it for vb.net or for vb6
    His example is VB.NET

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    132

    Re: Add data to combo from other table

    thanks but I need the same for VB6. Also i have attached a file in last post. Can someone plz help me to sort out the problem

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    132

    Re: Add data to combo from other table

    I have tried to do same by Adodc and adodb but not getting the result.
    For form 3, adodc is used. Here I want data from field(0). But no data comes in combo1 if field(0) is used. But If field(1) is used, data are coming. Why?
    Form form2, I tried Adodb. Here no data is coming.
    Can some one please help me.
    File is attached.
    Attached Files Attached Files

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Add data to combo from other table

    Don't use the Recordcount property to loop thru the records - it is much more efficient (and takes less code) to use the .EOF property instead, eg:
    Code:
    connect 
    
    Do While Not rs.EOF
        Combo1.AddItem rs.Fields(0)
        rs.MoveNext
    Loop
    The problem you had was that the RecordCount was -1 (there are records, but the exact number hasn't been determined yet). The .EOF and .BOF properties do not have that kind of issue.

    Note also that your line to open the recordset is less efficient than it should be - it would be better to replace the last two parameters with these three:
    adOpenReadOnly, adLockForwardOnly, adCmdText


    All of this is shown in the article How can I fill a combobox with values in a database? from our Database Development FAQs/Tutorials (at the top of this forum)

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    132

    Re: Add data to combo from other table

    Thanks si the greek. for the reply and guidance and for the tutorial link too. Very useful.

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