I need to get the records that will be ended after two days
but always I got an empty datagridview.
I have tried this code:
Dim after2days As Date = Today.Date.AddDays(3)
Dim sqlstr As String = "SELECT * FROM tblvac where vend between " & Today.Date & " and " & after2days & " "
Dim da As New OleDbDataAdapter(sqlstr, Conn)
ds.Reset()
da = New OleDbDataAdapter(sqlstr, Conn)
da.Fill(ds)
dgv.DataSource = ds
Conn.Close()
Why is this happening and how can I fix it?
Access SQL includes functions, Date() and DateAdd(), which can give you the date range you want for your query.
Dim sqlstr As String = "SELECT * FROM tblvac where vend between Date() and DateAdd('d', 3, Date());"
If you prefer to pass date values from your VB.Net code to the db engine, use a parameter query so that you needn't bother about date format and delimiters. Just supply valid Date/Time values for the parameters.
You are building your query as a string and thus need to use the convert to date function... for MS Access its DateValue see http://www.techonthenet.com/access/functions/date/datevalue.php
Try
Dim sqlstr As String = "SELECT * FROM tblvac where vend between
DateValue('" & Today.Date & "') and DateValue('" & after2days & "') "
As commented by HansUp... this solution needs to have the date format as mm/dd/yyyy or yyyy-mm-dd
Related
So I'm trying to select record with a specific date range ex. 5/21/2019 - 5/30/2019 and put the records into a datagridview.
I'm using Ms. Access and I don't know many things about SQL.
DA = New OleDb.OleDbDataAdapter("SELECT * FROM table where date >= " & DateValue(FirstDate) & " AND date <= " & DateValue(SecondDate) & "", conn)
DS = New DataSet
DS.Clear()
DA.Fill(DS, "table")
DataGridView1.DataSource = DS.Tables("table")
It doesn't give me an error message, but the datagridview doesn't show anything. Thanks for your response
In MS Access, the date delimiter is a hash (#).
So have to change your query to:
"SELECT * FROM table where date >= #" & DateValue(FirstDate) & "# AND date <= #" & DateValue(SecondDate) & "#"
Or you use the between operator:
"Select * FROM table WHERE [Date] Between #" & DateValue(FirstDate) & "# and #" & DateValue(SecondDate) & "#";
A better way to handle this is to simply use parameters which is commonly recommended by many programmers.
da = New OleDb.OleDbDataAdapter("SELECT * FROM table where date >= #StartDate AND date <= #EndeDate ", conn)
'If you show how you declare and set FirstDate and SecondDate we can drop the DateValue function'
da.SelectCommand.Parameters.Add("#StartDate", OleDb.OleDbType.Date).Value = DateValue(FirstDate)
da.SelectCommand.Parameters.Add("#EndeDate", OleDb.OleDbType.Date).Value = DateValue(SecondDate)
ds = New DataSet
DS.Clear()
DA.Fill(DS, "table")
DataGridView1.DataSource = DS.Tables("table")
SELECT ID
FROMTestInfo
WHERE CapturedDate BETWEEN ‘2012-03-27’ AND ‘2012-03-28 ’
I'm having problems trying to make this work, I have this query on an application that Im writing in vb.net 2012:
Dim strSql As String = " SELECT * FROM Table_Master WHERE field & "'= ('" & txtCadena.Text & "')"
What I need is to have the option to choose the field that I'm querying writing the field name on a textbox.
Maybe something like:
strSql As String = string.format("SELECT * FROM Table_Master WHERE [{0}] = '{1}'", txtField.text, txtFieldValue.text.replace("'","''"))
This should work (only for text, not dates, numbers etc) but you have to know that it is not the best practice.
I finally made it doing this.
Dim Query As String
Dimm DT As DataTable = New DataTable
Query = "select Actual, Description, Unit_of_measurement from Table_ARTIClES WHERE NUMPART = '" & txtPartNum.Text & "'"
Dim Table As SqlDataAdapter = New SqlDataAdapter(Query, conn)
Table.Fill(DT)
lblInventory.Text = DT.Rows(0)("Actual").ToString
I have a table in an Access database with information like the following:
DATE SERIAL RESULT
1/5/2016 5299 PASS
1/5/2016 5371 PASS
1/6/2016 5280 PASS
1/6/2016 3962 FAIL
1/7/2016 1325 PASS
I'm trying to run a query to display a count of the SERIAL's that have a RESULT of "PASS" and plot them for each day. The closest I have managed to get is the following:
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim query As String
Dim FullDatabasePath As String = "C:\db.accdb"
Dim dbProvider As String = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
Dim dbSource As String = " Data Source = " & FullDatabasePath
Dim table As String = "tblQA"
query = "SELECT [DATE], (SELECT COUNT([SERIAL]) FROM " & table & " WHERE [DATE] BETWEEN #" & dtpFrom.Value.ToString("MM/dd/yyyy") & "# And #" & dtpTo.Value.ToString("MM/dd/yyyy") & "# AND RESULT = 'PASS') as RowCount FROM [" & table & "] WHERE DATE BETWEEN #" & dtpFrom.Value.ToString("MM/dd/yyyy") & "# And #" & dtpTo.Value.ToString("MM/dd/yyyy") & "# AND RESULT = 'PASS'"
Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection
con.ConnectionString = dbProvider & dbSource
da = New OleDb.OleDbDataAdapter(query, con)
ds.Clear()
con.Open()
da.Fill(ds, table)
con.Close()
Chart1.Series.Add("PASS")
Chart1.DataSource = ds.Tables(table)
Chart1.Series("PASS").XValueMember = "DATE"
Chart1.Series("PASS").YValueMembers = "RowCount"
If I select 1/5/2016 for both start and end dates using the DateTimePickers on my form (dtpFrom and dtpTo), my chart shows me 2 serials on 1/5/2016. Likewise, it shows 1 serial if I choose a start and end date of 1/6/2015. (yay)
If choosing a start date of 1/5/2016 and end date of 1/7/2016, it shows 4 serials on each of those 3 days. (boo)
How can I make it not accumulate, but instead display the number of passing serials for each day?
I believe this SQL will do what you want:
SELECT [DATE], COUNT(RESULT)
FROM Table_1
where result = 'PASS' AND [Date] BETWEEN '20160501' AND '20160701'
group by [date]
EDIT: The dates are formatted to suit MS T-SQL - you'll have to tweak these to suit Access ( and your dtp's) of course.
Dim data1 As String
Dim conn1 As SqlConnection
Dim cmd1 As New SqlCommand
Dim ada1 As New SqlDataAdapter()
Dim ds1 As New DataSet
data1 = lbldmdate.Text.ToString()
conn1 = New SqlConnection("Persist Security Info=false;User Id=justin;Data Source=ARULJUSTIN\SQLEXPRESS;Initial Catalog=firemaintain;Integrated Security=True;Pooling=False")
conn1.Open()
ada1 = New SqlDataAdapter("select Recordno, uhnumber,uhbuilding,uhlocation from hydrantmaintain WHERE unndate= '" & Today.ToString("dd/mm/yyyy") & "'", conn)
ada1.Fill(ds1)
dgnxtmdates.DataSource = ds1.Tables(0)
I am getting an error: "cannot convert from date to string"
As #Nawful pointed out, you are using "mm" (minutes) instead of "MM" (months) but it also looks like you month and day are reversed. Now, this is dependant upon your region settings for dates and specifically how you configured the SQL server but to avoid these issues, I like to do this for dates:
CONVERT(datetime, '" & Format(Value, "yyyy-MM-dd") & " 00:00:00', 102)
and this for Date and Time:
CONVERT(datetime, '" & Format(Value, "yyyy-MM-ddTHH:mm:ss") & "', 126)
Note, Value is a date or datetime data type.
In your example, it would be something like this:
ada1 = New SqlDataAdapter("select Recordno, uhnumber,uhbuilding,uhlocation from hydrantmaintain WHERE unndate= CONVERT(datetime, '" & Format(Today, "yyyy-MM-dd") & " 00:00:00', 102)", conn)
Hard to point out, but your string formatting should be dd/MM/yy. mm corresponds to minutes component.
Today.ToString("dd/MM/yyyy");
If unndate is the datetime data type in the database, you don't need to convert Today to string when querying the database.
Cause, Today is already a DateTime.
But, if unndate is a string datatype, varchar or nvarchar for example, my only advice is to use the datatype datetime for the unndate value in the first place if you want to query like that.
I'm using basic date picker for vb.net and oracle as my database. when i insert the date from basic date picker, i got this error >> ORA-01797: this operator must be followed by ANY or ALL
this is my code :
Private Function GetDate(ByVal bdp1 As Date) As DataSet
Dim connectionString As String = "Data Source = ***; User ID =***; Password =**;"
Dim sqlConnection As OracleClient.OracleConnection = New OracleClient.OracleConnection(connectionString)
Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= ('" & bdp1 & "' , 'MM/DD/YYYY') and tran_dttm > ('" & bdp1 & "', 'MM/DD/YYYY')"
Dim sqlCommand As OracleClient.OracleCommand = New OracleClient.OracleCommand(queryString, sqlConnection)
sqlCommand.CommandTimeout = 0
Dim dataAdapter As OracleClient.OracleDataAdapter = New OracleClient.OracleDataAdapter(sqlCommand)
Dim dataSet As DataSet = New DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
Try, instead:
Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= :dtm1 and tran_dttm > :dtm2"
Dim sqlCommand As OracleClient.OracleCommand = New OracleClient.OracleCommand(queryString, sqlConnection)
sqlCommand.Parameters.AddWithValue("dtm1",bdp1)
sqlCommand.Parameters.AddWithValue("dtm2",bdp1)
Which a) avoids the possibility of SQL injection, and b) Keeps the date as a date throughout, rather than mangling it to/from a string.
It doesn't fix the logical issue with your query though - where you're trying to find a row where tran_dttm is both "less than or equal" and "greater than" the same value.
Try this ..
Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= cdate('" & bdp1.Value.ToString & "') and tran_dttm > cdate('" & bdp1.Value.ToString & "')"
Try this:
Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= '" & bdp1.ToString("MM/dd/yyyy") & "' and tran_dttm > '" & bdp1.ToString("MM/dd/yyyy") & "'"
But for oracle the default datetime format is YYYY-MM-DD. You have three options:
Change the default file format for the one that suits your desires
Use the default format: bdp1.ToString("yyyy-MM-dd")
Use the todate oracle function specifiying the read format:
" to_date('" & bdp1.ToString("MM/dd/yyyy") & "', 'mm/dd/yyyy') "