MS access update query issue with date range - ms-access

I'm using this update query in MS access 2016
Update sampledata set VALUE= '" & Me.value & "' where Day between '" & Me.dayfrom & "' and '" & Me.dayto & "';
The strangest problem m facing is- It is considering the form values for start and end date but however updates records only for the start date. Example. If dayfrom is 01-Nov-2021 and dayto is 30-Nov-2021, the query is updating records of only 01-Nov-2021.
When I pass the day from as 30-Nov-2021, it is updating records for the whole month.
Note: This doesn't happen when I directly pass the values in the query, it happens only when i Pick data from FORM and apply it in query.

UPDATE table3
SET table3.status = "YES"
WHERE (((table3.transactiondate)>=[FORM]![Form3]![startdate] And
(table3.transactiondate)<=[FORM]![Form3]![enddate]));
When you run the query, two pop up input box will open, in the first one, enter the value for start date, e.g 01/01/2022 , in the second one enter the end date e.g 02/28/2022. Do check the date format in use by your system so you can be sure you are entering the date parameter in the right format.

As is, the date values will be casted to strings using your Windows settings.
So, force a format on the string expressions to have true string expressions for the date values:
Sql = "Update sampledata " & _
"Set VALUE = '" & Me!value.Value & "' " & _
"Where Day Between #" & Format(Me!dayfrom.Value, "yyyy\/mm\/dd") & "# And #" & Format(Me!dayto.Value, "yyyy\/mm\/dd") & "#;"

Related

SQL will not insert into table

