Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Data from two tables

Data from two tables

Scheduled Pinned Locked Moved Visual Basic
databasehelpquestioncareer
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chaos Machine
    wrote on last edited by
    #1

    Hello everybody. I am trying to retrive data from a Access Database containing two tables. 1) tblKeyword 2) tblArea In tblKeyword there are two attributes: a) Keyword and b) AreaName in tblArea there are couple including AreaName which is the connection between the two tables. I am trying to use the following code: ======================================================================== Dim strConnection As String = OleDbConnection1.ConnectionString Dim connect As New OleDbConnection(strConnection) Dim test As String Dim te As String Dim SQLString As String connect.Open() te = cmbKeywords.Items(cmbKeywords.SelectedIndex) On Error Resume Next SQLString = "SELECT tblKeywords.Keyword, tblArea.AreaName, tblArea.StartDate, tblArea.JobNumber FROM tblKeywords INNER JOINT tblArea ON tblKeywords.AreaName = tblArea.AreaName WHERE tblKeyWords.Keyword =" & "'" & te & "'" Dim cmd As New OleDbCommand(SQLString, connect) Dim reader As OleDbDataReader = cmd.ExecuteReader() While reader.Read() txtAreaName.Text = reader.GetValue(1) End While ======================================================================== The SQL command goes through OK but the reader.Read is causing an error. How can i retrive the data from that SQL. I am lost. :confused: I know that the GetValue gets a value from attributes in one table. But with the inner joint I need values from both tables. Any help would be appreaciated. Thank you.. Still trying to find the way

    J 1 Reply Last reply
    0
    • C Chaos Machine

      Hello everybody. I am trying to retrive data from a Access Database containing two tables. 1) tblKeyword 2) tblArea In tblKeyword there are two attributes: a) Keyword and b) AreaName in tblArea there are couple including AreaName which is the connection between the two tables. I am trying to use the following code: ======================================================================== Dim strConnection As String = OleDbConnection1.ConnectionString Dim connect As New OleDbConnection(strConnection) Dim test As String Dim te As String Dim SQLString As String connect.Open() te = cmbKeywords.Items(cmbKeywords.SelectedIndex) On Error Resume Next SQLString = "SELECT tblKeywords.Keyword, tblArea.AreaName, tblArea.StartDate, tblArea.JobNumber FROM tblKeywords INNER JOINT tblArea ON tblKeywords.AreaName = tblArea.AreaName WHERE tblKeyWords.Keyword =" & "'" & te & "'" Dim cmd As New OleDbCommand(SQLString, connect) Dim reader As OleDbDataReader = cmd.ExecuteReader() While reader.Read() txtAreaName.Text = reader.GetValue(1) End While ======================================================================== The SQL command goes through OK but the reader.Read is causing an error. How can i retrive the data from that SQL. I am lost. :confused: I know that the GetValue gets a value from attributes in one table. But with the inner joint I need values from both tables. Any help would be appreaciated. Thank you.. Still trying to find the way

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Chaos Machine wrote: The SQL command goes through OK What you mean is that you are able to create the SqlCommand, however no validation is done on the provided SQL string provided at this point. Chaos Machine wrote: but the reader.Read is causing an error At the point of opening the datareader, the SQL provided is executed, so any syntactical errors will be thrown here. Chaos Machine wrote: INNER JOINT Now, if that were my mistake it would've been freidian ;) Oh, and another pointer: Chaos Machine wrote: I know that the GetValue gets a value from attributes in one table. But with the inner joint I need values from both tables. By doing an join, you are effectively creating one table (albeit a virtual table that doesnt exist in the DB). Your single table will have the columns you specify in the select list: Chaos Machine wrote: SELECT tblKeywords.Keyword, tblArea.AreaName, tblArea.StartDate, tblArea.JobNumber Hope it helps.

      C 1 Reply Last reply
      0
      • J J4amieC

        Chaos Machine wrote: The SQL command goes through OK What you mean is that you are able to create the SqlCommand, however no validation is done on the provided SQL string provided at this point. Chaos Machine wrote: but the reader.Read is causing an error At the point of opening the datareader, the SQL provided is executed, so any syntactical errors will be thrown here. Chaos Machine wrote: INNER JOINT Now, if that were my mistake it would've been freidian ;) Oh, and another pointer: Chaos Machine wrote: I know that the GetValue gets a value from attributes in one table. But with the inner joint I need values from both tables. By doing an join, you are effectively creating one table (albeit a virtual table that doesnt exist in the DB). Your single table will have the columns you specify in the select list: Chaos Machine wrote: SELECT tblKeywords.Keyword, tblArea.AreaName, tblArea.StartDate, tblArea.JobNumber Hope it helps.

        C Offline
        C Offline
        Chaos Machine
        wrote on last edited by
        #3

        Hello J4amieC, thanks for replying. I am still at a loss of what to do. Can you please show me with a bit of code how I can retrive the informations that i have collect from the SQL command? I am trying from books and online info, but still nothing. Thank you for your help. Still trying to find the way

        J 1 Reply Last reply
        0
        • C Chaos Machine

          Hello J4amieC, thanks for replying. I am still at a loss of what to do. Can you please show me with a bit of code how I can retrive the informations that i have collect from the SQL command? I am trying from books and online info, but still nothing. Thank you for your help. Still trying to find the way

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          OK, my comment was that you had a error in your SQLString (you used INNER JOINT instead of INNER JOIN) it should read: SQLString = "SELECT tblKeywords.Keyword, tblArea.AreaName, tblArea.StartDate, tblArea.JobNumber FROM tblKeywords INNER JOIN tblArea ON tblKeywords.AreaName = tblArea.AreaName WHERE tblKeyWords.Keyword ='" & te & "'" Now when you execute this string you will have the following columns: 0: Keyword (from tblKeywords) 1: AreaName (from tblArea) 2: StartDate (from tblArea) 3: JobNumber (from tblArea) so if you use: reader.GetValue(0) you will get the Keyword column.

          C 1 Reply Last reply
          0
          • J J4amieC

            OK, my comment was that you had a error in your SQLString (you used INNER JOINT instead of INNER JOIN) it should read: SQLString = "SELECT tblKeywords.Keyword, tblArea.AreaName, tblArea.StartDate, tblArea.JobNumber FROM tblKeywords INNER JOIN tblArea ON tblKeywords.AreaName = tblArea.AreaName WHERE tblKeyWords.Keyword ='" & te & "'" Now when you execute this string you will have the following columns: 0: Keyword (from tblKeywords) 1: AreaName (from tblArea) 2: StartDate (from tblArea) 3: JobNumber (from tblArea) so if you use: reader.GetValue(0) you will get the Keyword column.

            C Offline
            C Offline
            Chaos Machine
            wrote on last edited by
            #5

            Thank you J4amieC it worked!!! Thank you so much. Now i have to expand this so when i have two areas with the same keyword a list is populated. Hmmm lets see if i can do it!! LOL Thank you again for your help...:-D Still trying to find the way

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups