Results 1 to 19 of 19

Thread: SQL string problem... take a value, take the ID, search for the ID

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    139

    SQL string problem... take a value, take the ID, search for the ID

    Hi,

    I would like to write allot of text here, but i know that I would just be waffling!

    What I have is a combobox that displays all of the Location Names in a form. (this works perfectly) What i want is to take the Location Name that the user selected and look in the Location Table Table for the Location ID.Then to get all of the records in the Stock Table with the Location ID as a Foreign key and put the the Name of the Item of stock (that was found) into a separate Combobox.

    For more info: see my post below.

    There must be a simple way to do this, I have had a look on google and tried the JOIN method,but that doesn't work! IT doesn't do anything at all.... its bugging me.

    I'm using Access 2010 and Vb.net 2008 with OLEDB

    Any help would be appreciated.


    Thanks

    John.
    Last edited by KingJ; Feb 8th, 2012 at 04:50 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: SQL string problem... take a value, take the ID, search for the ID

    Show us the SQL using the JOIN, because that is what you need to do, most likely. It would look something like this:

    SELECT <fields from Stock table> FROM StockTable INNER JOIN LocationTable ON StockTable.LocationID = StockTable.LocationID WHERE LocationTable.LocationName = '" & comboboxitem & "'"

    The exact details are missing from that, though (and it concatenates in a string rather than using parameters, but that could be easily changed).
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    139

    Re: SQL string problem... take a value, take the ID, search for the ID

    Thanks.
    Will Test that out now and let you know

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    139

    Re: SQL string problem... take a value, take the ID, search for the ID

    Right I can't get it to work

    I have 3 tables:

    Table "DepartmentTable" contains: "DepartmentID", "ClientID", "DepartmentName"
    Table "LocationTable" contains: "LocationID", "LocationID"
    Table "ClientTable" contains: "ClientID", "ClientName"

    I have the following code that I am completely Stuck with, trying to follow the advice of @Shaggy Hiker .

    I am getting NO reading at all from this. which seems VERY strange. I think that im doing it correctly although I am not certain. Infact, 100&#37; sure its not correct.

    vb Code:
    1. Sql = "SELECT * FROM DepartmentTable " & _
    2.     " INNER JOIN ClientTable" & _
    3.     " ON DepartmentTable.ClientID = ClientTable.ClientID" & _
    4.     " WHERE ClientTable.ClientName = '" & _
    5.         " " & CLBClient.Items(x) & " ') "
    6.  
    7.  
    8.         Comando = New OleDbCommand(Sql, DBConn)
    9.         Comando.CommandType = CommandType.Text
    10.         RDR = Comando.ExecuteReader()
    11.  
    12.         While RDR.Read()
    13.             MessageBox.Show(RDR("DepartmentName"))
    14.  
    15.         End While

    EDIT:
    When I add a breakPoint into the table, I get the following SQL string.


    Code:
    SELECT * FROM DepartmentTable  INNER JOIN ClientTable ON DepartmentTable.ClientID = ClientTable.ClientID WHERE ClientTable.ClientName = ' MAH ')

    MAH has a ClientID 2 and appears 4 times in the DepartmentTable with CLIENTID as 2.


    Last edited by KingJ; Feb 8th, 2012 at 04:42 PM.

  5. #5
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: SQL string problem... take a value, take the ID, search for the ID

    Why not load your combo box with the LocationID and the Location, display only the Location and when you select the Location you will also have the LocationID, requery the Stock Table with the LocationID and fill the other combo box?
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    139

    Re: SQL string problem... take a value, take the ID, search for the ID

    Quote Originally Posted by CoachBarker View Post
    Why not load your combo box with the LocationID and the Location, display only the Location and when you select the Location you will also have the LocationID, requery the Stock Table with the LocationID and fill the other combo box?
    Its because Its not a Combobox i'm adding it to .. its a ComboListbox. I will try the suggestion and see it helps. Thanks

  7. #7
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    340

    Re: SQL string problem... take a value, take the ID, search for the ID

    I'm also having a similar problem.

    Code:
    Dim SQLQuery = "SELECT * FROM BTABLE WHERE BCOLUMN = WC.DownloadString("http://mywebsite.com/create.txt")
    Is there anyway I can get this to work? I know I need more quotes,apostrophes,colons, etc; But I have no clue where to put them.

    thanks

  8. #8
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: SQL string problem... take a value, take the ID, search for the ID

    What I have is a combobox that displays all of the Location Names in a form. (this works perfectly) What i want is to take the Location Name that the user selected and look in the Location Table Table for the Location ID.Then to get all of the records in the Stock Table with the Location ID as a Foreign key and put the the Name of the Item of stock (that was found) into a separate Combobox.
    Same principle different control.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    139

    Re: SQL string problem... take a value, take the ID, search for the ID

    I really Don't understand what your saying CoachBarker. Sorry

  10. #10
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: SQL string problem... take a value, take the ID, search for the ID

    will try to put something together later, am going out for a while and will be back later.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    139

    Re: SQL string problem... take a value, take the ID, search for the ID

    Thanks CoachBarker. I may well go to bed when you return. As i live in the UK.

    If you could post a comment later then that would be brilliant! thanks

  12. #12
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: SQL string problem... take a value, take the ID, search for the ID

    Here's a simple example of what CoachBarker is suggesting. Define an object that holds both the name of the location (that you want to display to the user) and the id of the location (that you want the computer to work with):

    vbnet Code:
    1. Public Class Location
    2.     Private ReadOnly _locationId As Integer
    3.     Private ReadOnly _locationName As String
    4.  
    5.     Public Sub new (ByVal locationId As Integer, ByVal locationName As string)
    6.         _locationId = locationId
    7.         _locationName = locationName
    8.     End Sub
    9.  
    10.     Public ReadOnly Property Id As Integer
    11.         Get
    12.             Return _locationId
    13.         End Get
    14.     End Property
    15.  
    16.     Public ReadOnly Property Name As String
    17.         Get
    18.             Return _locationName
    19.         End Get
    20.     End Property
    21. End Class

    Now when you fill your combo box of locations, don't fill it with strings holding the location name, fill it with Location objects holding both the name and the id. Use the ComboBox's DisplayMember path to make the ComboBox display the Name property of the item.
    When retrieving the user's selection, use the SelectedItem property to get the whole Location object, from which you can retrieve the Id directly:
    vbnet Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles MyBase.Load
    4.         ' Populate the combo box from your database, obviously, but for a sample...
    5.         locationsComboBox.Items.AddRange(
    6.             {
    7.                 New Location(1, "Location 1"),
    8.                 New Location(2, "Location 2"),
    9.                 New Location(3, "Location 3")
    10.             })
    11.  
    12.         ' Make the combo box display the Name property of the item.
    13.         ' This can be done from the designer as well, but I can't show that in a code sample
    14.         locationsComboBox.DisplayMember = "Name"
    15.     End Sub
    16.  
    17.     Private Sub getStockButton_Click( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles getStockButton.Click
    18.         Dim selectedLocation As Location = locationsComboBox.SelectedItem
    19.  
    20.         ' If no location selected, exit early
    21.         If selectedLocation is Nothing
    22.             Return
    23.         End If
    24.  
    25.         ' Otherwise, do your query directly with the location id of the location
    26.         MessageBox.Show(String.Format("Location Id = {0}", selectedLocation.Id))
    27.     End Sub
    28. End Class

    Passing the location name as a parameter to your subsequent query isn't a great idea. Not only does it mean you need to join two tables, but the database will be optimised for looking up the relevant records based on their ids. Also consider what happens if you have two locations with the same name?

    It's also just more complex to understand what you're doing, whereas a query that takes a location id and queries a table to get all the stock for that location (or clients and departments as your subsequent posts talk about) is nice and clear in what its doing. Compare:
    Code:
    SELECT <fields from Stock table>
    FROM StockTable
    INNER JOIN LocationTable ON StockTable.LocationID = StockTable.LocationID
    WHERE LocationTable.LocationName = ???
    Code:
    SELECT <fields from Stock table>
    FROM StockTable
    WHERE StockTable.LocationID = ???
    Last edited by Evil_Giraffe; Feb 8th, 2012 at 05:58 PM. Reason: added SQL comparison

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: SQL string problem... take a value, take the ID, search for the ID

    I totally agree. That's the way to go. The JOIN will be inefficient unless you had set an index on the name field, which you shouldn't be doing, because you already have an index on the ID field (as it should be).
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    139

    Re: SQL string problem... take a value, take the ID, search for the ID

    Ok - so how would i use the index on the field name and put that into something that I can actually use. I have tried the Code that Evil suggested no luck

  15. #15
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: SQL string problem... take a value, take the ID, search for the ID

    Something doesn't add up, you posted
    Then to get all of the records in the Stock Table with the Location ID as a Foreign key and put the the Name of the Item of stock (that was found) into a separate Combobox.
    then you posted:

    I have 3 tables:

    Table "DepartmentTable" contains: "DepartmentID", "ClientID", "DepartmentName"
    Table "LocationTable" contains: "LocationID", "LocationID"
    Table "ClientTable" contains: "ClientID", "ClientName"
    I assume in LocationTable you meant "LocationID", "LocationName". So how many tables are you using and what are their relationships? Where does Stock fit in here?

    Post a screen shot of your relationship diagram from your Access DB.
    Last edited by CoachBarker; Feb 9th, 2012 at 08:19 AM. Reason: add a comment
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jan 2011
    Posts
    139

    Re: SQL string problem... take a value, take the ID, search for the ID

    Yes, your correct to assume that. LocationID was location Name. I didn't mean to write that!

    I don't have any relationships in Access..i'm trying to create them all in the application. Is that wrong?!?

    OK so what the mishap, and it is totally my fault. I'M going to use the same code several times but adapt it. Originally i was going to start with STOCK and Location... (its a many to many relationship and therefore we need another table to make it 3rd normal form). On that basis i asked for help. It was then that i realized that If i started with the Client and also the Department then I could knock out a larger chunk. If that makes sense.

    Sorry my written English is TERRIBLE! you can see that i failed my English GCSE

  17. #17
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: SQL string problem... take a value, take the ID, search for the ID

    Quote Originally Posted by KingJ View Post
    Ok - so how would i use the index on the field name and put that into something that I can actually use. I have tried the Code that Evil suggested no luck
    What didn't work?

  18. #18
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: SQL string problem... take a value, take the ID, search for the ID

    I was taught to always set the relationships in access when creating a db. If you don't set the relationships how is the db functional, a foreign key in a table doesn't mean anything if you don't create a relationship between the tables.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  19. #19
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: SQL string problem... take a value, take the ID, search for the ID

    I'm not as strict about the relationships in the DB, though I think it is often a good idea. In this case, it isn't really the problem.

    What I would do would be this:

    On form load, fill a datatable with the LocationID and LocationName. This will be used for the display of the combobox, so that datatable can just sit there unchanged (unless items are added to it). I would then bind the datatable to the combobox, setting the ValueMember as the LocationID and the DisplayMember as the Location Name. This gives you either the LocationID or the LocationName when somebody selects something (depending on whether you use SelectedItem (though this will have to be cast to a datarowview, I think), SelectedValue, or whatever the other one is).

    Once you have access to the ID, which is probably the Key field, I'd use that in a JOIN if a JOIN is necessary, or use that in the WHERE clause if the JOIN was not necessary.
    My usual boring signature: Nothing

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