SQL Query Date Related SELECT Statement - sql

This is my query which runs perfectly
"SELECT RemBal FROM Sales WHERE CustomerName='" & CustomerName.Text & "'"
now i am trying to get the balance on two basis customer name & current date
"SELECT RemBal FROM Sales WHERE CustomerName='" & CustomerName.Text & "' AND SaleDate=#" & SaleDate.Value & "#"
now this query not giving me any error but not returning any value too
please help

Try this
SaleDate.Format = DateTimePickerFormat.Custom
SaleDate.CustomFormat = "yyyy-mm-dd"
"SELECT RemBal FROM Sales WHERE CustomerName='" & CustomerName.Text & "' AND SaleDate=#" & SaleDate.Text() &"# "

SELECT rembal
FROM sales
WHERE customername = '" & CustomerName.Text & "'
AND Format(saledate, 'dd/MM/yyyy') = '" & SaleDate.Value & "'

Related

Datefilter not working in accessdb with vb.net

I'm making ledger where I want to fetch data from date filter but it returns everything here is my query
str = "Select * from [ledger] where [client_name]='" & client_name.Text & "' OR [company_name]='" & company_name.Text.ToString & "' OR [registration_no]='" & reg_no.Text.ToString & "' And [date] Between '" & from_date.Value.ToShortDateString & "' And '" & To_Date.Value.ToShortDateString & "'"
In Access there is already date has datetime data type even I tried using access query builder it also returns all data instead of that particular date range.
Edit: after doing some troubleshoot query working perfectly like this
str = "SELECT * FROM [ledger] WHERE [date] Between # 10/03/2022 # And # 10/05/2022 # AND [client_name]='" & client_name.Text & "' AND [company_name]='" & company_name.Text.ToString & "' AND [registration_no]='" & reg_no.Text.ToString & "'"
but when I integrate with my date time picker to get that particular format, I get an error
System.InvalidCastException: 'Conversion from string "MM/dd/yyyy" to type 'Integer' is not valid
Any solution for this?
After some troubleshoot this is the final query which is woring properly
str = "SELECT * FROM [ledger] WHERE [date] Between #" & from_date.Value.ToString("MM/dd/yyyy") & "# And #" & To_Date.Value.ToString("MM/dd/yyyy") & "# AND [client_name]='" & client_name.Text & "' AND [company_name]='" & company_name.Text.ToString & "' AND [registration_no]='" & reg_no.Text.ToString & "'"
Thanks everyone for commenting.

SQL syntax issue related to totalcount in combination with dates

Can any body help me with the following SQL statement?
What am I doing wrong here?
It is giving me no result however there should be some...
The desired result should be a list or errorsdetails with the total number of times it is in the database (mdb) based on the selected date-period.
PS sorry for my badly written English..
Many thanks
Koen
sSQL = "SELECT ErrorDetail, count(*) AS totalcount FROM tblErrors WHERE DisplayName = " & cbox_modules.Text & " AND ErrorDate BETWEEN #" & startdate & "# AND #" & enddate & "# GROUP BY ErrorDetail"
Assuming the "DisplayName" column is a string, ensure you surround it with apostrophes:
SELECT
ErrorDetail,
COUNT(*) AS totalcount
FROM tblErrors
WHERE DisplayName = '" & cbox_modules.Text & "' AND ErrorDate BETWEEN #" & startdate & "# AND #" & enddate & "#
GROUP BY ErrorDetail

VBA SQL Syntax Problems

I have the following SQL:
SQL = "UPDATE [TBLTMP] SET TBLTMP24 '" & Me.TOWN & "' WHERE TBLTMP00 = '" & "1" & "';"
Table name TBLTMP
Field to update TBLTMP24
Record to update TBLTMP00
I want to store the value of ‘Me.Town’ in the field TBLTMP24 which is in the table TBLTMP, record number 1, anyone have any ideas what might work?
You're missing an = in your SQL Statement after TBLTMP24. You're statement should be:
SQL = "UPDATE [TBLTMP] SET TBLTMP24 = '" & Me.TOWN & "' WHERE TBLTMP00 = '" & "1" & "';"
I think all you need is to add = into your query, like below:
SQL = "UPDATE TBLTMP SET TBLTMP24 = '" & Me.TOWN & "' WHERE TBLTMP00 = '" & "1" & "';"
If you want to change some columns add commas, like below:
SQL = "UPDATE TBLTMP SET TBLTMP24 = '" & Me.TOWN & "', another_col = '" & Me.another & "' WHERE TBLTMP00 = '" & "1" & "';"

