Results 1 to 7 of 7
  1. #1
    Anacronos is offline Novice
    Windows 8 Access 2013
    Join Date
    Jan 2016
    Posts
    24

    Filtering a subform with a SQL statement using a text box.

    I have a form which have a subform that is filtered by two things one is a Combobox which works fine; The other one is a text box in which short date is typed by the user, I have a mask in the text box so short date is forced but when entered and executed it returns no records... the subforms goes blank so I know is executed but still no matches the field that saves the date is short Date and the default value is Date() the field in the second table where the records are copied to is also short date the records are copied from one table to another one with a add query/consult? which also has 2 text boxes that works just beautifully ;( Iīm hell of a new.

    This is the code of the button

    Private Sub cmdFiltrarpordia_Click()
    Dim SQL As String



    SQL = "SELECT TablaTemporal.Contacto, TablaTemporal.Total, TablaTemporal.Anticipo, TablaTemporal.Id, TablaTemporal.Nombre, TablaTemporal.Id_dia, TablaTemporal.Resta, TablaTemporal.Creado " _
    & "FROM TablaTemporal " _
    & "Where [Creado] = #" & Me.txtFecha & "# " _
    & "ORDER BY Id_dia ASC"
    Me.txtTot = DSum("[Total]", "[TablaTemporal]", "Creado = #" & Forms!Cort![txtFecha] & "# ")
    Me.SbfrCorte.Form.RecordSource = SQL
    End Sub

    Also the txtTot with Dsum doesnīt work but I guess is the same issue.

    Thanks in advance.

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Troubleshooting time.....

    What is Forms!Cort![txtFecha] when the code executes?
    Is the SQL string properly formed?
    Code:
    Private Sub cmdFiltrarpordia_Click()
        Dim SQL As String
    
        SQL = "SELECT TablaTemporal.Contacto, TablaTemporal.Total, TablaTemporal.Anticipo, TablaTemporal.Id, TablaTemporal.Nombre, TablaTemporal.Id_dia, TablaTemporal.Resta, TablaTemporal.Creado " _
              & "FROM TablaTemporal " _
              & "Where [Creado] = #" & Me.txtFecha & "# " _
              & "ORDER BY Id_dia ASC"
    
        '----debugging----------------------------------------
        Debug.Print SQL
    
        MsgBox "the value for txtFecha is  " & Forms!Cort![txtFecha]
        '----debugging----------------------------------------
    
    
        Me.txtTot = DSum("[Total]", "[TablaTemporal]", "Creado = #" & Forms!Cort![txtFecha] & "# ")
        Me.SbfrCorte.Form.RecordSource = SQL
    End Sub
    Set a breakpoint at this line
    Me.txtTot = DSum("[Total]", "[TablaTemporal]", "Creado = #" & Forms!Cort![txtFecha] & "# ")
    so you can see what is happening

  3. #3
    Anacronos is offline Novice
    Windows 8 Access 2013
    Join Date
    Jan 2016
    Posts
    24
    What is Forms!Cort![txtFecha] when the code executes? Where Creado = #12/02/2016# ORDER BY Id_dia ASC
    Is the SQL string properly formed? I donīt understand this one xD

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Did you execute the code with the debugging modifications?

    What did the message box display?


    Is the SQL string properly formed? I donīt understand this one xD
    Set the breakpoint.
    Execute the code
    What is in the Immediate Window? Copy that line and post it here.

  5. #5
    Anacronos is offline Novice
    Windows 8 Access 2013
    Join Date
    Jan 2016
    Posts
    24
    This is the SQL in the immediate window
    SELECT TablaTemporal.Contacto, TablaTemporal.Total, TablaTemporal.Anticipo, TablaTemporal.Id, TablaTemporal.Nombre, TablaTemporal.Id_dia, TablaTemporal.Resta, TablaTemporal.Creado FROM TablaTemporal Where Creado = #12/02/2016# ORDER BY Id_dia ASC

    The msgbox says 12/02/2016.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    OK, two thing here. The form problem is separate from the DSum() issue.

    You say the combo box filtering is working. Is the code for that different that the code for the text box?


    The DSum should work. Is the date "12/02/2016" "mm/dd/yyyy" or "dd/mm/2016"?
    If it is "dd/mm/yyyy", see http://www.allenbrowne.com/ser-36.html

  7. #7
    Anacronos is offline Novice
    Windows 8 Access 2013
    Join Date
    Jan 2016
    Posts
    24
    The format was the problem, I used the link http://www.allenbrowne.com/ser-36.html it provided with some formating code.
    Private Sub cmdFiltrarpordia_Click()
    Dim SQL As String

    SQL = "SELECT TablaTemporal.Contacto, TablaTemporal.Total, TablaTemporal.Anticipo, TablaTemporal.Id, TablaTemporal.Nombre, TablaTemporal.Id_dia, TablaTemporal.Resta, TablaTemporal.Creado " _
    & "FROM TablaTemporal " _
    & "Where TablaTemporal.Creado = #" & Format(Me.txtFecha, "mm\/dd\/yyyy") & "# " _
    & "ORDER BY Id_dia ASC"
    Me.txtTot = DSum("[Total]", "[TablaTemporal]", "Creado = #" & Format(Forms!Cort![txtFecha], "mm\/dd\/yyyy") & "# ")

    Me.SbfrCorte.Form.RecordSource = SQL
    End Sub

    So the problem was that since Iīm outside of EUA ( which Iīve never been) the format I use dd/mm/yyyy itīs correctly shown in the tables and if the forms but not in vba the prof provided by the link is with the queries where you can type a date as criteria in a date field and see it as you writed but when changed to sql it changes to mm/dd/yyyy.



    Thank you so much :3, ssanfu .

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Subform combobox filtering
    By warren0127 in forum Forms
    Replies: 14
    Last Post: 05-27-2015, 11:23 AM
  2. Subform Not Filtering Properly
    By pmontalt in forum Reports
    Replies: 3
    Last Post: 07-08-2014, 12:28 PM
  3. Subform isn't filtering properly
    By Access_Novice in forum Forms
    Replies: 3
    Last Post: 11-29-2013, 12:44 PM
  4. Filtering a subform
    By JvdP in forum Forms
    Replies: 2
    Last Post: 03-16-2011, 08:48 AM
  5. Filtering a Subform
    By swalsh84 in forum Programming
    Replies: 2
    Last Post: 06-23-2010, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums