Looking for work? Check out our jobs area.

VB.NET

Moderators: seancampbell
Number of threads: 3618
Number of posts: 9200

This Forum Only
Post New Thread

Report
Pick multiples from single table on one form? Posted by Chemboy on 4 Nov 2009 at 11:12 AM
I am using VB 2005 and Access 2007

How can I select four different things from the same list?

Basically, here's what I'm trying to do: I have table of intermediates. On my form, I want to be able to choose up to four different intermediates, which will be used in creating calibration standards.

In my CalStandards table I have listed intermediate1 through intremediate4
These 4 different intermediates are the 4 comboboxes used on the form.

When I place the four comboboxes, they all point to the same thing. If one changes, they all change.

So, here's what I did next: I added 4 different datasources, each pointing to the intermediates table in the dataset; (Intermediates1-Intermediates4), as well as the the whole dataset (LabDataSet). I dragged CalStandards under the LabDataSet to the form, then from the CalStandards table, I dragged the four Intermediate(1-4)Descriptions to the form.

Intermediate1Description ComboBox: DataSource = IntermediatesBindingSource, Display and Value Member = IntermediateDescription, SelectedValue = IntermediatesBindingSource - IntermediateDescription

Intermediate2Description ComboBox: DataSource = IntermediatesBindingSource1, Display and Value Member = IntermediateDescription, SelectedValue = IntermediatesBindingSource1 - IntermediateDescription

While they no longer match, I cannot move from one combobox to the next. It appears that I have the comboboxes setup incorrectly, which I don't understand because I have set my comboboxes the same way in other applications.

Well, apparently I had text assigned under the (DataBindings). Removing that allows me to move to and from comboboxes. But here's something curious, Say I have A, B, and C populated in the combobox. A is the first, and is what is initially showing. If I choose B, then A disappears, and the choice is B, B, and C. Then if I choose C, A is still gone, and the choice becomes C, B, C.

Where is A going? Does this mean that there is still something wrong with my comboboxes? If so, what?

Report
Re: Pick multiples from single table on one form? Posted by seancampbell on 11 Nov 2009 at 7:26 AM
Sorry for a delay in response. I am busy meeting deadlines at work and haven't had the time to make a proper answer for you.

This issue is related to the method in which you are populating your ComboBoxes. You have directly bound them to a table through a connection. Your best bet will be to gather the data from the table into an array, and populating the drop downs manually...

For instance:
    'Imports System.Data
    'Add the above line to the top of the form (above the class declaration)
    'This is globally defined
    Dim Intermediates() As String

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        Dim FO As OpenFileDialog
        Dim Con As New OleDbConnection
        Dim DBName As String = ""

        FO = New OpenFileDialog
        FO.InitialDirectory = Application.StartupPath
        If FO.ShowDialog() = Windows.Forms.DialogResult.OK Then
            DBName = FO.FileName
        Else 'Mustve clicked Cancel, exit
            Return
        End If

        'Set the Connection String of the Connection Object.
        'I found this connection string online, it seems to be the standard
        'for connecting to MS Access databases. If you use a Username and
        'password for your database, then you will need to use the correct
        'ones on the end of this connection string

        Con = New OleDbConnection
        Con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBName & ";User ID=Admin;Password="
        Try
            'Error handling keeps our software from crashing
            'when an error occurs
            Con.Open()
        Catch ex As Exception
            'You could opt to do something here
            'This code is called when an error occurs
            'I usually do a MsgBox
            MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Connection Error")
            Return
        End Try

        If Con.State = ConnectionState.Open Then
            'It openned
        Else
            'It didn't open
            MsgBox("Connection Did Not Open", MsgBoxStyle.OkOnly, "Connection Error")
            Return
        End If

        Dim SQL As String = "SELECT * FROM Intermediates"

        Dim DS As New DataSet

        Dim oData As OleDbDataAdapter
        oData = New OleDbDataAdapter(SQL, Con)
        oData.Fill(DS)

        'DS should contain our records, lets parse it and add them to the
        'Intermediates() String array we definted globally.
        'Remember that DS.Tables will contain all tables in our query
        'Since we only asked for 1 table of data, our data is located in
        'DS.Tables(0) (which is a DataTable Object)
        'If you prefered you could do:
        'Dim DT As New System.Data.DataTable
        'DT = DS.Tables(0)
        'And work off of DT, but it doesn't gain you much if your not working
        'with hundreds of thousands of records...

        ReDim Intermediates(DS.Tables(0).Rows.Count - 1) 'prep Intermediates by dimensioning array cells in it

        'Code for parsing the column names/column data
        'commented because we don't need it here
        'For i As Integer = 0 To DS.Tables(0).Columns.Count - 1
        '    'DS.Tables(0).Columns(i).Caption
        'Next

        'Parse and add data to the Intermediates Array
        For i As Integer = 0 To DS.Tables(0).Rows.Count - 1
            'The string value, Intermediate_ID, should be the exact name of the
            'Feild of data you want (column name)
            Intermediates(i) = DS.Tables(0).Rows(i)("Intermediate_ID")
        Next

        Con.Close() 'We don't want to keep the database connection
        'open constantly. In an access world it isn't as bad because the
        'database typically resides on you local machine
        'but if it were on a server, a constant connection would slow
        'down the network significantly

        Dim ComboBox As New ComboBox 'Just an example of how to fill the combo
        If Not IsNothing(Intermediates) Then
            'Incase Intermediates was never populated, check if it's Null
            ComboBox.Items.Clear()
            For i As Integer = 0 To Intermediates.Length - 1
                ComboBox.Items.Add(Intermediates(i))
            Next
        End If

    End Sub


