Could you help me please? I have tried to solve my problem for about 6 hours.
I have one table and there are 4 columns.
Columns:
Nio ,checked,date,time
I need to get data from the first and second columns, where I have to compare it by date and time.
I wanted to use the operator IN but excel gives me the error
Incorrect syntax near the keyword
Thanks for help.
strQueryQ7DrLeftElox =
"((Select nio,checked
FROM q7_dr_left_elox_incoming_inspection
where date >= '" & sDateFrom & "' AND date <= '" & sDateTo & "') IN (Select nio,checked from q7_dr_left_elox_incoming_inspection WHERE time = '19:55:06'))"
Both select statements query the same table, so simplify it by adding one more condition in the 1st query's WHERE clause:
SELECT nio,checked
FROM q7_dr_left_elox_incoming_inspection
WHERE date >= '" & sDateFrom & "' AND date <= '" & sDateTo & "'
AND time = '19:55:06'
Related
I am trying to execute an SQL statement in a VBA script. I have gotten the script to run, but it ignores the where with a date filter.
I have researched and tried every option I can find, but just cant seem to get it to work.
Set rs = conn.Execute("Select [adjustment_number], [status], [tax_adjusted],[amount], [gl_date],[creation_date],[apply_date],[comments],[type],[adjustment_type],[dbo].[Code_Combinations].[segment1], [dbo].[Code_Combinations].[segment10],[dbo].[Code_Combinations].[segment11],[dbo].[Code_Combinations].[segment12],[dbo].[Code_Combinations].[code_combination] FROM [dbo].[AR_ADJUSTMENTS_ALL] " & _
"left outer join [dbo].[Code_Combinations] on [dbo].[AR_ADJUSTMENTS_ALL].[CODE_COMBINATION_ID] = [dbo].[Code_Combinations].[CODE_COMBINATION_ID] where [gl_date] >= " & gldate & ";")
(Copying this from my comment above, into an answer, since this solved your problem)
SQL Server? Put single quotes around your date in the WHERE clause.
where [gl_date] >= '" & gldate & "';"
Assumes that gldate is a valid date, and [gl_date] is of a date datatype.
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.
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)
I have a page that displays results in a SQL database. And then I have another page that lets me edit whichever row I want to edit. One of the fields are dates. If added into the database through one of my pages it gets put in with the format (YEAR-MN-DY)(2014-04-11). When I go to UPDATE the date it then does arithmetic on the date. For example. If the date is currently 2014-04-11 and I update/change the date to 2010-01-01 it will replace the date with "2008" which is 2010 -1 - 1.
The variable is a string that is received through a HTML form.
strSQL = "UPDATE sales SET cust_id = " & intcust_id & ", agent_id = " & intagent_id & ", saledate = " & strsaledate & " WHERE sale_id = " & intsale_id & ""
Is the SQL query I am running.
Also, the DATE is VARCHAR2 in the database and used as a string throughout my VB code. I kept it this way because not everyone enters the date the same and this is for simplicity.
The subtraction is occurring because the date is not being interpreted as a date, but as a number because it is missing its quotes.
Before
strSQL = "UPDATE sales SET cust_id = " & intcust_id & ", agent_id = " & intagent_id & ", saledate = " & strsaledate & " WHERE sale_id = " & intsale_id & ""
After
strSQL = "UPDATE sales SET cust_id = " & intcust_id & ", agent_id = " & intagent_id & ", saledate ='" & strsaledate & "' WHERE sale_id = " & intsale_id & ""
The answer from WorkSmarter tackles the problem. I think however you should not use string concatenation. It is wide open to sql-injections and it is indeed much nicer, simpler and less error prone to use parameters. Something like
strSQL = "UPDATE sales SET cust_id = #custid, agent_id = #agentid, saledate = #salesdate WHERE sale_id = #saleid"
set custid = cmd.CreateParameter("#custid", adChar,adInput,10, incust_id)
set agentid = cmd.CreateParameter("#cagentid", adInteger,adInput,0, ) ...
I'm asuming you have an ADODB.Command by the name of cmd. By doing it like this you are making your code a lot safer and, in my opinion, more readable since you don't have to worry about quotes and single quotes all the time. There is a clear distinction between the sql command and the params/values involved.
You can find good documentation on parameter on http://www.w3schools.com/asp/met_comm_createparameter.asp
In my software I have a registered date as well as expiry date as shown below:
And I'm updating them to the database as shown below:
Now I would like to filter all the records from my database where who has completed the expirydate and give some color to them. (or) can I compare the expiry date with today's date.
For Filtering by name I have done this way:
Dim Name As String = "%" + txtName.Text.ToString + "%"
Me.CustomersBindingSource.Filter = " FirstName LIKE '" & Name & "'"
In the similar way can I do something for the expiry date?If So how do I proceed?
Any suggestions are most welcome..
Why can't you use something like this:
Me.CustomersBindingSource.Filter = " ExpiryDate > '" & System.DateTime.Now.ToString("yyyyMMdd") & "'"