Saltar al contenido
View in the app

A better way to browse. Learn more.

Ayuda Excel

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Filtrar consulta Sql por año

publicado

Buenos días a todos;

-Tengo creada una macro que hace una consulta Sql sobre una tabla Access con un Where donde le pongo los parámetros de fecha.

Ahora estoy intentado cambiar los parámetros de fecha (DD/MM/AAAA) por solo año (AAAA).

Lo que he añadido a mayores al formulario son dos SpinButton que insertan los años en cuestión.

Private Sub btn_Buscar_Click(): On Error Resume Next
Dim vConsulta, where As String
Dim Fecha1, Fecha2 As Date
'<<<Dim Año As Variant
Dim i As Integer
Conectar
Application.ScreenUpdating = False
Application.DisplayAlerts = False

vConsulta = "sql_Resumen"

Fecha1 = Format(Me.TextBox100, "DD/MM/YYYY")
'<<<<<Fecha1 = Year(Format(Me.TextBox100, "YYYY"))
MsgBox Fecha1
Fecha2 = Format(Me.TextBox200, "DD/MM/YYYY")
'<<<<Fecha2 = Year(Format(Me.TextBox100, "YYYY"))
MsgBox Fecha2

If IsNull(TextBox100) Or TextBox100 = "" And IsNull(TextBox200) Or TextBox200 = "" Then
        MsgBox "No ha seleccionado ninguna fecha"
    TextBox100.SetFocus
    Exit Sub
End If

   where = "fecha >= #" & Fecha1 & "# AND fecha <= #" & Fecha2 & "#"
   
   Sql = "SELECT *"
   Sql = Sql & " FROM " & vConsulta & ""
   Sql = Sql & " WHERE " & where & ""
   rs.Open Sql, cnn, 3, 3, 1
   
   With Sheets("Informes")
      .Cells.Delete
      For i = 0 To rs.Fields.Count - 1
       .Cells(1, i + 1) = rs.Fields(i).Name
      Next i
      .Range("A2").CopyFromRecordset rs
      If rs.RecordCount > 0 Then
      Else
      MsgBox "No hay ventas para mostrar"
      Exit Sub
      End If
   End With
   rs.Close

Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

 

 

MAño.zip

Featured Replies

publicado

Usa la función Year() de SQL

sql = "Where Year(Fecha)>=" & TextBox100 & " And Year(Fecha)<=" & TextBox200

 

 

Editado el por Antoni

publicado
  • Autor

Gracias por el cable Antoni;

Me centraba en la variable "Fecha1 y Fecha2" y no en la Sql.

Moitas como sempre e bo finde Antoni.

Archivado

Este tema está ahora archivado y está cerrado a más respuestas.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.