I'm updating a program from Visual Basic 6 to Visual Studio 2010 and, of course, I have founded a lot of problems so solve.
I'm using Access database with four tables with the same Key (Indice).
If I use the code as follow, I can get the last record from CodDekafix Table:
Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
Dim Con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\DEKAFIX\Consulta Dekafix\dekafix.mdb")
Dim cmd As New OleDbCommand()
Con.Open()
sql = "Select * From Indice Where CodDekafix=(Select max(CodDekafix) From Indice)"
But If I want to get all the results from all tables with the same Key (Indice) with the change as showed below the program doesn't work.
sql = "Select * from Indice, dekafix1, dekafix2, dekafix3" _
& " where CodDekafix=(Select max(CodDekafix) From Indice) and" _
& " Indice.CodDekafix = dekafix1.CodDekafix and" _
& " dekafix1.CodDekafix=dekafix2.CodDekafix and" _
& " dekafix2.CodDekafix=dekafix3.CodDekafix and" _
& " ORDER BY Indice.CodDekafix"
Your SQL code in the second code sample is invalid. When we strip out the formatting representing the string in VB code, you get this:
Select * from Indice, dekafix1, dekafix2, dekafix3
where CodDekafix=(Select max(CodDekafix) From Indice) and
Indice.CodDekafix = dekafix1.CodDekafix and
dekafix1.CodDekafix=dekafix2.CodDekafix and
dekafix2.CodDekafix=dekafix3.CodDekafix and
ORDER BY Indice.CodDekafix
You have two problems here:
1) There's an extra "and" before the ORDER BY clause. Remove it.
2) The first line of your WHERE clause has an ambiguous reference to CodDekafix
-- you need to specify what table that's coming from. Replacing CodDekafix
with Indice.CodDekafix
should do the trick.
sql = "Select * from Indice, dekafix1, dekafix2, dekafix3" _
& " where Indice.CodDekafix=(Select max(CodDekafix) From Indice) and" _
& " Indice.CodDekafix = dekafix1.CodDekafix and" _
& " dekafix1.CodDekafix=dekafix2.CodDekafix and" _
& " dekafix2.CodDekafix=dekafix3.CodDekafix" _
& " ORDER BY Indice.CodDekafix"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.