I apologies in advance for this question – but I have no experience in classic ASP, although I have tried, I just cannot figure it out.
I have been given the task of tweaking an existing system (until a replacement is found), which has been written in ASP/VBScript.
The issue I am facing is that when I retrieve some data using SQL that I can’t get the script to update the results into the table. The number of results can change each time a query is run.
For example; The data in table calculates the time someone is absent, and each time someone is absent it records the duration in one field, in another field it records the date and in another field gives the record a value = 1.
This part works.
What I’m trying to do is collate the total duration absence per date and insert that data into the same table with a value = 2.
The code:
Set rssum = conn.execute("select sum(total) as total, visitdate from table_1 where repid=" & session("repid") & " and type=1 group by visitdate ")
If rssum.eof Then
While Not rssum.eof
rstotal=rssum("total")
rstotal=rssum("visitdate")
'some code to generate nextid
Set abrec = conn.execute(insert into table_1 (abid,repid,type,visitdate,total) values(" & nextid & ", " & session("repid") & ",2,'" & visitdate & "'," & total & ")
Wend
rssum.movenext
rssum.close
Set rssum = Nothing
End If
When I run the script, and say I’m calculating 2 days worth of data, it returns 2 type=2 entries, but duplicates the date for the first date.
Why does this happen?
movenext needs to be at the end of the loop, but inside the loop. Otherwise it will repeat indefinitely. I'm surprised you didn't get an infinite loop.
while not rssum.eof
rstotal=rssum("total")
rstotal=rssum("visitdate")
'some code to generate nextid
set abrec = conn.execute(insert into table_1 (abid,repid,type,visitdate,total) values(" & nextid & ", " & session("repid") & ",2,'" & visitdate & "'," & total & ")
rssum.movenext
wend
Several issues here.
1.
if rssum.eof then
while not rssum.eof
Think about the logic of this, you're saying if a condition exists then do this only when the condition doesn't exist. You probably don't need a conditional statement at all, but if you want one which only applies to a non empty recordset then the way to do it is
if not (rssum.eof and rssum.bof) then
2.
set abrec = conn.execute(...)
This is an insert command not a select. You don't have any output to populate a recordset, so lose set abrec =
You need quotes around your insert query
As Ekkehard points out you need to assign the values of rssum("total") and rssum("visitdate") to separate variables, don't use rstotal twice
As Kyle JV points out in another answer wend needs to come after rssum.movenext, not before.
Putting it all together, try
set rssum = conn.execute("select sum(total) as total, visitdate from table_1 where repid=" & session("repid") & " and type=1 group by visitdate ")
if not (rssum.bof and rssum.eof) then
while not rssum.eof
total=rssum("total")
visitdate=rssum("visitdate")
conn.execute("insert into table_1 (abid,repid,type,visitdate,total) values(" & nextid & ", " & session("repid") & ",2,'" & visitdate & "'," & total & ")"
rssum.movenext
wend
rssum.close
set rssum = nothing
end if
if rssum.eof then ' if EOF
while not rssum.eof ' while not EOF
can't possibly work and assigning to rstotal twice:
rstotal=rssum("total")
rstotal=rssum("visitdate")
makes no sense either.

how to make query by date in access 2013?

I made access application and i have query build on date criteria
this is my query
Set sales = CurrentDb.OpenRecordset("Select * From sales where action_date = #" & date_actions & "#")
I change the date in windows to dd/mm/yyyy
But when i try to run this query nothing happens
But when i change it to default MM/d/yyyy
It run correctly
How to solve this problem? Please, and thanks in advance
I suggest use this format: YYYY-MM-DD HH:MM:SS
You can convert your date as follows:
Format(date_actions, "yyyy-mm-dd hh:mm:ss")
Then your statement will be:
Set ftm_date = Format(date_actions, "yyyy-mm-dd hh:mm:ss")
Set sales = CurrentDb.OpenRecordset("Select * From sales where action_date = #" & ftm_date & "#")
JET engine deals date in American format, not the regular DD/MM/YYYY HH:NN:SS. So you need to format the dates accordingly.
Set sales = CurrentDb.OpenRecordset("SELECT * " & _
"FROM " & _
"sales " & _
"WHERE action_date = " & Format(date_actions, "\#mm\/dd\/yyyy\#"))
Hope this helps.

Not able to read from access table properly

I have encountering a very silly problem. The requirement is to stop the duplicate entry in access table. I am using Vb6. But each time I try I am encountering syntax error.
My code
My flexgrid is populated and refreshed. I am able to insert and select data in another table in same database. But this one is failing
sql_txt1 = "Select SL_No from SpAIReport where DOM ='" & Format(Now, "mm/dd/yyyy") & _
"' and AC-REG ='" & msgDisFlex.TextMatrix(i, 4) & "' and Flt No = '" & msgDisFlex.TextMatrix(i, 6) & "'"
Set rs1 = db.OpenRecordset(sql_txt1)
I am able to update this table, but multiple time same data are getting populated.
The access table structure with the entries are given below
Date_OF_Fly Flt No GMT Weight Airtime Station DOM Data_hrs Filename
7/3/2000 11 03:45:01 5 01:23:40 XXX 01/25/2014 120:10:15 ABCD
Plus, after saving if I want to access it through recordset then it is showing NULL.
The code is:
sql_txt = "select * from SpAIReport where DOM='" & dateDailyReport & "'"
Set rs = db.OpenRecordset(sql_txt)
The dateDailyReport value is 01/25/2014. This value is present in database. Still this query is not working.
Please help.
You probably want to put 'Flt No' and 'AC-REG' in square brackets otherwise they look like two field names:
"' and [AC-REG] ='" & msgDisFlex.TextMatrix(i, 4) & "' and [Flt No] = '" &

Scrollbar size wrong after filtering a forms record source

The forms recordsource is 1900 records & then gets filtered based on what user selects in a combobox list, in VBA I have a SELECT CASE statement to do something like the below for each selection
Private Sub cmbDateRange_AfterUpdate()
SELECT CASE Me.cmbDateRange.Value
Case "Yesterday"
Me.Filter = "messageDateandtime BETWEEN #" & Date - 1 & "# AND #" & Date - 1 & " 23:59#"
Case "Past 7 Days"
Me.Filter = "messageDateandtime BETWEEN #" & Date - 7 & "# AND #" & Date & " 23:59#"
End Select
Me.FilterOn = True
Me.Recordset.MoveLast: Me.Recordset.MoveFirst
The last line is a cheap hack that is working to fix the scroll bars being long for the original record set 1900, whereas this filter should be like 100. I am guessing there is a better/more-efficient way?

An Access Problem:how to show records between two dates

I have two combobox in my Access form named CombDate and CombDat1. Now I want to show records between two dates. How can I do this?
Using VBA, you can retrieve the values from the two date combo boxes and store them in date1 and date2, then you can open a recordset using VBA and DAO, with the following SQL:
"SELECT field1, field2 " & _
"FROM table " & _
"WHERE datefield > " & date1 & " AND datefield < " & date2 & ";"
More details would help clarify the solution, but that's the basic process.