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:
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