Report
Re: Pick multiples from single table on one form? Posted by seancampbell on 11 Nov 2009 at 7:27 AM
For a more indepth example of manually working with data in an access database

http://www.programmersheaven.com/mb/VBNET/386345/386345/program-demonstrating-ms-access-database-manipulation/?S=B20000
Report
Re: Pick multiples from single table on one form? Posted by Chemboy on 11 Nov 2009 at 1:20 PM
Wow, that's complicated! Its like real programming, so it is very valuable. I need to learn how/what you did in time (I'm teaching myself, so it is slow going).

I do thank you very much for your reply.

It seems that I was on the right track in the beginning. And it actually was a simple solution. No data adapters or sql commands needed!

In case anyone else was/is interested in my solution, Here's what I did: I added the CalStandards table, then I dragged the four Intermediate(1-4)Descriptions to the form.

Intermediate1Description ComboBox: DataSource = IntermediatesBindingSource, Display Member = IntermediateDescription, Value Member = IntermediateID, SelectedValue = IntermediatesBindingSource - Intermediate1Description

Intermediate2Description ComboBox: DataSource = IntermediatesBindingSource1, Display Member = IntermediateDescription, Value Member = IntermediateID, SelectedValue = IntermediatesBindingSource1 - Intermediate2Description

The IntermediateID is the primary key, if that isn't obvious.

To get the different data sources (IntermediatesBindingSource, IntermediatesBindingSource1...): From the ComboBox Tasks, Data Source ComboBox, choose Other Data Sources > Project Data Sources > LabDataSet > Intermediates for each combobox. I ended up with 4 different IntermediatesBindingSources (IntermediatesBindingSource - IntermediatesBindingSource3) in the Combobox Tasks.

It appears to be working flawlessly. I can add, edit, delete records without any errors. Viewing the database in Access, shows the changes made through the program.
Report
Re: Pick multiples from single table on one form? Posted by seancampbell on 11 Nov 2009 at 1:26 PM
Excelent! Glad to hear you reached a solution on your own!

Dealing with data from databases with this method is ideal when you want to do advanced things. If your plan is to become a programmer professionally, then learning how to deal with SQL Data is going to be fairly important for you (In the last 4 years with my company, all programs I have written have involved collecting, storing, tracking, manipulating, and displaying data in some form or another. SQL Server 2003 databases have been the majority of my work, with some MySql, MsSql, Access DBs, and once an XML DB... data is gold

That article I posted a link to has an example of connecting SQL data to a ListView and allowing you to edit it. I am not a blogger or writer, so the flow of the information might not be ideal, but I tryed my hardest to explain each line of code with comments, so it should act as a great starting point to teach yourself this style of solution!

Take care, and thanks for reposting your solution!
-Sean Campbell - Forum Mod



 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.