Hello I am using VB6 and mysql and I am facing a problem in searching through dates its not fetching records
Here is my code
Dim GetDateNow As Date
Dim GetDateTen As String
Dim SetDateTen As Date
GetDateNow = Format$(Now, "yyyy-mm-dd")
GetDateTen = Now - 15
SetDateTen = Format$(GetDateTen, "yyyy-mm-dd")
Dim rs As New Recordset
SQL = "SELECT * FROM CreditPayLog WHERE payment_user='" & Label5.Caption & "' AND payment_date BETWEEN '" & Format(SetDateTen, "yyyy-MM-dd") & "' AND '" & Format(GetDateNow, "yyyy-MM-dd") & "'"
Also i have used debug.print function to see the query all looks good but its not fetching data here is the query
SELECT * FROM CreditPayLog WHERE payment_user='1222150322' AND payment_date BETWEEN '2016-04-06' AND '2016-04-21'
payment_date is DATETIME column in mysql
Your sql is creating a text/string value for date... you need to cast as a date. Not sure how in mySQL but in MSSql you would use Convert(datetime, 'yourDateAsString', 103)
Related
Dim myDate As Date
myDate = CDate(Date)
SQL = " Select Count(PONumber) As OverDue from tblPO where POExpireDate < '" & myDate & "'"
Set db = CurrentDb
Set rs = db.OpenRecordset(SQL)
Im running this code....at OpenRecordset its shows error...
Data Type Mismatch in Criteria Expression.
What should i do now...why this error Coming?
As Date returns the current date, all you need is to use Date() directly in the SQL:
SQL = "Select Count(PONumber) As OverDue from tblPO where POExpireDate < Date()"
I have created a database within Microsoft access and with it a form.
I have a button attached to the form and when it's clicked I want to insert a new record into the table customerHeader.
There are four fields within that table one being an autonumber. In my SQL statement, I don't include the orderNumber since it is an autofield.
When I try to click the button and the on click event is executed, I get an error saying that Microsoft Access cannot append the records in the append query.
Any ideas, I have looked everywhere and I have not been able to find the solution.
Private Sub addOrder_Click()
Dim mySql As String
Dim rowNum As Integer
Dim recCount As Long
Dim orderNumber As Long
Dim myBool As Boolean
Dim todayDate As Date
todayDate = CDate(Date)
myBool = False
MsgBox todayDate
rowNum = Form.CurrentRecord
rowNum = CInt(rowNum - 1)
'DoCmd.GoToRecord , , acNewRec
mySql = "INSERT INTO orderHeader (orderDate,custNumber,printed) VALUES (" & todayDate & "," & rowNum & "," & myBool & ")"
DoCmd.RunSQL mySql
Me!orderNum.Requery
You don't have to much around - just use the ISO sequence for the format of the string expression for the date value:
mySql = "INSERT INTO orderHeader (orderDate,custNumber,printed) VALUES (#" & Format(todayDate, "yyyy\/mm\/dd") & "#," & rowNum & "," & myBool & ")"
However, it it's today, simply use:
mySql = "INSERT INTO orderHeader (orderDate,custNumber,printed) VALUES (Date()," & rowNum & "," & myBool & ")"
I'm trying to query my database based on the current user and the current date. I currently am getting no results from my query and I think the problem is with my formatting of the current date in vb.net. The query as is
query = "SELECT movie_name from movie2 Where movie_id=(SELECT movie_id from rental where client_username='" & currentUser & "' AND start_date<='" & Format(Now, "yyyy-MM-dd") & "' AND return_date>='" & Format(Now, "yyyy-MM-dd") & "')"
I know the rest of the query works (except calling the currentUser) as I have tested it on the mySql server. I have called currentUser before, which is saved through a variable in a module, without any hassle so I feel the problem has to be with my dates.
I don't know how to format the current date so mySql will receive it properly?
I have also tried the code
query = "SELECT movie_name from movie2 Where movie_id=(SELECT movie_id from rental where client_username='" & currentUser & "' AND start_date<='" & DateTime.Now.ToString("yyyy-MM-dd") & "' AND return_date>='" & DateTime.Now.ToString("yyyy-MM-dd") & "')"
with no success. Any help would be greatly appreciated!
Thanks, Alan.
The problem is the usual bad practice to use string concatenation when building sql command.
And the remedy is always the same. A parameterized query.
query = "SELECT movie_name from movie2 Where movie_id=" & _
"(SELECT movie_id from rental where client_username=#name" &_
" AND start_date<=#date AND return_date>=#date"
Using cmd = new MySqlCommand(query, connection)
cmd.Parameters.Add("#name", MySqlDbType.VarChar).Value = currentUser
cmd.Parameters.Add("#date", MySqlDbType.DateTime).Value = DateTime.Now
Using reader = cmd.ExecuteReader()
.... now read your data ....
End Using
End Using
With string concatenation you are vulnerable to Sql Injection and when forcing the conversion of decimals or date to a string you are an easy victim of wrong conversions. Parameters avoid all of that
EDIT: With an MySqlDataAdapter
query = "SELECT movie_name from movie2 Where movie_id=" & _
"(SELECT movie_id from rental where client_username=#name" &_
" AND start_date<=#date AND return_date>=#date"
Using cmd = new MySqlCommand(query, connection)
cmd.Parameters.Add("#name", MySqlDbType.VarChar).Value = currentUser
cmd.Parameters.Add("#date", MySqlDbType.DateTime).Value = DateTime.Now
Using adapter = new MySqlDataAdapter(cmd)
Dim dt = new DataTable()
adapter.Fill(dt)
End Using
End Using
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.
I have two datetimepicker, startDate is stored datetimepicker1 value and endDate is stored datetimepicker2 value.
I want to to get the data between startDate and endDate from database.
Dim bSql As String = "select date, sum(total_price) from bill where Date = '" & Format(startDate, "yyyy/MM/dd") & " and Date='" & Format(endDate, "yyyy/MM/dd") & "'"
i tried the code above but it can't work. Anyone can help me?
If you're trying to find a string format for a date at all, you've already lost. Try this:
Dim bSql As String = "select date, sum(total_price) from bill where Date >= #startDate and Date < #endDate;"
Using cn As New MySqlConnection("connection string here"), _
cmd As New MySqlCommand(bSql, cn)
cmd.Parameters.Add("#startDate", SqlDbType.DateTime).Value = startDate
cmd.Parameters.Add("#endDate", SqlDbType.DateTime).Value = endDate.AddDays(1)
cn.Open()
'...
End Using
No formatting required or wanted.
Try this, using the SQL BETWEEN operator, which allows you to specify the lower and upper bounds of a range.
Dim bSql As String = "select date, sum(total_price) from bill where Date BETWEEN '" & startDate.ToString("yyyy/MM/dd") & "' AND '" & endDate.ToString("yyyy/MM/dd") & "' GROUP BY date;"
You will also need to apply a grouping to use the aggregate function "SUM":
-- find all dates with sales and the total prices on each date
SELECT [date], SUM(total_price) AS [TotalPrice]
FROM bill
WHERE [date] BETWEEN '2013-01-01' AND '2013-12-31' -- use appropriate date format here
GROUP BY [date];
in data base i have DD/MM/YYYY
solution is to make in VB MM/DD/YYYY
this is a CODE
oldbc.CommandText = "select * from recette where Date between #10/09/2015# and #10/011/2015#"
Try formatting your dates like this (you will need to use the Value of the DateTimePicker as well):
Format(startDate.Value, "yyyy-MM-dd")
A better option is to use a parameterised query and then you don't have to format the date into any particular format. More info here: How do I create a parameterized SQL query? Why Should I?
Dim bSql As String = "select date, sum(total_price) from bill where Date = " & DateTimePicker1.Text & " and Date=" & DateTimePicker1.Text & ""
Set the datetime picker date format.
I hope it will helpful for you...
Another possibility is to make a little function to format DATE variable and return correct date string syntax for MySQL.
Public Function MySQLDate(inDate As Date) As String
Return "'" & inDate.ToString(format:="yyyy'/'MM'/'dd") & "'"
End Function
Now your query is more readable, easier to create, and looks like this:
Dim bSql As String = "select date, sum(total_price) from bill where Date BETWEEN " & MySQLDate(startDate) & " and " & MySQLDate(endDate)