missing operator in query sum expression vb net - vb.net

Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select sum[Shark Individual Weight] from FishCaught'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView1.DataSource = ds
myConnection.Close()
Hello. I am using vb.net connecting to ms access. I keep getting error 'missing operator in query expression'. I would like to know what's wrong with my sql statement.
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught Where ([OperationID]) = '" & TextBoxOpID4.Text & "'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView2.DataSource = ds
I think my SQL syntax is wrong. How should write it actually? I get this error 'Data type mismatch in criteria expression'. If i just write ' Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select Sum([Shark Individual Weight]) From FishCaught', the sum will be display. So i think there's error in WHERE clause. Any help would be appreciated, thank you.

Sum is a function and you have a disturbing single quote, thus:
"Select Sum([Shark Individual Weight]) From FishCaught"

this is your code
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select sum[Shark Individual Weight] from FishCaught'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView1.DataSource = ds
myConnection.Close()
Supposed to be the code must be like this
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select sum(The_Name_Of_The_Field) from FishCaught'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView1.DataSource = ds
myConnection.Close()
Is that so, try this FINAL
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select sum(Shark__Individual_Weight) as FishWeight from FishCaught'", myConnection)
Dim ds As DataTable = New DataTable()
da.Fill(ds)
DataGridView1.DataSource = ds
myConnection.Close()
Try to change Shark Individual Weight to Shark_Individual_Weight it will work

"Select Sum([field name]) From FishCaught" should work. Have you checked the field name in database, it should not contain any space. To make field name more readable you can replace any spaces with "_".

Related

How to populate my DataGridView from an Access database with only selected columns?

Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
myConnection.Open()
da = New OleDbDataAdapter("select * from Products", myConnection)
da.Fill(dt)
datagridview1.DataSource = dt.DefaultView
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
myConnection.Open()
da = New OleDbDataAdapter("select column-name1, column-name2, column-name3, column-
name-n from Products", myConnection)
da.Fill(dt)
Hi, you can do a select not to all columns else a few columns and in the order that you want. I hope that this was what you were looking for, else only comment. Thank you very much and happy codding. :)

SQL SELECT query not working with parameters, but works fine with concatenation

I'm trying to select the name of a customer based on the selected code from a ComboBox. When I run the following code, I get an error saying that there's no value for one or more required parameters.
sql = "SELECT [Customer_Name] FROM [Customers] WHERE [Customer_Code] = #code"
Dim cmd As New OleDb.OleDbCommand(sql, con)
cmd.Parameters.Add("#code", OleDb.OleDbType.VarChar).Value = cmbCustomer.Text
Dim da As New OleDb.OleDbDataAdapter(sql, con)
Dim ds As New DataSet
da.Fill(ds)
txtCustomer.Text = ds.Tables(0).Rows(0).Item("Customer_Name")
However, when I run the same query but without the parameter, it works fine.
sql = "SELECT [Customer_Name] FROM [Customers] WHERE [Customer_Code] = '" & cmbCustomer.Text & "'"
Dim cmd As New OleDb.OleDbCommand(sql, con)
Dim da As New OleDb.OleDbDataAdapter(sql, con)
Dim ds As New DataSet
da.Fill(ds)
txtCustomer.Text = ds.Tables(0).Rows(0).Item("Customer_Name")
Is there something really obvious that I've missed with the first way of doing it? If not, why is this way not working?
It's because of the OleDbDataAdapter
You're setting the DataAdapter to take the string of sql, and connection of con, which means the cmd.Parameter is not passed in with it.
So your code would be looking for da.SelectCommand.Parameters.Add.
You either need
da.SelectCommand.Parameters.Add("#Code", OleDb.OleDbType.VarChar).Value = cmbCustomer.Text
Or
Dim da As New OleDb.OleDbDataAdapter(cmd)

VB.net | how can i turn an access db record in to a tabcontrol add tabpage?

I have a question, I want to add a TabPage to my TabControl from an access record? I think it's something along these lines but it didn't work:
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath.ToString() & "\data\testing.Accdb;Persist Security Info=False;")
con.Open()
Dim constr As String = "SELECT ProductType, Discription FROM TblProductType"
Dim cmd As OleDbCommand = New OleDbCommand(constr, con)
Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
da.Fill(ds, "TblProductType")
Me.TabControl1.TabPages.Add("Discription")
con.Close()
You have to iterate over the row collection of the table:
Using cmd As New OleDbCommand("SELECT ProductType, Discription FROM TblProductType", con)
Using rdr As OleDbDataReader = cmd.ExecuteReader
While rdr.Read
TabControl1.TabPages.Add(rdr("Discription").ToString)
End While
End Using
End Using
Of course, this only gives you an empty TabPage for every record and in it's current implementation, you don't have any reference to the record anymore. That ProductType, if it's unique, should be used somewhere, too.

Declare a DataSet for DataGridView CellFormatting

Im trying to change a row color in datagridview but the code that used to work for me needs a dataset. This is the code that i use to fill my datagridview
Dim sql As String
sql = "SELECT * FROM [product info]"
Dim adapter As New OleDbDataAdapter(sql, strcon)
Dim dt As New DataTable("[product info]")
adapter.Fill(dt)
DataGridView1.DataSource = dt
Dim sql1 As String
sql1 = "SELECT * FROM [product info]"
Dim adapter1 As New OleDbDataAdapter(sql1, strcon)
Dim cmd As New OleDbCommand(sql1, strcon)
strcon.Open()
Dim myreader As OleDbDataReader = cmd.ExecuteReader
myreader.Read()
strcon.Close()
How can i declare a dataset within this line of codes?
Dim sql As String
sql = "SELECT * FROM [product info]"
Dim adapter As New OleDbDataAdapter(sql, strcon)
Dim ds As New DataSet("[product info]")
adapter.Fill(ds)
DataGridView1.DataSource = ds
Dim sql1 As String
sql1 = "SELECT * FROM [product info]"
Dim adapter1 As New OleDbDataAdapter(sql1, strcon)
Dim cmd As New OleDbCommand(sql1, strcon)
strcon.Open()
Dim myreader As OleDbDataReader = cmd.ExecuteReader
myreader.Read()
strcon.Close()
Dim ds as New Dataset("product_info")
ds.Tables.Add(dt)
Add these row after you filled data to datatable dt

How to call a query from MS Access 2010 (.accdb) within VB.Net 2010?

I have this code that populates a datagridview with data from ms access:
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim Sql As String
Sql = "SELECT * FROM myTable WHERE case_no=?"
Try
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.accdb;Persist Security Info=True;Jet OLEDB:Database Password=dbadmin2010"
con.Open()
Dim cmd As OleDbCommand = New OleDbCommand(Sql, con)
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Case Info")
DataGridView1.DataSource = ds.Tables("Case Info")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Now I had just finished creating a query from design view within MS Access itself, is there a way to call that query and retrieve the results to my datagridview?
Just use the query name and set the command type, for example, in addition to what you already have, you can use the following notes:
Try
con.ConnectionString = enter_connection_string_here
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "NameOfQuery"
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
da.Fill(ds, "Case Info")