SQL for Classic ASP : How to insert record to tbl_order at the same time subtract qty from tbl_inventory and save back into Database

Currently I have the update code for insert record to tbl_order. Now that I implement the inventory section. I need the system can subtract qty from inventory once the order had been made.
I'm thinking about INSERT (Subtract (od_qty-tbl_inventory.inv_qty)) but I don't know how SQL statement gonna be in this case. So could you please help. Thank you
Below is my current update code for INSERT data to tbl_order.
<%
dim od_total, cal_total, od_qty
cust_id = request.querystring("cust_id")
bill_id = request.querystring("bill_id")
pd_id = request.querystring("pd_id")
od_price = request.querystring("od_price")
od_qty = request.querystring("od_qty")
od_qty_unit = request.querystring("od_qty_unit")
date_current = date
od_total = od_price * od_qty
Dim conn ' ADO connection
Dim rstSimple ' ADO recordset
Dim strDBPath ' path to our Access database (*.mdb) file
Dim bill_total
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("../database/tkp.mdb"))
set rs=Server.CreateObject("ADODB.recordset")
' Next, the script inserts the form inputs retrieved from the querystring into the database table:
sql="INSERT INTO tbl_order (cust_id,bill_id,pd_id,od_price,od_qty,od_qty_unit,od_total)"
sql=sql & " VALUES "
sql=sql & "(" & cust_id & ","
sql=sql & "'" & bill_id & "',"
sql=sql & "'" & pd_id & "',"
sql=sql & "" & od_price & ","
sql=sql & "" & od_qty & ","
sql=sql & "'" & od_qty_unit & "',"
sql=sql & "" & od_total & ")"
'response.write sql
on error resume next
conn.Execute sql
if err<>0 then
Response.Write("No update permissions!") 'ae_name field is primary key so new record not inserted if name pre-exists in table
else
'Response.Write("<h3> Record added</h3>")
end if
rstSimple.Close
Set rstSimple = Nothing
conn.Close
Set conn = Nothing
%>
You need to run an update query on your inventory table that subtracts the order quantity from the current inventory amount. Something like this:
sql = "UPDATE tbl_inventory SET inv_qty = inv_qty - " & od_qty & " WHERE pd_id = " & pd_id
You will be able to solve your problem with transaction handling
conn.BeginTrans
'—Run Your Statements Here
conn.Execute sql1
conn.Execute sql2
conn.CommitTrans
Refer this for more info : http://classical-asp.blogspot.com/2010/08/transaction.html
EDIT : make sure inv_date column is a datetime column
sql1 = "UPDATE tbl_inventory SET inv_qty_act = inv_qty_act - " & od_qty & ", inv_date = now() WHERE pd_id = '" & pd_id & "'"

how to work BETWEEN query by dates in Crystal Report usingvb.net

Can someone help to edit this RecordSelectionFormula? It's giving an error, something with date format...
Date value from database is:
data type = datetime (yyyy-mm-dd)
and
datetimepicker is formatdatetime(now,vbshortdate)
And my code-snippet is:
"{tblTimeLog.dtr_name}='" & cboName.Text & "' and
{tbltimelog.dtr_datelog} Between '" & DateValue(DateTimePicker1.Text) &
"' AND '" & DateValue(DateTimePicker2.Text) & "'"
The first line of my code (dtr_name to cboname) is correct; I've checked it. But I think the problem is from filtering the date..
Change your query as
"{tblTimeLog.dtr_name}='" & cboName.Text & "' and {tbltimelog.dtr_datelog}
in '" & DateValue(DateTimePicker1.Text) & "' to '" &
DateValue(DateTimePicker2.Text) & "'"