How to Count Values in Access Database using Visual Studio VB.NET - vb.net

I have an access database that stores data in a table (called DataCollection) that is logged by a Visual Studio program. I have been struggling to find a solution to how to count the number records that contain specific values in specific columns.
For example, in my database, there are two columns. One column is titled "M/Y OF LOG" and it returns dates formatted like this "1/1/2021". The other column is titled "MISSED PART", and it can return only two values, "Missed Part" or "NEATOL". Ideally, I would like to count, using Visual Studio, the number of times "Missed Part" occurs in a certain month.
Does anyone have any ideas on how the code for this may look, or if counting like this is even possible?
My connection string:
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb"
Preview of my Access database formatting:
You will see there are many more columns than mentioned. Please ignore any columns other than "M/Y of Log" and "Missed Part"
I have these variables in my code. They will be the variables that hole the return values from the database, used as y values in graphs to display the data:
Private Function CountMissedParts() Handles MyBase.Load
Dim sql = $"SELECT COUNT(*)
FROM DataCollection
WHERE [MISSED PART] = 'Missed Part'
AND [M/Y OF LOG] = #MY_OF_LOG;"
Dim JANmyOfLog = #1/1/2021#
Dim FEBmyOfLog = #2/1/2021#
Dim MARmyOfLog = #3/1/2021#
Dim APRmyOfLog = #4/1/2021#
Dim MAYmyOfLog = #5/1/2021#
Dim JUNmyOfLog = #6/1/2021#
Dim JULmyOfLog = #7/1/2021#
Dim AUGmyOfLog = #8/1/2021#
Dim SEPmyOfLog = #9/1/2021#
Dim OCTmyOfLog = #10/1/2021#
Dim NOVmyOfLog = #11/1/2021#
Dim DECmyOfLog = #12/1/2021#
Dim count As Integer
Dim JanuaryMP As Double
Dim FebruaryMP As Double
Dim MarchMP As Double
Dim AprilMP As Double
Dim MayMP As Double
Dim JuneMP As Double
Dim JulyMP As Double
Dim AugustMP As Double
Dim SeptemberMP As Double
Dim OctoberMP As Double
Dim NovemberMP As Double
Dim DecemberMP As Double
Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb"),
command As New OleDbCommand(sql, connection)
JanuaryMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = JANmyOfLog
FebruaryMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = FEBmyOfLog
MarchMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = MARmyOfLog
AprilMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = APRmyOfLog
MayMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = MAYmyOfLog
JuneMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = JUNmyOfLog
JulyMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = JULmyOfLog
AugustMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = AUGmyOfLog
SeptemberMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = SEPmyOfLog
OctoberMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = OCTmyOfLog
NovemberMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = NOVmyOfLog
DecemberMP = command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = DECmyOfLog
connection.Open()
count = command.ExecuteScalar()
' set 0,0
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(0, 0)
' other points
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(1, JanuaryMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(2, FebruaryMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(3, MarchMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(4, AprilMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(5, MayMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(6, JuneMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(7, JulyMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(8, AugustMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(9, SeptemberMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(10, OctoberMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(11, NovemberMP)
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(12, DecemberMP)
End Using
NotInEpicorCHRT.ChartAreas(0).AxisX.Minimum = 0.0
NotInEpicorCHRT.ChartAreas(0).AxisX.Maximum = 12
NotInEpicorCHRT.ChartAreas(0).AxisX.Interval = 1
NotInEpicorCHRT.ChartAreas(0).AxisY.Minimum = 0.0
NotInEpicorCHRT.ChartAreas(0).AxisY.Maximum = 45
NotInEpicorCHRT.ChartAreas(0).AxisY.Interval = 5
End Function

Dim sql = $"SELECT COUNT(*)
FROM DataCollection
WHERE [MISSED PART] = 'Missed Part'
AND [M/Y OF LOG] = #MY_OF_LOG"
Dim myOfLog = #1/01/2021#
Dim count As Integer
Using connection As New OleDbConnection("connection string here"),
command As New OleDbCommand(sql, connection)
command.Parameters.Add("#MY_OF_LOG", OleDbType.Date).Value = myOfLog
connection.Open()
count = CInt(command.ExecuteScalar())
End Using
That example uses a hard-coded date but you can get the date value from anywhere you like. A Date is a Date.

There is no need to define variable for each month, you can use loop instead.
Check the following code:
Dim count As Integer
Dim lst As List(Of Double) = New List(Of Double)
Dim sql = $"SELECT COUNT(*) FROM DataCollection WHERE [MISSED PART] = 'Missed Part' AND [M/Y OF LOG] = #MY_OF_LOG;"
Using connection As New OleDbConnection("your connection String"),
command As New OleDbCommand(sql, connection)
connection.Open()
For i As Integer = 1 To 12
command.Parameters.Clear()
Dim dtime = New DateTime(2021, i, 1).ToString("MM/dd/yyyy")
command.Parameters.AddWithValue("#MY_OF_LOG", dtime)
count = command.ExecuteScalar()
lst.Add(count)
Next
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(0, 0)
For j As Integer = 1 To lst.Count
NotInEpicorCHRT.Series("Missed Part").Points.AddXY(j, lst(j - 1))
Next
End Using
NotInEpicorCHRT.ChartAreas(0).AxisX.Minimum = 0.0
NotInEpicorCHRT.ChartAreas(0).AxisX.Maximum = 12
NotInEpicorCHRT.ChartAreas(0).AxisX.Interval = 1
NotInEpicorCHRT.ChartAreas(0).AxisY.Minimum = 0.0
NotInEpicorCHRT.ChartAreas(0).AxisY.Maximum = 45
NotInEpicorCHRT.ChartAreas(0).AxisY.Interval = 5

Well, I assume that the date column is a in fact a actual date column. That way, your display of the date does NOT matter. As a general rule, all dates are stored in a internal format - you don't care about the external.
So, all we REALLY need is the month, and year you want.
So, build a form with two text boxes. One text box has year, the other the month.
Say like this:
So, this code would prompt you for the year, then the month, and then spit out the results.
the button code can be this, and it will give a total for both Missed and NeatOL
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strSQL As String
strSQL = "SELECT abs(SUM([Missed Part] = 'Missed Part')) As MissPCount " &
"abs(SUM([Missed Part] = 'NEATOL')) As MissNCount " &
"FROM DataCollection WHERE Month([M/Y OF LOG]) = #Month " &
"AND Year([M/Y OF LOG]) = #Year"
Using cmdSQL As New OleDbCommand(strSQL,
New OleDbConnection(My.Settings.TestDB))
cmdSQL.Parameters.Add("#Month", OleDbType.Integer).Value = txtMonth.Text
cmdSQL.Parameters.Add("#Year", OleDbType.Integer).Value = txtYear.Text
cmdSQL.Connection.Open()
Dim rstTable As New DataTable
rstTable.Load(cmdSQL.ExecuteReader)
' display the reuslts in the two text boxes.
With rstTable.Rows(0)
txtNeatCount.Text = .Item("MissNCount")
txtPartCount.Text = .Item("MissPCount")
End With
End Using
End Sub
Edit:
As a follow up? It looks like you have a single column that is ALWAYS set to the 1st of the month. Give this, and that I used month() and year() functions? Well, using month() and year() is great for things like say who has a birthday this month, ( we can use month() and day() to extract the month, day and ignore the year.
HOWEVER, we should STILL include a valid date range. The reason is of course "indexing" or the so called term "sargable". The above query as written can't use indexing for the results. Thus for a large table, the posted query will run quite slow.
A better (and much faster) performing SQL query would be this, assuming there is a index on the MY/Y column. Thus, I suggest this solution:
strSQL = "SELECT abs(SUM([Missed Part] = 'Missed Part')) As MissPCount " &
"abs(SUM([Missed Part] = 'NEATOL' As MissNCount " &
"FROM DataCollection WHERE [M/Y OF LOG] = #MyDate "
Dim dtMyDate As Date = DateSerial(txtYear.Text, txtMonth.Text, 1)
Using cmdSQL As New OleDbCommand(strSQL,
New OleDbConnection(My.Settings.TestDB))
cmdSQL.Parameters.Add("#MyDate", OleDbType.DBDate).Value = dtMyDate
cmdSQL.Connection.Open()
etc. etc. etc. etc.

I think the following code will get you the data you need. Ask another question if you need the data table columns translated to the chart.
Private Sub OPCode()
Dim sql = $"SELECT [M/Y OF LOG], COUNT([Missed Part]) As Total
FROM [Data Collection]
WHERE [MISSED PART] = 'MISSED PART'
GROUP BY [M/Y OF LOG]
ORDER BY [M/Y OF LOG];"
Dim dt As New DataTable
Using cn As New OleDbConnection(My.Settings.DataCollection),
cmd As New OleDbCommand(sql, cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
DataGridView1.DataSource = dt
End Sub

Related

I want to query between two dates in sqlite with two textbox(textbox4.text and textbox5.text = variable) in form

Dim query As String = "select * from mytable where strftime('%Y-%m-%d',tarih) between 'TextBox4.text' and 'TextBox5.text' "
Can not assign query variable ?
Datagridview result = empty
info :Datagridview date format = 2018-01-01 17:42:24
Dim btarihyil As String
btarihyil = DateTimePicker2.Value.ToString.Substring(6, 4)
Dim btarihay As String
btarihay = DateTimePicker2.Value.ToString.Substring(3, 2)
Dim btarihgun As String
btarihgun = DateTimePicker2.Value.ToString.Substring(0, 2)
TextBox4.Text = btarihyil & "-" & btarihay & "-" & btarihgun
Dim bttarihyil As String
bttarihyil = DateTimePicker3.Value.ToString.Substring(6, 4)
Dim bttarihay As String
bttarihay = DateTimePicker3.Value.ToString.Substring(3, 2)
Dim bttarihgun As String
bttarihgun = DateTimePicker3.Value.ToString.Substring(0, 2)
TextBox4.Text = btarihyil & "-" & btarihay & "-" & btarihgun
TextBox5.Text = bttarihyil & "-" & bttarihay & "-" & bttarihgun
Dim Yol As String = "Data Source=database1.s3db;version=3;new=False"
Using MyConn As New SQLiteConnection(Yol)
If (MyConn.State = ConnectionState.Closed) Then
MyConn.Open()
End If
Dim Sorgu As String = "select * from mytable where strftime('%Y-%m-%d',tarih) between 'TextBox4.text' and 'TextBox5.text' "
Using MyCmd As New SQLiteCommand(Sorgu, MyConn)
Dim Da As New SQLiteDataAdapter(MyCmd)
Dim Ds As New DataSet
Dim Dt As New DataTable
Ds.Reset()
Da.Fill(Ds)
Dt = Ds.Tables(0)
Dim Bs As New BindingSource With {.DataSource = Dt}
DataGridView1.DataSource = Bs
Bs.MoveLast()
MyConn.Close()
MyCmd.Dispose()
MyConn.Dispose()
End Using
End Using
End Sub
You are using "TextBox4.Text" and "TextBox5.Text" literally in the query. That will not pass the values of textboxes to the query. So it will result in error.
Also you need to use parameterized query to avoid syntax errors.
Also I am not sure why you are using strftime function. That function is used just for formatting.
Following the code I re-written using parameterzied query approach.
Dim Yol As String = "Data Source=database1.s3db;version=3;new=False"
Using MyConn As New SQLiteConnection(Yol)
If (MyConn.State = ConnectionState.Closed) Then
MyConn.Open()
End If
Dim Sorgu As String = "select * from mytable where tarih between #startDate and #endDate "
Using MyCmd As New SQLiteCommand(Sorgu, MyConn)
Dim startDate as new SQLiteParameter("#startDate")
startDate.Value = DateTimePicker2.Value
Dim endDate as new SQLiteParameter("#endDate")
endDate.Value = DateTimePicker3.Value
MyCmd.Parameters.Add(startDate)
MyCmd.Parameters.Add(endDate)
Dim Da As New SQLiteDataAdapter(MyCmd)
Dim Ds As New DataSet
Dim Dt As New DataTable
Ds.Reset()
Da.Fill(Ds)
Dt = Ds.Tables(0)
Dim Bs As New BindingSource With {.DataSource = Dt}
DataGridView1.DataSource = Bs
Bs.MoveLast()
MyConn.Close()
MyCmd.Dispose()
MyConn.Dispose()
End Using
End Using
This should help you resolve your issue.

ID numbers with text

Help. So this is my code and my database table. The itemcat is based on the tbl_category and it gives IN, PF, SS, SV + the number of the id.
As you can see the. PF003 and SS003 have the same number. How to change it to SS004 automatically?
Private Sub AutoGenerateID()
Dim mysqlconnection As MySqlConnection
Dim command As New MySqlCommand
mysqlconnection = New MySqlConnection()
mysqlconnection.ConnectionString = "Server=localhost;User=root;Password=;Database=debis"
command.Connection = mysqlconnection
mysqlconnection.Open()
da = New MySqlDataAdapter("select * from tbl_category where catname = '" & cmbcat.SelectedItem & "'", con)
ds.Reset()
da.Fill(ds)
Dim sqlquery = "select Max(itemid) from tbl_item "
command.CommandText = sqlquery
Dim ID As Integer
Dim value As String
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
value = command.ExecuteScalar().ToString()
If String.IsNullOrEmpty(value) Then
value = (ds.Tables(0).Rows(i).Item("catid")) + "0000"
End If
value = value.Substring(3)
Int32.TryParse(value, ID)
ID = ID + 1
value = (ds.Tables(0).Rows(i).Item("catid")) + ID.ToString("D3")
tbitemid.Text = value
Next
command.Dispose()
mysqlconnection.Close()
mysqlconnection.Dispose()
End Sub
This is the image of the table
If the first part is always 2 characters long then you can split your string to increment only the number part.
intValue = val(strings.mid(value,3))
intValue = intValue + 1
Also, you should hide/change your server address, user/password when you post your code on the web.

Availability using Access Database

I am attempting to make a hotel booking system. However the availability has got me a bit confused. I have about 15 buttons which I am able to save the number to the database but when the form loads/ date changed. I need the button to stay red and be unclickable. For example if I had a room 11 booked from 3/06/17 to 5/06/17 then I'd need the button to remain red from the 3/06/17 to 4/06/17 since the room is able to still be booked on the 5/06/17 after cleaning. I hope this makes sense. Below is the code I am using to try to do this. The code does run however the button does not turn red.
I was thinking does my SQL statement need to be changed but I'm not too sure. I'm pretty new to coding so an explanation would be helpful. Thanks.
Private Sub ReadRecords()
Dim btn As Button = Nothing
Dim BookingFound As Boolean = False
Using MyConn As New OleDbConnection
MyConn.ConnectionString = connString
MyConn.Open()
Dim check As String = "SELECT COUNT(*) FROM [BookingInformation] WHERE [Date In] = '" & dtpDateIn.Value.Date & "' AND [Date Out] = '" & dtpDateOut.Value.Date & "'"
Dim BookingExists As Boolean = False
Dim command As OleDbCommand = New OleDbCommand(check, MyConn)
Using reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
If reader(0) = 0 Then
BookingExists = False
Else
BookingExists = True
End If
End While
End Using
If BookingExists = True Then
Dim getData As String = "SELECT * FROM [BookingInformation] WHERE [Date Out] = '" & dtpDateOut.Text & "'"
Dim command2 As OleDbCommand = New OleDbCommand(getData, MyConn)
Using reader As OleDbDataReader = command2.ExecuteReader()
While reader.Read()
BookingFound = True
strDateIn = reader("Date In").ToString()
strDateOut = reader("DateOut").ToString
strRoomNumber = reader("Room Number").ToString
End While
If BookingFound = True Then
btn.BackColor = Color.Red
End If
End Using
End If
MyConn.Close()
End Using
End Sub
Private Sub Room_Booking_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ReadRecords()
End Sub
You should make your access database understand your input as date, access database is very sensitive to datatypes, for example if you write
"SELECT * FROM [user_tb] WHERE user_id=1"
Your code will work fine if your user_id data type is autonumber.
so try
Dim getData As String = "SELECT * FROM [BookingInformation] WHERE [Date Out] = #" & dtpDateOut.Text & "#"
Instead of
Dim getData As String = "SELECT * FROM [BookingInformation] WHERE [Date Out] = '" & dtpDateOut.Text & "'"
That is replace ' with #

Excel to Sql adding certain rows to sql server

cmd.CommandText = (Convert.ToString("SELECT * From [") & excelSheet) & "] Order By" & columnCompany & "OFFSET 0 ROWS FETCH NEXT 100000 ROWS ONLY "
I am trying to map data form excel to an sql table but the excel table has a lot of records in it, so I run out of memory if I try to add all of the records at once. So what I am trying to do is use this line of code
cmd.CommandText = (Convert.ToString("SELECT * From [") & excelSheet) & "] Order By B OFFSET 0 ROWS FETCH NEXT 100000 ROWS ONLY "
So that 100000 row will be added. Then I intend to clear the data table and do the same only from 100001 to 200000 and so on.
But I cant get this to work correctly, it works fine when it is just SELECT TOP 100000 FROM... but then I dont know how to get the next 100000 record. I think the Order By is where the issue is as I'm not sure how to get the correct column.
Here is my code Below
Private Sub btnMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMap.Click
'Allows columns to be moved mapped in the correct order
Dim nbColumnsToTransfer As Integer = dgvMapped.Columns.GetColumnCount(1)
Dim indexes As List(Of Integer) = (From column As DataGridViewColumn In dgvLoadedata.Columns.Cast(Of DataGridViewColumn)() _
Take nbColumnsToTransfer _
Order By column.DisplayIndex _
Select column.Index).ToList()
Dim columnCompany As Integer = indexes(0)
Dim columnEmail As Integer = indexes(1)
Dim columnFirstName As Integer = indexes(2)
Dim columnSurname As Integer = indexes(3)
Dim columnPostCode As Integer = indexes(4)
Dim columnTelephone As Integer = indexes(5)
Dim columnEmployeeBand As Integer = indexes(6)
Dim DeleteConnection As New Data.SqlClient.SqlConnection
DeleteConnection.ConnectionString = "Data Source=DC01\SQLEXPRESS;Initial Catalog=YCM;User ID=sa;Password=S0rtmypc!"
DeleteConnection.Open()
Dim sqlDTM As String = "Delete From TempMapped"
Dim command As New SqlCommand(sqlDTM, DeleteConnection)
command.ExecuteNonQuery()
Dim sqlDTSR As String = "Delete From TempSplitReq"
command = New SqlCommand(sqlDTSR, DeleteConnection)
command.ExecuteNonQuery()
DeleteConnection.Close()
If chkContactSplitReq.Checked = True Then
Dim dt As New DataTable()
Using con As New OleDbConnection(conString)
Using cmd As New OleDbCommand()
Using oda As New OleDbDataAdapter()
cmd.CommandText = (Convert.ToString("SELECT * From [") & excelSheet) & "] Order By" & columnCompany & "OFFSET 0 ROWS FETCH NEXT 100000 ROWS ONLY ""
cmd.Connection = con
con.Open()
oda.SelectCommand = cmd
oda.Fill(dt)
con.Close()
End Using
End Using
End Using
Dim connection As String = "Data Source=DC01\SQLEXPRESS;Initial Catalog=YCM;User ID=sa;Password=S0rtmypc!"
Using cn As New SqlConnection(connection)
cn.Open()
Using copy As New SqlBulkCopy(cn)
copy.ColumnMappings.Add(columnCompany, 0)
copy.ColumnMappings.Add(columnEmail, 1)
copy.ColumnMappings.Add(columnFirstName, 2)
copy.ColumnMappings.Add(columnPostCode, 3)
copy.ColumnMappings.Add(columnTelephone, 4)
copy.DestinationTableName = "TempSplitReq"
copy.WriteToServer(dt)
End Using
End Using
Dim SplitConnection As New Data.SqlClient.SqlConnection
SplitConnection.ConnectionString = "Data Source=DC01\SQLEXPRESS;Initial Catalog=YCM;User ID=sa;Password=S0rtmypc!"
SplitConnection.Open()
Dim sqlSplit As String = "TempSplitReq_SaveToMapped"
Dim commandSplit As New SqlCommand(sqlSplit, SplitConnection)
commandSplit.ExecuteNonQuery()
SplitConnection.Close()
Else
Dim dt As New DataTable()
Using con As New OleDbConnection(conString)
Using cmd As New OleDbCommand()
Using oda As New OleDbDataAdapter()
cmd.CommandText = (Convert.ToString("SELECT * From [") & excelSheet) + "] "
cmd.Connection = con
con.Open()
oda.SelectCommand = cmd
oda.Fill(dt)
con.Close()
End Using
End Using
End Using
Dim connection As String = "Data Source=DC01\SQLEXPRESS;Initial Catalog=YCM;User ID=sa;Password=S0rtmypc!"
Using cn As New SqlConnection(connection)
cn.Open()
Using copy As New SqlBulkCopy(cn)
copy.ColumnMappings.Add(columnCompany, 0)
copy.ColumnMappings.Add(columnEmail, 1)
copy.ColumnMappings.Add(columnFirstName, 2)
copy.ColumnMappings.Add(columnSurname, 3)
copy.ColumnMappings.Add(columnPostCode, 4)
copy.ColumnMappings.Add(columnTelephone, 5)
copy.DestinationTableName = "TempMapped"
copy.WriteToServer(dt)
End Using
End Using
End If
MsgBox("Data Mapping Complete")
End Sub
A SqlBulkCopy object has a BatchSize property. I have a similar situation and set
copy.BatchSize = 1000
And mine works with a large spreadsheet import. Adjust this property in your environment for better performance.

help date format in vb.net

Dim Con As OleDb.OleDbConnection
Dim Sql As String = Nothing
Dim Reader As OleDb.OleDbDataReader
Dim ComboRow As Integer = -1
Dim Columns As Integer = 0
Dim Category As String = Nothing
Dim oDatumMin As Date
Dim column As String
column = Replace(TxtDateMax.Text, "'", "''")
'oDatumMin = Convert.ToDateTime(TxtDateMin.Text)
oDatumMin = DtpMin.Value
Dim oPath As String
oPath = Application.StartupPath
' Select records.
Con = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & oPath & "\trb.accdb;")
Dim cmd As New OleDb.OleDbCommand
'Dim data_reader As OleDbDataReader = cmd.ExecuteReader()
Sql = ("SELECT * FROM " & cmbvalue & " WHERE Datum>='" & oDatumMin & "'")
cmd = New OleDb.OleDbCommand(Sql, Con)
Con.Open()
Reader = cmd.ExecuteReader()
Do While Reader.Read()
Dim new_item As New ListViewItem(Reader.Item("Datum").ToString)
new_item.SubItems.Add(Reader.Item("Steleks i krpe za cišcenje-toal papir-ubrusi-domestos").ToString)
new_item.SubItems.Add(Reader.Item("TEKUCINA ZA CIŠCENJE PLOCICA").ToString)
new_item.SubItems.Add(Reader.Item("KESE ZA SMECE").ToString)
new_item.SubItems.Add(Reader.Item("OSTALO-džoger-spužva za laminat").ToString)
new_item.SubItems.Add(Reader.Item("PAPIR").ToString)
LvIzvjestaj.Items.Add(new_item)
Loop
' Close the connection.strong text
Con.Close()``
when i select table,(cmbvalue) from combobox
and when i select date from datetime picker (dtp) or in last case from texbox converted to date and time sql looks like this "SELECT * FROM Uprava WHERE Datum>='2.6.2010 10:28:14'"
and all query looks ok but am geting
Data type mismatch in criteria expression. error for date (oDatumMin) when excute
column in access is also set to date
i have no idea what else to try
I suspect this is a localisation issue. Try changing the date format to one which should be universally recognised:
SELECT * FROM Uprava WHERE Datum >='2010/06/02 10:28:14'
[What is your server locale/code page?]
Dim sql As String = "SELECT * FROM MyTable WHERE Datum >= ?"
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql)
cmd.Parameters.Add(New OleDb.OleDbParameter("#Datum", OleDb.OleDbType.Date))
cmd.Parameters("Datum").Value = oDatumMin
http://msdn.microsoft.com/de-de/library/bbw6zyha.aspx