Results 1 to 10 of 10

Thread: Selecting from different tables. Pls Help..Thanx

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    100

    Selecting from different tables. Pls Help..Thanx

    Hi,

    I have a doubt. I am using microsoft access as my database. I want to extract various information from different tables based on one primary key, the user id. What i want is to extract information from the two tables based on the user id.

    I also want deatils displayed only if present date corresponds to the date in another table. The matchNo corresponding to that date should also be the same as that of corresponding to the userid.

    I have written the following code but i am not sure if i am correct. I hope someone can guide me. Thanx

    P
    VB Code:
    1. rivate Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    2.         Dim dbCon As New OleDbConnection(ConnectionString)
    3.         Dim dbCommand As OleDbCommand
    4.         Dim MatricNo As String
    5.         Dim day As Date
    6.         Dim MatchNo As New Integer
    7.         MatricNo = txtPMatricNo.Text
    8.         day = day.Date
    9.         dbCon.Open()
    10.         dbCommand = New OleDbCommand("SELECT * FROM MatchesWSinglesRound1,MatchesMDoublesRound1,Matches WHERE MatchesMDoublesRound1.MatricNo = '" & MatricNo & "', MatchesWSinglesRound1 = '" & MatricNo & "',Matches.MatchDay = '" & day & "',Matches.MatchNo = '" & MatchesWSinglesRound1.MatchNo & "',Matches.MatchNo = '" & MatchesMDoublesRound1.MatchNo & "'", dbCon)
    11.         dbCommand.ExecuteNonQuery()
    12.         dbCommand.Dispose()
    13.         dbCon.Close()
    14.     End Sub

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Selecting from different tables. Pls Help..Thanx

    Hi Sand !

    First of all, your query is not good. You may use MS Access Query builder to be assisted in building it or event the Configuation wizard on the ADO.NET DataAdapter

    In the WHERE clause, separate each Critera with the AND or OR keyword
    you may also look on MSDN web site to learn how it work.

    And you should'nt use ExecuteNonQuery to run a query, ExecuteNonQuery is for UPDATE, DELETE, or INSERT query, everything that do non return data

    here is a function that i've build to return a DataSet from a SQL Query, note that this fonction is for SQL Server database, but it do not change much for other DB


    VB Code:
    1. Public Function GetDataSet(ByVal pCommandText As String, ByVal pCommandType As CommandType, ByVal pParamArray As ArrayList) As DataSet
    2.  
    3.         Dim cmd As SqlCommand = New SqlCommand
    4.         Dim Param As SqlParameter = New SqlParameter
    5.         Dim ds As DataSet = New DataSet
    6.         Dim intBoucle As Integer
    7.  
    8.         Dim dad As SqlDataAdapter
    9.  
    10.         For intBoucle = 0 To pParamArray.Count - 1
    11.             cmd.Parameters.Add(pParamArray(intBoucle))
    12.         Next
    13.  
    14.         cmd.Connection = New SqlClient.SqlConnection(conConnection.ConnectionString)
    15.  
    16.         cmd.CommandText = pCommandText
    17.         cmd.CommandType = pCommandType
    18.  
    19.         dad = New SqlDataAdapter(cmd)
    20.  
    21.         dad.Fill(ds)
    22.  
    23.         Return ds
    24.  
    25.     End Function
    Using VS 2010 on Fw4.0

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    100

    Re: Selecting from different tables. Pls Help..Thanx

    Hi,

    I dun quite understand your code. Could you pls explain. Thanx

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    100

    Re: Selecting from different tables. Pls Help..Thanx

    I believe i figured out what you actually meant, you are trying to say the following?

    VB Code:
    1. Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    2.         Dim dbCon As New OleDbConnection(ConnectionString)
    3.         Dim dbAdapter As New OleDbDataAdapter
    4.         Dim MatricNo As String
    5.         Dim day As Date
    6.         Dim MatchNo As New Integer
    7.         MatricNo = txtPMatricNo.Text
    8.         day = day.Date
    9.         dbAdapter.SelectCommand = New OleDbCommand("SELECT * FROM MatchesWSinglesRound1,MatchesMDoublesRound1,Matches WHERE MatchesMDoublesRound1.MatricNo = '" & MatricNo & "', MatchesWSinglesRound1 = '" & MatricNo & "',Matches.MatchDay = '" & day & "',Matches.MatchNo = '" & MatchesWSinglesRound1.MatchNo & "'", dbCon)
    10.     End Sub

    However, I can't figure out how to extract only the data 'MatchNo' in the 'Matches' table corresponds to that of the 'MatchesWSinglesRound1' and 'MatchesMDoublesRound1' tables?

  5. #5
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Selecting from different tables. Pls Help..Thanx

    Sure ! No problem!
    Here a version simplified


    If you need more info on how ADO.NET work i'll suggest you to visit this thread

    VB Code:
    1. Public Function GetDataSet(ByVal pSQLCommand As String) As DataSet
    2.         '**********************************************
    3.         ' Parameter
    4.         '**********************************************
    5.         ' pSQLCommand -> The SQL Query to run
    6.         '
    7.  
    8.         ' Define a new command object
    9.         Dim cmd As SqlCommand = New SqlCommand
    10.  
    11.         ' define the DataSet that will be return
    12.         Dim ds As DataSet = New DataSet
    13.  
    14.         ' Define de DataAdapter that will handle the command
    15.         Dim dad As SqlDataAdapter
    16.  
    17.  
    18.         ' Set the BD connection to the Command object
    19.         cmd.Connection = New SqlClient.SqlConnection(conConnection.ConnectionString)
    20.  
    21.         ' Set the SQL Query to run to the command  object
    22.         cmd.CommandText = pSQLCommand
    23.         ' Set the type of command to be run; DirectTable, Text, or StoreProc
    24.         cmd.CommandType = CommandType.Text
    25.  
    26.         'Set the command to the DataAdapter so , the DataADatpetr will know witch SELECT command to run
    27.         dad = New SqlDataAdapter(cmd)
    28.  
    29.         ' Lanch the Fill method to fill the DataSet
    30.         dad.Fill(ds)
    31.  
    32.         ' return the DataSet to the Calling procedure
    33.         Return ds
    34.  
    35.     End Function
    Using VS 2010 on Fw4.0

  6. #6
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Selecting from different tables. Pls Help..Thanx

    Quote Originally Posted by sand
    I believe i figured out what you actually meant, you are trying to say the following?

    VB Code:
    1. Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    2.         Dim dbCon As New OleDbConnection(ConnectionString)
    3.         Dim dbAdapter As New OleDbDataAdapter
    4.         Dim MatricNo As String
    5.         Dim day As Date
    6.         Dim MatchNo As New Integer
    7.         MatricNo = txtPMatricNo.Text
    8.         day = day.Date
    9.         dbAdapter.SelectCommand = New OleDbCommand("SELECT * FROM MatchesWSinglesRound1,MatchesMDoublesRound1,Matches WHERE MatchesMDoublesRound1.MatricNo = '" & MatricNo & "', MatchesWSinglesRound1 = '" & MatricNo & "',Matches.MatchDay = '" & day & "',Matches.MatchNo = '" & MatchesWSinglesRound1.MatchNo & "'", dbCon)
    10.     End Sub

    However, I can't figure out how to extract only the data 'MatchNo' in the 'Matches' table corresponds to that of the 'MatchesWSinglesRound1' and 'MatchesMDoublesRound1' tables?

    You're neer the true with this, but the SQL Statement is but good ... what a sec i'll be back with a corrected one shortly
    Using VS 2010 on Fw4.0

  7. #7
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Selecting from different tables. Pls Help..Thanx

    Here is with the SQL Corrected, i can't tell if it bug free, cause i can't test it

    but it is a idea ...

    VB Code:
    1. Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    2.         Dim dbCon As New OleDbConnection(ConnectionString)
    3.         Dim dbAdapter As New OleDbDataAdapter
    4.         Dim ds As DataSet = New DataSet
    5.  
    6.         Dim MatricNo As String
    7.         Dim day As Date
    8.         Dim MatchNo As New Integer
    9.         Dim SQL As String
    10.        
    11.         MatricNo = txtPMatricNo.Text
    12.         day = day.Date
    13.  
    14.         SQL = "SELECT * "        ' Here you should specifie exaclty witch field you wich
    15.         SQL &= " FROM "
    16.         SQL &= "    MatchesWSinglesRound1 ,"
    17.         SQL &= "    MatchesMDoublesRound1,"
    18.         SQL &= "    Matches "
    19.         SQL &= " WHERE "
    20.         SQL &= "    MatchesMDoublesRound1.MatricNo = MatchesWSinglesRound1.MatricNo "
    21.         SQL &= "    AND Matches.MatchNo = MatchesWSinglesRound1.MatchNo"
    22.         SQL &= "    AND MatchesMDoublesRound1.MatricNo = '" & MatricNo & "'"
    23.         SQL &= "    AND Matches.MatchDay = '" & day & "'"
    24.  
    25.         dbAdapter.SelectCommand = New OleDbCommand(SQL, dbCon)
    26.         dbAdapter.fill(ds)
    27. ' Now you may use ds to handle the data return by the query
    28.  
    29.     End Sub
    Using VS 2010 on Fw4.0

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    100

    Re: Selecting from different tables. Pls Help..Thanx

    Hi,
    Sorry for the late reply.

    So if i want to display a field from any one of the tables stated in the sql statement i just write as follows?

    txtMatchDate.Text = Matches.MatchDay

    The above should display the match day based on the select statement right? However the word in red is highlighted. Is there another way of displaying the data in textbox instead of table?

  9. #9
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Selecting from different tables. Pls Help..Thanx

    You may access the data in the DataSet called "ds" by using Item

    ds.Tables(0).Item("FieldName")


    I realy suggest you to look at this tread to figure a bit how ADO.NET work
    Last edited by Zakary; Jun 20th, 2005 at 07:22 AM.
    Using VS 2010 on Fw4.0

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    100

    Re: Selecting from different tables. Pls Help..Thanx

    Hi,

    I read the thread as suggested. I installed the examples, but i can't find it in my comp after installing it. How do i access the files after installing it?

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