-1

我正在将一个程序从 Visual Basic 6 更新到 Visual Studio 2010,当然,我发现了很多问题,所以请解决。

我正在使用具有相同键(索引)的四个表的 Access 数据库。

如果我使用如下代码,我可以从 CodDekafix 表中获取最后一条记录:

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)"

但是,如果我想从具有相同键(索引)的所有表中获取所有结果,并进行如下所示的更改,则程序不起作用。

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"
4

1 回答 1

4

您在第二个代码示例中的 SQL 代码无效。当我们在 VB 代码中去掉表示字符串的格式时,你会得到:

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

你在这里有两个问题:

1)在 ORDER BY 子句之前有一个额外的“和”。去掉它。

2) WHERE 子句的第一行有一个模棱两可的引用 CodDekafix——您需要指定来自哪个表。替换应该可以解决问题CodDekafixIndice.CodDekafix

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"
于 2013-04-25T13:30:15.337 回答