Error al añadir instrucción SQL evento combo

Perdonad que vuelva sobre este tema pero cuando parecía que ya lo tenía solucionado me encuentro con que no consigo acomodar el código que me pasó Sveinbjorn El Rojo a mi caso exacto.

Añado a la consulta CPorProductos varios campos de tabla diferentes, y después copio la instrucción SQL y la pego en el evento de cboProducto y de cboDetalle y me da el error: "Se encontraron caracteres después del final de la instrucción SQL"

Concretamente la instrucción que pongo para cboProducto es:
Private Sub cboProducto_AfterUpdate()

'Actualizas el segundo combo en función del primero
Me.cboDetalle.RowSource = "SELECT IdDetalle, DetalleProdUCTO, IDProdUCTO FROM TABLADetalle WHERE IDProdUCTO=" & Me.cboProducto & " ORDER BY

DetalleProdUCTO"
Me.cboDetalle.Requery
'Recalculas el surformulario
If IsNull(Me.cboProducto) Then
    'Si dejas el combo en blanco, muestra todos los registros
    Me.PorProductosSubformulario.Form.RecordSource = "SELECT TABLAGastos.Id, TABLAGastos.Fecha, TablaProveedores.PROVEEDOR, TABLAProductos.Producto,

TABLADetalle.DetalleProdUCTO, " _
    & "TABLAGastos.Cantidad, TablaUnidades.UNIDAD, TABLAGastos.PRECIOUNITARIO, TABLAGastos.PRECIOTOTAL, TABLAGastos.OBSERVACIONES,

TABLAGastos.IDDetalle, TABLAGastos.IDprodUCTO " _
    & "FROM TablaUnidades RIGHT JOIN (TablaProveedores RIGHT JOIN (TABLAProductos RIGHT JOIN (TABLADetalle RIGHT JOIN TABLAGastos ON

TABLADetalle.IdDetalle = TABLAGastos.IDDetalle) " _
    & "ON TABLAProductos.IDProdUCTO = TABLAGastos.IDprodUCTO) ON TablaProveedores.IDPROVEEDOR = TABLAGastos.IDPROVEEDOR) ON TablaUnidades.IDUNIDADES =

TABLAProductos.IDUNIDAD;"

Else

    'Si no, muestra los registros de ese producto (WHERE IDProd=Me.cboProducto)
    Me.PorProductosSubformulario.Form.RecordSource = "SELECT TABLAGastos.Id, TABLAGastos.Fecha, TablaProveedores.PROVEEDOR, TABLAProductos.Producto,

TABLADetalle.DetalleProdUCTO, " _
    & "TABLAGastos.Cantidad, TablaUnidades.UNIDAD, TABLAGastos.PRECIOUNITARIO, TABLAGastos.PRECIOTOTAL, TABLAGastos.OBSERVACIONES,

TABLAGastos.IDDetalle, TABLAGastos.IDprodUCTO " _
    & "FROM TablaUnidades RIGHT JOIN (TablaProveedores RIGHT JOIN (TABLAProductos RIGHT JOIN (TABLADetalle RIGHT JOIN TABLAGastos ON

TABLADetalle.IdDetalle = TABLAGastos.IDDetalle) " _
    & "ON TABLAProductos.IDProdUCTO = TABLAGastos.IDprodUCTO) ON TablaProveedores.IDPROVEEDOR = TABLAGastos.IDPROVEEDOR) ON TablaUnidades.IDUNIDADES =

TABLAProductos.IDUNIDAD;" _
    & "WHERE TABLAGastos.IDprodUCTO=" & Me.cboProducto
End If
'Actualizas el subformulario
Me.PorProductosSubformulario.Form.Requery

End Sub

y para cboDetalle:
Private Sub cboDetalle_AfterUpdate()

If IsNull(Me.cboProducto) Then
    Me.PorProductosSubformulario.Form.RecordSource = "SELECT TABLAGastos.Id, TABLAGastos.Fecha, TablaProveedores.PROVEEDOR, TABLAProductos.Producto,

TABLADetalle.DetalleProdUCTO, " _
    & "TABLAGastos.Cantidad, TablaUnidades.UNIDAD, TABLAGastos.PRECIOUNITARIO, TABLAGastos.PRECIOTOTAL, TABLAGastos.OBSERVACIONES,

TABLAGastos.IDDetalle, TABLAGastos.IDprodUCTO " _
    & "FROM TablaUnidades RIGHT JOIN (TablaProveedores RIGHT JOIN (TABLAProductos RIGHT JOIN (TABLADetalle RIGHT JOIN TABLAGastos ON

TABLADetalle.IdDetalle = TABLAGastos.IDDetalle) " _
    & "ON TABLAProductos.IDProdUCTO = TABLAGastos.IDprodUCTO) ON TablaProveedores.IDPROVEEDOR = TABLAGastos.IDPROVEEDOR) ON TablaUnidades.IDUNIDADES =

TABLAProductos.IDUNIDAD; " _
    & "WHERE TABLAGastos.IDprodUCTO=" & Me.cboProducto
Else
    Me.PorProductosSubformulario.Form.RecordSource = "SELECT TABLAGastos.Id, TABLAGastos.Fecha, TablaProveedores.PROVEEDOR, TABLAProductos.Producto,

TABLADetalle.DetalleProdUCTO, " _
    & "TABLAGastos.Cantidad, TablaUnidades.UNIDAD, TABLAGastos.PRECIOUNITARIO, TABLAGastos.PRECIOTOTAL, TABLAGastos.OBSERVACIONES,

TABLAGastos.IDDetalle, TABLAGastos.IDprodUCTO " _
    & "FROM TablaUnidades RIGHT JOIN (TablaProveedores RIGHT JOIN (TABLAProductos RIGHT JOIN (TABLADetalle RIGHT JOIN TABLAGastos ON

TABLADetalle.IdDetalle = TABLAGastos.IDDetalle) " _
    & "ON TABLAProductos.IDProdUCTO = TABLAGastos.IDprodUCTO) ON TablaProveedores.IDPROVEEDOR = TABLAGastos.IDPROVEEDOR) ON TablaUnidades.IDUNIDADES =

TABLAProductos.IDUNIDAD; " _
    & "WHERE TABLAGastos.IDprodUCTO=" & Me.cboProducto & " AND TABLAGastos.IDDetalle=" & Me.cboDetalle
End If
Me.PorProductosSubformulario.Form.Requery

End Sub

1 Respuesta

Respuesta
1

Fíjate que antes de encadenar las partes del WHERE, en la SQL que copiaste tienes que borrarle el punto y coma, por eso te da el error.

Por ejemplo:

TABLAProductos.IDUNIDAD; " _
    & "WHERE TABLAGastos.IDprodUCTO=" & Me.cboProducto

Después de IDUNIDAD tienes que quitar el punto y coma, para poder encadenar la condición del WHERE.

Y así tendrás que borrar todos los puntos y coma que tengas en el código.

Muchas gracias, funciona perfecto.

Sois los mejores.

Un saludo.

Ten siempre en cuenta el borrar el punto y coma final cuando copies el código SQL de una consulta hecha en Access.
Y no te olvides de puntuar la respuesta, jejeje

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas