Retrieve data using form from Dates - sql

I have a query in Access named 'Laufzettel' and it looks in this way. The dates are in dd.mm.yyyy format
Antragsnummer Eingang esigniert Ausgang Anlage Policierung
111 2.10.2016 2.10.2016 3.10.2016 3.10.2016
222 3.10.2016 3.10.2016 3.10.2016 4.10.2016
333 5.10.2016 6.10.2016 7.10.2016 7.10.2016
I am creating a form named 'overview' with two Textboxes named StartDate and EndDate and search button. What I need is When I click the search button by giving in two dates it should retrieve me the related records from the above query with all the fields in form of a report.
The StartDate and EndDate are related to the Eingang field of the query. SO when I enter 2.10.2016 as StartDate and 5.10.2016 as EndDate it must produce the above query. To achieve this I have started to create a query using the form fields which in turn can produce me the required report when I click the search button. So I am trying with the code
SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.Eingang, Laufzettel.esigniert, Laufzettel.Ausgang, Laufzettel.Anlage, Laufzettel.Policierung
FROM Laufzettel
WHERE Lauzettel.Eingang BETWEEN [Forms]![overview]![StartDate] and [Forms]![overview]![EndDate];
For which I get an error Access cannot recognise [Forms]![overview]![StartDate] and [Forms]![overview]![EndDate] as a valid field name or expression
How can I achieve the above? Can someone help me?
EDIT : Here is my working Query now. Although It doesn't give me the records from EndDate values.
PARAMETERS [Forms]![overview]![start] DateTime, [Forms]![overview]![end] DateTime;
SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.AEingangDatenstromZWorkflow, Laufzettel.BEingangesigniertDokumentZWorkflow, Laufzettel.CAusgangDatenstromZWorkflow, Laufzettel.DAnlageSchwebeVSL, Laufzettel.EPolicierungVSL
FROM Laufzettel
WHERE (((Laufzettel.AEingangDatenstromZWorkflow) Between [Forms]! [overview]![start] And [Forms]![overview]![end]));
Can someone tell me where am I going Wrong?

If build in code (VBA), try with:
SQL = "SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.Eingang, Laufzettel.esigniert, Laufzettel.Ausgang, Laufzettel.Anlage, Laufzettel.Policierung " & _
"FROM Laufzettel " & _
"WHERE Lauzettel.Eingang BETWEEN #" & Format([Forms]![overview]![StartDate], "yyyy\/mm\/dd") & "# AND #" & Format([Forms]![overview]![EndDate], "yyyy\/mm\/dd") & "#"
If the "date" field really is text, apply DateValue:
SQL = "SELECT Laufzettel.ANTRAGSNUMMER, Laufzettel.Eingang, Laufzettel.esigniert, Laufzettel.Ausgang, Laufzettel.Anlage, Laufzettel.Policierung " & _
"FROM Laufzettel " & _
"WHERE DateValue(Lauzettel.Eingang) BETWEEN #" & Format([Forms]![overview]![StartDate], "yyyy\/mm\/dd") & "# AND #" & Format([Forms]![overview]![EndDate], "yyyy\/mm\/dd") & "#"

Related

Access VBA SQL query searching between dates

I have a some VBA code that takes in a date from and date to fields from two date picker values in textboxes.
However, when searching between the two dates I receive the 'Enter Parameter Value' prompt asking for input.
Dim SQLAllReject As String
Dim strDateFrom As String
Dim strDateTo As String
strDateFrom = Format(txtDate.Value, "mm/dd/yyyy")
strDateTo = Format(txtDateTo.Value, "mm/dd/yyyy")
When running directly in the Query Design wizard, searching between dates works fine:
WHERE (((XXXXXXXXXXXXXXXXXXXX.Date) Between #10/1/2021# And #10/27/2021#))
What is wrong here? How can I force Access/VBA to take the dates I've specified and search between them and NOT display the 'Enter Parameter Value' prompt?
When I debug the query and step through, it gives exactly the same dates as running it in Query Design view.
FYI if I use one date and the LIKE operator, this works fine.
SQLAllReject = "SELECT dbo_vw_busobj_file_rejections_load_access_temp5_copy.HashKey AS [ID], dbo_vw_busobj_file_rejections_load_access_temp5_copy.Reject_Date AS [Date], " & _
"FROM dbo_vw_busobj_file_rejections_load_access_temp5_copy " & _
"WHERE (((dbo_busobj_file_rejections_load_access_temp5_copy.Reject_Date) Between #" & strDateFrom & "# And #" & strDateTo & "#)) " & _
"ORDER BY dbo_vw_busobj_file_rejections_load_access_temp5_copy.Reject_Date DESC;"
Ok I somehow fixed it... the query string was trying to select from a view rather than the table and for some odd reason it was causing the parameter prompt.
I have changed dbo_vw_busobj_file_rejections_load_access_temp5_copy to dbo_busobj_file_rejections_load_access_temp5_copy table and managed to get it to work after doing some string to date conversion when selecting dates from the textboxes.

MS Access VBA SQL SELECT * INTO tempTbl WHERE Stuff="" AND OtherStuff BETWEEN Date Range

I have a form with one text box, two combo boxes (dropdowns), and two text boxes with input masks for mm/dd/yyyy 99/99/0000;0;_
I am attempting to use all of these fields as filters for a subform.
I have the controls set to fire after update and run a sub that builds a SELECT * INTO sql string for a temp Table that is then sourceobject'ed back to the subform.
In code I have for each control I have code building a snippet of the Where statement for the final sql. the snippet grows as needed, is labeled "Filter"
Some other non-Finished Code....
If Not IsNull(txtDateFrom) Then
If i > 1 Then Filter = Filter & " AND " & "([Date_LastSaved] >= " & Me.txtDateFrom & ")" & " And " & "([Date_LastSaved] <= " & Me.txtDateTo & ")"
End If
Dim sql As String
sql = "SELECT * INTO tmpTable FROM tblReview"
If Not IsNull(Filter) Then
sql = sql & " WHERE " & Filter
End If
My issue and question is that I am testing this one specific situation where there will be Filter = Filter & " AND " & "DATE_STUFF"
where the final sql looks like...
SELECT * INTO tmpTable FROM tblReview WHERE ReviewStatus = 'Quoted' AND ([Date_LastSaved] >= 09/12/2018) And ([Date_LastSaved] <= 10/16/2018)
which should have some result with the test data. Yet, the tmpTable is empty.
This is only happening when I apply a date range criteria.
I tried BETWEEN but could not nail down the syntax.
Any insight is greatly appreciated, Thanks!
UPDATE:
Answer:
If i > 1 Then Filter = Filter & " AND " & "([Date_LastSaved] >= #" & Me.txtDateFrom & "#)" & " And " & "([Date_LastSaved] <= #" & Me.txtDateTo & "#)"
If you want to use dates then they must be quoted. If you just say 10/16/2018 then you are dividing the integer 10 by 16 by 2018 which will yield an integer zero. It will then convert the dates to an integer to do the compare, which will yield a much bigger number, and thus you get no rows.
Any date testing should always be done using date types rather than strings. I think in msaccess you can surround it in #, but not sure. Just research this.

MS Access VBA, not getting results when running DoCmd to find a record

I am not a programmer by any means but I am trying to get a small data collection database going. I need to find a record based on input. Ive got two criteria that I want it to find, and if both arent found together, it is supposed to create a new record (with both fields filled out to prevent duplication of the pair). First criteria is that it needs to look for a date. I figured that one out already, here is the code.
Dim Date1 As String
Date1 = Text6.Value
DoCmd.OpenForm "Data", , , "Data![ShiftDate] = #" & Date1 & "#"
Works like a charm. Now, I tried to add the second criteria in. The second criteria pulls from a listbox, either Day or Night. Then, it is supposed to search my data fields for that same criteria in a list box on my Data! form.
Dim Date1 As String
Dim Shift1 As String
Date1 = Text6.Value
Shift1 = List12.Column(1)
MsgBox Shift1
DoCmd.OpenForm "Data", , , _
"Data![ShiftDate] = #" & Date1 & "# AND Data![Shift] ='" & Shift1 & "'"
the msgbox was just to verify that it is pulling the right words, day or night. Everything works great, but instead of pulling a record on the right date, and with the right shift, I get blank records. I tried taking out the first half of the where statement to isolate the shift part and still just cannot get it to find a record. I have no idea what I am doing wrong.
It seems that you already have a form.
I recommend you trying this:
Add a button with a click event to the form and add following code to the click event:
dim rs as dao.recordset
set rs = currentdb.openrecordset ("SELECT * FROM [yourtable] WHERE ('ShiftDate' = '" & [yourParameter] & "' AND 'Shift' = '" & [your2ndParameter] & "')")
if rs.eof then
' here NO record is found
currentdb.execute ("INSERT INTO [yourtable] ('ShiftDate', 'Shift') VALUES ('" & [yourParameter] & "', '" & [your2ndParameter] & "')")
else
' here a record is found
endif
Since your ShiftDate is a date you need to convert your date to a SQL date Format, like this:
Function SQLDatum(Datumx) As String
If IsDate(Datumx) Then
SQLDatum = Format(CDate(Datumx), "\#yyyy-mm-dd\#", vbMonday, _
vbFirstFourDays)
Else
SQLDatum = ""
End If
End Function
In your command
DoCmd.OpenForm "Data", , , "Data![ShiftDate] = #" & Date1 & "# AND ...
You open a form Data but at the same time you specify a filter by referring to the form which is being opened with Data![ShiftDate]. This is very strange, since the filter must be applied to the data source. I would simply write
DoCmd.OpenForm "Data", , , "ShiftDate= #" & Date1 & "# AND Shift= '" & Shift1 & "'"
by referring to the table columns of the data source (on the left side of =).
Note that the column Shift must be a text column. If it was some number, you would have to drop the single quotes and pass it a corresponding id.

I would like to make a query with a concatenated fields from a table in my vba code in ms access

I have a table in my ms access database which has 1 column that says downdate and 1 column that says downtime also 1 column that says update and 1 columns that says uptime. I have a created a query that concatenated the downdate and downtime, it was created in my query as Down: [downdate] & " # " & [downtime] and Up: [update] & "#" & [uptime].
What I wanted to do is to make another query where I can find all the records between the date from my first textbox to my second textbox with out losing the query that I have created to show up in my listbox. I really need your help everyone thanks in advance.
If you wish to filter for a date/time period, you can't use that weird string format. You must ad the date and time:
"Where #" & Format(date1, "yyyy\/mm\/dd") & "# Between [downdate] + [downtime] And #" & Format(date2, "yyyy\/mm\/dd") & "# Between [update] + [uptime]"

comparison between dates not working in ms access

Using Access 2013
I have table in Access where Column "DueDate" is defined as "Date/Time"
System default date format is dd/mm/yyyy
Want to change List's Row Source through VBA, based on value in a textbox "txtDueDateUpto".
So, that I get all the task where the dude date is less than equal to the date entered by user in "txtDueDateUpto"
mySql = "SELECT TaskTbl.TaskID "
& " FROM TaskTbl " _
& " WHERE " _
& " DueDate is not Null and " _
& " Format (DueDate," & """dd/mm/yyyy""" & ") <= " & Format(CDate(Me.txtDueDateUpto.Value), "dd/mm/yyyy") _
Me.listTask.RowSource = mySql
I have 3 Tasks for testing purpose.
where DueDate is saved as
TaskID DueDate
1 25-17-2015
2 01-07-2015
3 29-06-2015
and, value in txtDueDateUpto is 06-07-2015
txtDueDateUpto format property is set to "Short Date"
I was expecting taskID 2,3 to be returned, with the given SQL, but I am getting taskID 2
I sat all night, trying many permutations and combinations, but cannot get what is that, I am doing wrong.
newbie for Access VBA. Pls help, thanks in advance.
Always handle dates as dates, not strings, no exceptions.
If your textbox has been assigned a date/time format, you don't even have to use CDate or DateValue because Access then reads that value as of data type Date.
However, you must concatenate the value with the SQL code as a properly formatted string expression of the date value:
mySql = "SELECT TaskTbl.TaskID "
& " FROM TaskTbl " _
& " WHERE " _
& " DueDate is not Null and " _
& " DueDate <= #" & Format(Me!txtDueDateUpto.Value, "yyyy\/mm\/dd") & "#"
If you are going to format the dates as strings -- and compare them -- then always use YYYY-MM-DD format:
& " Format (DueDate," & """yyyy-mm-dd""" & ") <= " & Format(CDate(Me.txtDueDateUpto.Value), "yyyy-mm-dd") _
However, I'm pretty sure that MS Access can just do date comparisons without the conversion:
DueDate <= CDate(Me.txtDueDateUpto.Value)