Visual Basic - Problema con consulta de fechas.

Life is soft - evento anual de software empresarial
 
Vista:
sin imagen de perfil

Problema con consulta de fechas.

Publicado por Adolfo (117 intervenciones) el 12/08/2002 18:06:53
Tengo el siguiente código que usaba para buscar un registro en una tabla, cuyo campo FECHA es de tipo “TEXT”. La fecha la digitaba en un TEXTBOX, Este código me funcionaba.

Pero me vi forzado a cambiarle el formato al campo FECHA en la tabla, a formato “DATE”, para poder hacer consultas de rangos de fechas.
El problema es que al cambiarle el campo FECHA a formato “DATE”, el código que usaba para hacer la consulta de una fecha especifica me da el siguiente mensaje de error:

“Data type mismatch in criteria expression”

El error lo da en la línea marcada con un asterisco “ * ”

Quisiera saber si hay alguna forma de hacer que este código funcione con un campo de tipo “DATE”.

Agradesco de antemano cualquier ayuda.

Codigo para consultar UNA sola fecha.

Do While True
' Get user input and build search string.
BuscaFecha = Trim(TxtBuscarfecha.Text)
If BuscaFecha = "" Then
Exit Do
Else
BuscaFecha = "Fecha = '" & BuscaFecha & "'" '

With Tabla
' Populate recordset.
.MoveLast
' Find first record satisfying search string. Exit
' loop if no such record exists.
* .FindFirst BuscaFecha

If .NoMatch Then
MsgBox "Esta fecha no existe"
TxtBuscarfecha.Text = ""
TxtBuscarfecha.SetFocus
Exit Sub
Else
Fecha.Caption = Tabla!Fecha
Totvehiculos.Caption = Tabla!Totvehiculos
Total.Caption = Format(Tabla!Total, "##,###,###")
CmdLimpiar.SetFocus
Exit Do
End If
End With
End If
Exit Do
Loop


Nota: el codigo que uso para hacer la consulta de RANGOS de fechas es el siguiente.

Dim basedatos1 As Database
Dim Tabla As Recordset
Dim Primera As Date
Dim Ultima As Date
Dim posicion, posicion2 As Byte

Set basedatos1 = OpenDatabase(App.Path & "\Bitacora.mdb")

If Tipocierre = 1 Then
Set Tabla = basedatos1.OpenRecordset("SELECT * from diario", dbOpenDynaset)
ElseIf Tipocierre = 2 Then
Set Tabla = basedatos1.OpenRecordset("SELECT * from semanal", dbOpenDynaset)
ElseIf Tipocierre = 3 Then
Set Tabla = basedatos1.OpenRecordset("SELECT * from mensual", dbOpenDynaset)
ElseIf Tipocierre = 4 Or Tipocierre = 5 Then
Set Tabla = basedatos1.OpenRecordset("SELECT * from anual", dbOpenDynaset)
End If

If Tabla.EOF Then
MsgBox "La base de datos esta basia"
TxtBuscarfecha.Text = " "
TxtBuscarfecha.SetFocus
Exit Sub
End If

If Fecha2.Text <> "" Then
Primera = CDate(TxtBuscarfecha.Text) ' se supone que en Fecha1.text y Fecha2.Text tengo fechas válidas
Ultima = CDate(Fecha2.Text)

Tabla.MoveFirst

Do While Not Tabla.EOF
If Tabla!Fecha >= Primera And Tabla!Fecha <= Ultima Then
FechaTemp = Tabla!Fecha
TotvehiTemp = Tabla!Totvehiculos
TotalTemp = Tabla!Total

Set Tabla1 = basedatos1.OpenRecordset("TablaTemp", dbOpenTable)

Tabla1.AddNew
Tabla1!Fecha = FechaTemp
Tabla1!Totvehiculos = TotvehiTemp
Tabla1!Total = TotalTemp
Tabla1.Update

posicion = posicion + 1
posicion2 = 1

Set Tabla = basedatos1.OpenRecordset("SELECT * from diario where fecha between #" & Primera & "# and #" & Ultima & "#", dbOpenDynaset)

Do While posicion2 <= posicion
Tabla.MoveNext
posicion2 = posicion2 + 1
If posicion2 > posicion Then
posicion2 = 0
Exit Do
End If
Loop
Else
Tabla.MoveNext
If Tabla.EOF Then
Exit Do
End If
End If
Loop

Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Problema con consulta de fechas.

Publicado por JUAN RODRIGUEZ (36 intervenciones) el 13/08/2002 14:47:43
Seria bueno que pruebes con la siguiente instruccion
FECHA_INICIAL = DATE
FECHA_FINAL = DATE

SQL = "SELECT * FROM TABLA WHERE FECHA >= #" & Format(FECHA_INICIAL, "mm/dd/yyyy") & "# AND FECHA <= #" & Format(FECHA_FINAL, "mm/dd/yyyy") & "# "

USANDO UN SELECT CON ESTE FORMATO, ATENDIENDO QUE FECHA_INICIAL y FECHA_FINAL SON VARIABLES DE MEMORIA y QUE FECHA ES EL CAMPO DE LA TABLA.

Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar