Error while using a date as a parameter in MSQuery Macro

HarryCallaghan

Board Regular
Joined
Jan 30, 2013
Messages
50
Hello,

I am programing a macro in which i first make a query to get a table where one of the fields is a date, then for each row of this table I use this date as a parameter for another msquery where i get the final result. The problem im having is that i always get a "types dont match" error regarding the date and i don't know what to do. The first code correspond to the main macro and the second one is the query that fails. I would appreciate any help.

My program is this so far:

Code:
Public Sub Last_Price()
Dim FECHA As Date
Dim ARTICULO As String
Dim Fila As Integer




Query_Principal

Columns("H:H").Select
Selection.NumberFormat = "yyyy-mm-dd"
Range("C5").Select

Fila = 2

ARTICULO = ThisWorkbook.Worksheets("Resumen").Range("c" & Fila).Value

FECHA = ThisWorkbook.Worksheets("Resumen").Range("h" & Fila).Value

Do Until ARTICULO = ""

ARTICULO = ThisWorkbook.Worksheets("Resumen").Range("c" & Fila).Value

FECHA = ThisWorkbook.Worksheets("Resumen").Range("h" & Fila).Value



ThisWorkbook.Worksheets("LASTPRICE").Range("A1:B2").Clear

Query_Precio FECHA, ARTICULO

ThisWorkbook.Worksheets("Resumen").Range("j" & Fila).Value = ThisWorkbook.Worksheets("LASTPRICE").Range("A2").Value
ThisWorkbook.Worksheets("Resumen").Range("k" & Fila).Value = ThisWorkbook.Worksheets("LASTPRICE").Range("B2").Value

Fila = Fila + 1

Loop

Columns("i:i").Select
Selection.NumberFormat = "m/d/yyyy"
ThisWorkbook.Worksheets("LASTPRICE").Range("A1:B2").Clear


Code:
Sub Query_Precio(FECHA As Date, ARTICULO As String)
'
' Query_Precio Macro
'

'
    Sheets("LASTPRICE").Select
    With Sheets("LASTPRICE").ListObjects.Add(SourceType:=0, Source:= _
        "ODBC;DRIVER=SQL Server;SERVER=192.168.3.20,14567;UID=ServoEgimeno;PWD=$ervO2013;APP=2007 Microsoft Office system;WSID=JIGNACIO-PC" _
        , Destination:=Range("$A$1")).QueryTable
        .CommandText = Array( _
        "select top 1 (importe/uds), fecha from palbaranes inner join palbaranes_detalle on palbaranes.codigo = palbaranes_detalle.codigo and palbaranes.serie = palbaranes_detalle.serie where PALBARANES.FECHA < " & FECHA & " AND    articulo = '" & ARTICULO & "' order by PALBARANES.fecha desc")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Tabla_Consulta_desde_FUSION"
        .Refresh BackgroundQuery:=False
    End With
    
    Sheets("LASTPRICE").ListObjects("Tabla_Consulta_desde_FUSION").Unlist
    Sheets("Resumen").Select
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top