Results 1 to 24 of 24

Thread: [RESOLVED] [02/03] Combobox/Textbox help please

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Resolved [RESOLVED] [02/03] Combobox/Textbox help please

    Hi people, was hoping someone could offer me some help please. On my form I have two comboboxes and two textboxes, the comboxes and textboxes are linked to a database.

    The first combobox and textbox is linked to a customers table, the second combobox and textbox is linked to a caller table. Each customer in the customer table has a CustomerID, this field is also used in the Caller table as 1 customer can have many callers. Now for the complicated part, when the form loads each customers name is loaded in combobox 1, and when a customer is selected from combobox1 in textbox1 the CustomerID is shown. So far I have this working perfectly, however the next part is where I’m struggling.

    What I want to do is take the customerID from textbox1, and show all the callers in combobox2 with that same CustomerID, also each caller has their own telephone number so when a caller is selected I want to display the telephone number in textbox 2.

    Is this possible? I’ve been struggling a lot on the coding and any help I would appreciate it.

    So far I have the following coding which shows the customers in combobox1 and the customer ID in textbox1.

    Code:
        Dim connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\MS.mdb;Jet OLEDB:Database Password=king;")
            Dim adapter As New OleDbDataAdapter("SELECT ID, CusName, CusID FROM Customer ORDER BY CusName", connection)
            Dim table As New DataTable
            adapter.Fill(table)
            Me.cmbcus.DisplayMember = "Name"
            Me.cmbcus.ValueMember = "CusID"
            Me.cmbcus.DataSource = table
            Me.txtcus.DataBindings.Add("Text", table, "CusID")
    Please help me as I am stuggling.

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [02/03] Combobox/Textbox help please


    my guess is it would be very similar to the code you have above, except for the select statement. In the select statement you would have to have a WHERE clause to filter by customer id

    So, and I'm just guessing here...

    "SELECT CallerID, CallerName, PhoneNumber
    FROM Caller
    WHERE CustomerID = " & textbox1.text

    Then you would set the combobox properties for combobox2 and textbox2 accordingly.

    Does this help?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3
    Lively Member koskilla's Avatar
    Join Date
    Jan 2007
    Location
    LA, California
    Posts
    79

    Re: [02/03] Combobox/Textbox help please

    Quote Originally Posted by dolot
    "SELECT CallerID, CallerName, PhoneNumber
    FROM Caller
    WHERE CustomerID = " & textbox1.text
    I'm not 100% sure, but I seem to remember you need extra quotes in there like this:

    "SELECT CallerID, CallerName, PhoneNumber
    FROM Caller
    WHERE CustomerID = '" & textbox1.text & "'"

    That is a single ' and a double " followed by a double " single ' double "

    so you end up with ...WHERE CustomerID = 'textbox1'

  4. #4
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [02/03] Combobox/Textbox help please

    regarding

    Code:
     Dim connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\MS.mdb;Jet OLEDB:Database Password=king;")
            Dim adapter As New OleDbDataAdapter("SELECT ID, CusName, CusID FROM Customer ORDER BY CusName", connection)
            Dim table As New DataTable
            adapter.Fill(table)
            Me.cmbcus.DisplayMember = "Name"
            Me.cmbcus.ValueMember = "CusID"
            Me.cmbcus.DataSource = table
            Me.txtcus.DataBindings.Add("Text", table, "CusID")
    If data is showing up in cmbcus than it should so related cud_id automatically.... just make sure your txtcus's Enable Property is True
    also try with
    Code:
    Me.txtcus.DataBindings.Clear()
    Me.txtcus.DataBindings.Add("Text", table, "CusID")
    __________________
    Rate the posts that helped you

  5. #5
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [02/03] Combobox/Textbox help please

    Quote Originally Posted by koskilla
    I'm not 100% sure, but I seem to remember you need extra quotes in there like this:

    "SELECT CallerID, CallerName, PhoneNumber
    FROM Caller
    WHERE CustomerID = '" & textbox1.text & "'"

    That is a single ' and a double " followed by a double " single ' double "

    so you end up with ...WHERE CustomerID = 'textbox1'
    That's true if the CustomerID is a text field of some sort. Most ID fields are some form of numeric, so I left the single quotes off. Good point, though.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  6. #6
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [02/03] Combobox/Textbox help please

    Code:
    SELECT CallerID, CallerName, PhoneNumber
    FROM Caller
    WHERE CustomerID = @CustomerID
    
    cmd.Parameters.AddWithValue ("@CustomerId", textbox1.Text)
    Cmd will be the name of your command
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  7. #7
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [02/03] Combobox/Textbox help please

    Quote Originally Posted by onlyGirl
    Code:
    SELECT CallerID, CallerName, PhoneNumber
    FROM Caller
    WHERE CustomerID = @CustomerID
    
    cmd.Parameters.AddWithValue ("@CustomerId", textbox1.Text)
    Cmd will be the name of your command
    Yes, this is another way of doing it. You set the Cmd.CommandText = the SQL string and then add the parameters.

    OnlyGirl: Does this automatically put in single quote around the parameter value if they're needed?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  8. #8
    Lively Member koskilla's Avatar
    Join Date
    Jan 2007
    Location
    LA, California
    Posts
    79

    Re: [02/03] Combobox/Textbox help please

    Quote Originally Posted by dolot
    That's true if the CustomerID is a text field of some sort. Most ID fields are some form of numeric, so I left the single quotes off. Good point, though.
    Ya, thats usually true. I've seen customer IDs that use part of the name or something so it easier to identify, so the quotes would be necessary in that case, and I think it works the same with the quotes, even if the data type is numeric. But ya, you're right, its probably not needed

  9. #9
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [02/03] Combobox/Textbox help please

    I believe so.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Combobox/Textbox help please

    I'm sorry but I'm confused with what everyone is talking about

    My coding above I have set as a sub and call it in the form load event, shall I copy and paste the same coding and change the Sql statement? Onlygirl I'm not sure what you mean with cmd. Sorry to be a pain people.

  11. #11
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [02/03] Combobox/Textbox help please

    Quote Originally Posted by frankwhite
    shall I copy and paste the same coding and change the Sql statement?
    What I would do is copy the code into a new sub procedure, change the SQL string, and then change the references to those of combobox2 and textbox2. Then you would call this new sub procedure from the combobox2_SelectedItemChanged event.

    I'll let OnlyGirl explain to you about the cmd.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Combobox/Textbox help please

    Hey Dolot so far I've done this following (currently I dont have the customer database so I'm testing it out on another database.) The database consists of pupils in the different years.

    The following is under my sub
    Code:
        Private Sub Loadpup()
            Dim connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\MS.mdb;Jet OLEDB:Database Password=liambradywasking;")
            Dim adapter As New OleDbDataAdapter("SELECT ID, Name, PupilNumber FROM Year7 ORDER BY Name", connection)
            Dim table As New DataTable
            adapter.Fill(table)
            Me.cmbbyname.DisplayMember = "Name"
            Me.cmbbyname.ValueMember = "ID"
            Me.cmbbyname.DataSource = table
            Me.txtbyname.DataBindings.Add("Text", table, "PupilNumber")
        End Sub
    
        Private Sub loadtest()
            Dim connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\MS.mdb;Jet OLEDB:Database Password=liambradywasking;")
            Dim adapter As New OleDbDataAdapter("SELECT ID, Name, PupilNumber FROM(Year8) WHERE PupilNumber = '" & txtbyname.Text & "'", connection)
            Dim table As New DataTable
            adapter.Fill(table)
            Me.cmbbyname2.DisplayMember = "Name"
            Me.cmbbyname2.ValueMember = "ID"
            Me.cmbbyname2.DataSource = table
            Me.txtbyname2.DataBindings.Add("Text", table, "Merits")
    
        End Sub
    
    'Form Load Event
    
     Loadpup()
    
     Private Sub cmbbyname2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbbyname2.SelectedIndexChanged
            loadtest()
        End Sub
    The above fills the first combobox and textbox but doesn't do anything with the 2nd combobox and textbox. Any ideas on where I may hvae gone wrong?

    Thanks

  13. #13
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [02/03] Combobox/Textbox help please

    Quote Originally Posted by frankwhite
    I'm sorry but I'm confused with what everyone is talking about

    My coding above I have set as a sub and call it in the form load event, shall I copy and paste the same coding and change the Sql statement? Onlygirl I'm not sure what you mean with cmd. Sorry to be a pain people.

    Instead of supplying the adapter with the delete command, you create a command by itself then you can add parameters to it. For example:
    Code:
     Dim deleteProd As New OleDbCommand("Delete FROM Param_Prod WHERE API = @API AND StartDate = @StartDate AND EndDate = @EndDate AND Reservoir=@Reservoir AND InitialRate = @InitialRate")
    
                        deleteProd.Parameters.Add("@API", OleDbType.VarChar, 30, "API")
                        deleteProd.Parameters.Add("@StartDate", OleDbType.Date, 12, "StartDate")
                        deleteProd.Parameters.Add("@EndDate", OleDbType.Date, 12, "EndDate")
                        deleteProd.Parameters.Add("@Reservoir", OleDbType.VarChar, 30, "Reservoir")
                        deleteProd.Parameters.Add("@InitialRate", OleDbType.Double, 30, "InitialRate")
    
                        deleteProd.Connection = gConn
                        daWellData.DeleteCommand = deleteProd
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  14. #14
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [02/03] Combobox/Textbox help please

    Did you mean to put "Merits" in instead of "PupilNumber" in loadtest()? I'm seeing "PupilNumber" in the sql string and "Merits" in the textbox databindings.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Combobox/Textbox help please

    Thanks Onlygirl but that sounds confusing.

    Dolot sorry my mistake, I want to display the merits from Year 8 in textbox 2 (so some student in year7 will have the same pupilnumber as some student in year8). I have changed it to:
    Code:
        Dim connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\MS.mdb;Jet OLEDB:Database Password=liambradywasking;")
            Dim adapter As New OleDbDataAdapter("SELECT ID, Name, PupilNumber, Merit FROM(Year8) WHERE PupilNumber = '" & txtbyname.Text & "'", connection)
            Dim table As New DataTable
            adapter.Fill(table)
            Me.cmbbyname2.DisplayMember = "Name"
            Me.cmbbyname2.ValueMember = "ID"
            Me.cmbbyname2.DataSource = table
            Me.txtbyname2.DataBindings.Add("Text", table, "Merit")
    However again only combobox1 and textbox 1 show the data, this is driving me crazy now

  16. #16
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [02/03] Combobox/Textbox help please

    Opps! Sorry. combobox1_SelectedIndexChanged event is where you should call loadtest() from.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Combobox/Textbox help please

    No problem, I tried that but it breaks on the adapter.fill line of loadtest.

  18. #18
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [02/03] Combobox/Textbox help please

    What's the error that you get when it breaks?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Combobox/Textbox help please

    well just before it breaks i can see it work for a split second, the error says

    An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

    hth

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Combobox/Textbox help please

    Hey,

    I did the following:

    Code:
            Try
                Dim connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "\MS.mdb;Jet OLEDB:Database Password=liambradywasking;")
                Dim adapter As New OleDbDataAdapter("SELECT ID, Name, PupilNumber, Merit FROM(Year8) WHERE PupilNumber = '" & txtbyname.Text & "'", connection)
                Dim table As New DataTable
                adapter.Fill(table)
                Me.cmbbyname2.DisplayMember = "Name"
                Me.cmbbyname2.ValueMember = "ID"
                Me.cmbbyname2.DataSource = table
                Me.txtbyname2.DataBindings.Add("Text", table, "Merit")
            Catch ex As OleDbException ' 
                MessageBox.Show(ex.Message)
            End Try
    And the error it threw was 'Data type mismatch in criteria expression'

    Hope that means something to you.

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Combobox/Textbox help please

    Anyone please? I'm desperate to get this working.


  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: [02/03] Combobox/Textbox help please

    I figured out as the PupilNumber was a number it did not require the "", however now it breaks on this line:

    Code:
     Me.txtbyname2.DataBindings.Add("Text", table, "Merit")
    The error it shows is:

    Code:
    Additional information: This would cause two bindings in the collection to bind to the same property.
    Anyone know maybe why it is saying this? Much appreciated

  23. #23
    Lively Member
    Join Date
    May 2008
    Posts
    117

    Re: [02/03] Combobox/Textbox help please

    Hi!

    I would recommend to start from new.
    All Databinding stuff can be easily done in several assistants and designers of the IDE.
    U can learn to do so by whatching the Forms over data video-series.
    An interesting experiment to me would be, if u try to understand my tutorial DatenBank in 10 Minuten.zip
    Sorry, its german, but its full animated (u always have to click the green zones), and can lead u through the configuration process.
    And there is a small runnable Sample-App, which can especially teach, how to load and save Datasets with linked Tables.

  24. #24
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: [02/03] Combobox/Textbox help please


    Sorry, I took the weekend off. Could you re-post your current code so we can take a look at it and see if we can see what's up?
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

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