1

I am developing a VB.NET program which has the need to join multiple tables and display them on a DataGridView.

I wrote this query:

SELECT m.idMedicos, m.Nombre, m.Apellido, h.idHistorial, h.CedulaP,
        r.*, rm.*,
        me.Nombre, me.Tipo, me.Administracion
   FROM Medicos m 
     INNER JOIN Historial h ON m.idMedicos = h.idMedicos 
     INNER JOIN Recetas r ON r.idHistorial = h.idHistorial 
     INNER JOIN RecetaMedica rm ON rm.idReceta = r.idReceta
     INNER JOIN Medicamentos me ON me.idMedicamento = rm.idMedicamento 
   WHERE h.CedulaP = $dato

($dato varies with each query)

When I run my program, it fails with the error: "Syntax error in FROM clause".

I tried to add a comma before each INNER, but the error persists.

Note: I am using a MS Access database, and I run all the queries with an OleDbDataAdapter.

Dim Consulta As New OleDb.OleDbDataAdapter("Select m.idMedicos, m.Nombre, m.Apellido, 
             h.idHistorial, h.CedulaP, r.*, rm.*, me.Nombre, me.Tipo, me.Administracion 
             FROM Medicos m INNER JOIN Historial h ON m.idMedicos = h.idMedicos, INNER JOIN Recetas r ON r.idHistorial = h.idHistorial, INNER JOIN RecetaMedica rm ON rm.idReceta = r.idReceta,
             INNER JOIN Medicamentos me ON me.idMedicamento = rm.idMedicamento 
             WHERE h.CedulaP ='" + dato + "'", conexion)

¿Can you guys give me a hand and see what happens here ? :/

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
TwoDent
  • 405
  • 7
  • 26

3 Answers3

0

I think that the problem is that you didn't include the me.idMedicamento in the initial SELECT so it's not recognizing it.

  • added what you said in the select ( and added another detail more ) and still not working .. "Select m.idMedicos, m.Nombre, m.Apellido,h.idMedicos, h.idHistorial, h.CedulaP, r.*, rm.*, me.idMedicamento, me.Nombre, me.Tipo, me.Administracion FROM Medicos m INNER JOIN Historial h ON m.idMedicos = h.idMedicos INNER JOIN Recetas r ON r.idHistorial = h.idHistorial INNER JOIN RecetaMedica rm ON rm.idReceta = r.idReceta INNER JOIN Medicamentos me ON me.idMedicamento = rm.idMedicamento WHERE h.CedulaP ='"+dato+"'" – TwoDent Oct 20 '15 at 14:42
0

Looking at this answer it would seem that you require to use parentheses to keep the joins logically separated. Here is a good read on the topic.

Your query should look like this when completed correctly:

Dim Consulta As New OleDb.OleDbDataAdapter("Select m.idMedicos, m.Nombre, m.Apellido, h.idHistorial, h.CedulaP, r.*, rm.*, me.Nombre, me.Tipo, me.Administracion FROM (((Medicos m  INNER JOIN Historial h ON m.idMedicos = h.idMedicos )   INNER JOIN Recetas r ON r.idHistorial = h.idHistorial ) INNER JOIN RecetaMedica rm ON rm.idReceta = r.idReceta )    INNER JOIN Medicamentos me ON me.idMedicamento = rm.idMedicamento WHERE h.CedulaP ='" + dato + "'", conexion)
Community
  • 1
  • 1
Karlta05
  • 165
  • 10
0

I already found a solution to this problem ( and indeed, long ago . )

I downloaded Fly Speed MySQL Query... It is a program in which you make your tables, relationships and others, that at the end , generates the correct code . (Sometimes you'll need to make some changes , if necessary ) .

http://www.activedbsoft.com/overview-querytool.html

Thanks guys for your help.

TwoDent
  • 405
  • 7
  • 26