systems_ax
asked on
SELECT statement with multiple inner joins in visual basic 2005
I am developing with visual basic 2005 and everything was working great until I divided my 1 very large access table into other 5 tables. I have 2 forms, 1 form is the search form with a datagrid that populates multiple record data. When I double click on a particular record, the following code executes successfully when I am reading from only 1 table:
Private Sub dgvLastNames_CellDoubleCli ck(ByVal sender As Object, ByVal e As System.Windows.Forms.DataG ridViewCel lEventArgs ) Handles dgvLastNames.CellDoubleCli ck
If dgvLastNames.CurrentRow.In dex > -1 Then
Dim editForm As New frmAddClient
editForm.ClientId = CType(dgvLastNames.Current Row.Cells( 0).Value.T oString(), Int32)
Me.Dispose()
'If editForm.WindowState = FormWindowState.Minimized Then
' editForm.WindowState = FormWindowState.Normal
'End If
editForm.ShowDialog()
RetrievClients()
Else : MessageBox.Show("please select a row for editing.", "No Row selected", MessageBoxButtons.OK, MessageBoxIcon.Information )
End If
End Sub
Private Sub RetrievClients()
Dim conn As New OleDbConnection("Provider= Microsoft. Jet.OLEDB. 4.0;Data Source=test.mdb")
Dim adapter As New OleDbDataAdapter("select * from Clients", conn)
Dim dt As New DataTable("Clients")
adapter.Fill(dt)
dgvLastNames.DataSource = dt
End Sub
on the form where the record populates executes this code:
Private Sub RetrievClientsDetails()
Dim conn As New OleDbConnection("Provider= Microsoft. Jet.OLEDB. 4.0;Data Source=test.mdb")
Dim command As New OleDbCommand("select * from Clients where ClientId=" & _clientID, conn)
conn.Open()
Dim contactreader As OleDbDataReader = command.ExecuteReader
contactreader.Read()
Me.txtFirst.Text = contactreader("ClientFirst Name").ToS tring
Me.txtMiddle.Text = contactreader("ClientMiddl eName").To String
Me.txtLast.Text = contactreader("ClientLastN ame").ToSt ring
Me.txtCompany.Text = contactreader("ClientCompa ny").ToStr ing
Me.txtAddress1.Text = contactreader("ClientStree tAddress1" ).ToString
Me.txtAddress2.Text = contactreader("ClientStree tAddress2" ).ToString
Me.txtID.Text = contactreader("ClientIdNum ber").ToSt ring
Me.txtCity.Text = contactreader("ClientCity" ).ToString
Me.txtState.Text = contactreader("ClientState ").ToStrin g
Me.txtPostal.Text = contactreader("ClientPosta lCode").To String
Me.txtCountry.Text = contactreader("ClientCount ry").ToStr ing
Me.txtDate.Text = contactreader("ClientDateo fBirth").T oString
Me.txtPhone1.Text = contactreader("ClientTelep honeNumber One").ToSt ring
Me.txtPhone2.Text = contactreader("ClientTelep honeNumber Two").ToSt ring
Me.txtFax.Text = contactreader("ClientFaxNu mber").ToS tring
Me.txtEmail.Text = contactreader("ClientEmail ").ToStrin g
conn.Close()
End Sub
HOWEVER WHEN I TRY to join multiple table data for 1 client on which I double clicked within the grid it is not working:
this is what I tried for both and replaced the current select statement:
please offer a suggestion.
Private Sub dgvLastNames_CellDoubleCli
If dgvLastNames.CurrentRow.In
Dim editForm As New frmAddClient
editForm.ClientId = CType(dgvLastNames.Current
Me.Dispose()
'If editForm.WindowState = FormWindowState.Minimized Then
' editForm.WindowState = FormWindowState.Normal
'End If
editForm.ShowDialog()
RetrievClients()
Else : MessageBox.Show("please select a row for editing.", "No Row selected", MessageBoxButtons.OK, MessageBoxIcon.Information
End If
End Sub
Private Sub RetrievClients()
Dim conn As New OleDbConnection("Provider=
Dim adapter As New OleDbDataAdapter("select * from Clients", conn)
Dim dt As New DataTable("Clients")
adapter.Fill(dt)
dgvLastNames.DataSource = dt
End Sub
on the form where the record populates executes this code:
Private Sub RetrievClientsDetails()
Dim conn As New OleDbConnection("Provider=
Dim command As New OleDbCommand("select * from Clients where ClientId=" & _clientID, conn)
conn.Open()
Dim contactreader As OleDbDataReader = command.ExecuteReader
contactreader.Read()
Me.txtFirst.Text = contactreader("ClientFirst
Me.txtMiddle.Text = contactreader("ClientMiddl
Me.txtLast.Text = contactreader("ClientLastN
Me.txtCompany.Text = contactreader("ClientCompa
Me.txtAddress1.Text = contactreader("ClientStree
Me.txtAddress2.Text = contactreader("ClientStree
Me.txtID.Text = contactreader("ClientIdNum
Me.txtCity.Text = contactreader("ClientCity"
Me.txtState.Text = contactreader("ClientState
Me.txtPostal.Text = contactreader("ClientPosta
Me.txtCountry.Text = contactreader("ClientCount
Me.txtDate.Text = contactreader("ClientDateo
Me.txtPhone1.Text = contactreader("ClientTelep
Me.txtPhone2.Text = contactreader("ClientTelep
Me.txtFax.Text = contactreader("ClientFaxNu
Me.txtEmail.Text = contactreader("ClientEmail
conn.Close()
End Sub
HOWEVER WHEN I TRY to join multiple table data for 1 client on which I double clicked within the grid it is not working:
this is what I tried for both and replaced the current select statement:
please offer a suggestion.
Dim command As New OleDbCommand("select * from tblClients INNER JOIN tblClientDetails ON tblClients.ClientId = tblClientDetails.ClientId where ClientId=" & _clientID, conn)
error that I receive is:
The specified field 'ClientId' could refer to more than one table listed in the FROM clause of your SQL statement.
ASKER
angelIII,
thank you for replying.
can you please explain why there are multiples of ClientId's, sorry I am just a novice.
thank you
thank you for replying.
can you please explain why there are multiples of ClientId's, sorry I am just a novice.
thank you
>can you please explain why there are multiples of ClientId's,
based on your join condition:
>ON c.ClientId = cd.ClientId
you have the same column name in both tables.
based on your join condition:
>ON c.ClientId = cd.ClientId
you have the same column name in both tables.
ASKER
angelIII,
thank you I do not understand what this line means:
Dim command As New OleDbCommand("select c.ClientID, cd.ClientID cdClientID ... ***why is there cd.ClientID and cdClientID.
thank you
thank you I do not understand what this line means:
Dim command As New OleDbCommand("select c.ClientID, cd.ClientID cdClientID ... ***why is there cd.ClientID and cdClientID.
thank you
the cdClientID would be the column alias for the ClientID from table CustomerDetails.
that alias would make sure you get 2 distinct names, while the tables initially have the same column name.
that alias would make sure you get 2 distinct names, while the tables initially have the same column name.
ASKER
angellll,
so,the command will look this way:
Dim command As New OleDbCommand("select c.ClientID, cd.ClientID from tblClients c INNER JOIN tblClientDetails cd ON c.ClientId = cd.ClientId where c.ClientId=" & _clientID, conn)
thank you.
so,the command will look this way:
Dim command As New OleDbCommand("select c.ClientID, cd.ClientID from tblClients c INNER JOIN tblClientDetails cd ON c.ClientId = cd.ClientId where c.ClientId=" & _clientID, conn)
thank you.
Try adding the table name to the ClientId in the where clause
Dim command As New OleDbCommand("select * from tblClients INNER JOIN tblClientDetails ON tblClients.ClientId = tblClientDetails.ClientId where tblClients.ClientId=" & _clientID, conn)
>so,the command will look this way:
>Dim command As New OleDbCommand("select c.ClientID, cd.ClientID from tblClients c INNER JOIN tblClientDetails cd ON >c.ClientId = cd.ClientId where c.ClientId=" & _clientID, conn)
no, because you keep then 2 return columns with the same name.
this would work (returning only 1 column with the name ClientID):
Dim command As New OleDbCommand("select c.ClientID from tblClients c INNER JOIN tblClientDetails cd ON c.ClientId = cd.ClientId where c.ClientId=" & _clientID, conn)
and this would work (returning 2 columns, but the second one having originally the same name with a different one -> alias name)
Dim command As New OleDbCommand("select c.ClientID, cd.ClientID as cdClient from tblClients c INNER JOIN tblClientDetails cd ON c.ClientId = cd.ClientId where c.ClientId=" & _clientID, conn)
>Dim command As New OleDbCommand("select c.ClientID, cd.ClientID from tblClients c INNER JOIN tblClientDetails cd ON >c.ClientId = cd.ClientId where c.ClientId=" & _clientID, conn)
no, because you keep then 2 return columns with the same name.
this would work (returning only 1 column with the name ClientID):
Dim command As New OleDbCommand("select c.ClientID from tblClients c INNER JOIN tblClientDetails cd ON c.ClientId = cd.ClientId where c.ClientId=" & _clientID, conn)
and this would work (returning 2 columns, but the second one having originally the same name with a different one -> alias name)
Dim command As New OleDbCommand("select c.ClientID, cd.ClientID as cdClient from tblClients c INNER JOIN tblClientDetails cd ON c.ClientId = cd.ClientId where c.ClientId=" & _clientID, conn)
ASKER
DelmarIT,
works great. How can I join more tables into the present select command?
thank you.
works great. How can I join more tables into the present select command?
thank you.
You can add more joins to the statement but it will not return rows if one of the tables does not have a match. This page has a good explanation http://www.w3schools.com/Sql/sql_join.asp
Dim command As New OleDbCommand("select * from tblClients INNER JOIN tblClientDetails ON tblClients.ClientId = tblClientDetails.ClientId INNER JOIN tblClientOrders ON tblClients.ClientId = tblClientOrders.ClientId where tblClients.ClientId=" & _clientID, conn)
ASKER
DelmarIT,
it is not working once I start adding more inner joins.
it is not working once I start adding more inner joins.
ASKER
angelIII,
are you familiar with multiple inner joins, I need at least 5 as I am trying to pull data from 5 tables for a client.
Please any ideas.
are you familiar with multiple inner joins, I need at least 5 as I am trying to pull data from 5 tables for a client.
Please any ideas.
>angelIII,
>are you familiar with multiple inner joins, I need at least 5 as I am trying to pull data from 5 tables for a client.
yes, very.
tip: get the SQL running in sql server directly, best in a stored procedure for example, so that you vb code will just call the procedure with some parameters.
that will make you collect all the benefits:
* rapid development
* quicker code
* smaller code
now, in regards to the multiple joins, you would need to clarify the problem(s) you have.
like, what (error) messages your get with which query...
>are you familiar with multiple inner joins, I need at least 5 as I am trying to pull data from 5 tables for a client.
yes, very.
tip: get the SQL running in sql server directly, best in a stored procedure for example, so that you vb code will just call the procedure with some parameters.
that will make you collect all the benefits:
* rapid development
* quicker code
* smaller code
now, in regards to the multiple joins, you would need to clarify the problem(s) you have.
like, what (error) messages your get with which query...
ASKER
angelIII,
I am working with access 2007 database unfortunately. I just tried doing an inner join on 3 tables, which works fine IF I ACTUALLY ENTER DATA. If i try to open a client with no such data, the code breaks: here is my inner join:
is there a problem with my inner join?
thank you
I am working with access 2007 database unfortunately. I just tried doing an inner join on 3 tables, which works fine IF I ACTUALLY ENTER DATA. If i try to open a client with no such data, the code breaks: here is my inner join:
is there a problem with my inner join?
thank you
Dim command As New OleDbCommand("select * from (tblClients INNER JOIN tblClientDetail ON tblClients.ClientId = tblClientDetail.ClientId) INNER JOIN tblClientDetailOne on tblClients.ClientId=tblClientDetailOne.ClientId where tblClients.ClientId=" & _clientID, conn)
>if i try to open a client with no such data, the code breaks
I assume you mean: there is no data returned if there is no data in neither tblClientDetailOne nor tblClientDetail.
in which case, change INNER JOIN into LEFT OUTER JOIN
I assume you mean: there is no data returned if there is no data in neither tblClientDetailOne nor tblClientDetail.
in which case, change INNER JOIN into LEFT OUTER JOIN
ASKER
angellll,
I have tried outer joining 3 tables with no issues, only when I add one more outer join, my code errors out and says that:
"syntax error in FROM clause"
what Am i doing wrong.
I have tried outer joining 3 tables with no issues, only when I add one more outer join, my code errors out and says that:
"syntax error in FROM clause"
what Am i doing wrong.
THIS ONE WORKS PERFECT
dim command As New OleDbCommand("select * from (tblClients left outer JOIN tblClientDetail1 ON tblClients.ClientId = tblClientDetail1.ClientId) left outer JOIN tblClientDetail2 on tblClients.ClientId=tblClientDetail2.ClientId
where tblClients.ClientId=" & _clientID, conn)
THIS IS HAS SYNTAX ERRORS:
dim command As New OleDbCommand("select * from (tblClients left outer JOIN tblClientDetail1 ON tblClients.ClientId = tblClientDetail1.ClientId) (left outer JOIN tblClientDetail2 on tblClients.ClientId=tblClientDetail2.ClientId) (left outer JOIN tblClientDetail3 on tblClients.ClientId=tblClientDetail3.ClientId) where tblClients.ClientId=" & _clientID, conn)
please try to move the " ( " like this:
dim command As New OleDbCommand("select * from (((tblClients left outer JOIN tblClientDetail1 ON tblClients.ClientId = tblClientDetail1.ClientId) left outer JOIN tblClientDetail2 on tblClients.ClientId=tblClientDetail2.ClientId) left outer JOIN tblClientDetail3 on tblClients.ClientId=tblClientDetail3.ClientId) where tblClients.ClientId=" & _clientID, conn)
ASKER
angelIII,
it says that there is "syntax error (missing operator) in my query. I pasted you code into my program.
thank you
it says that there is "syntax error (missing operator) in my query. I pasted you code into my program.
thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
angelIII,
you are a genious. So, I just add an extra "(" to from (((tblClients as I add more joins?
thank you
you are a genious. So, I just add an extra "(" to from (((tblClients as I add more joins?
thank you
>So, I just add an extra "(" to from (((tblClients as I add more joins?
yes, and also () around the ON <condition>, ie ON ( <condition> )
yes, and also () around the ON <condition>, ie ON ( <condition> )
ASKER
thank you so much.
it works great.
it works great.
which is the problem.
so, what you need to do is do something like this, putting the field names explicitly, including putting column alias names ...
Open in new window