I am trying to import data from my Access database in a VBA form using a button and a gridview. I tested the code in Access and it worked, but for some reason it doesn't work when I try to implement it in VBA button. I get the error "No value given for one or more required parameters". Can you tell me please what am I doing wrong?
This is the code (I translated it to English, so maybe I forgot to modify something, but I repeat that it worked on Access):
Dim conn As OleDbConnection = New OleDbConnection
Dim provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
Dim dataFile = "C:\Users\solov\Downloads\LibrarieV2.accdb"
conn.ConnectionString = provider & dataFile
conn.Open()
Dim q = "SELECT Customers.gender, Books.book_category, " & _
" SUM(Details_sales.total_cost) AS TotalSumCategory " & _
" FROM ((Customers INNER JOIN Sales ON Customers.customer_id = " & _
" Sales.customer_id )INNER JOIN Details_sales ON Sales.sale_id = " & _
" Details_sales.sale_id) INNER JOIN Books ON Details_sales.isbn = " & _
" Books.isbn GROUP BY Customers.gender, Books.book_category, " & _
" TotalSumCategory ORDER BY Books.book_category"
Dim cmd As OleDbCommand = New OleDbCommand(q, conn)
Dim da As New OleDbDataAdapter(q, conn)
Dim dr As OleDbDataReader = cmd.ExecuteReader
Dim ds = New DataSet
da.Fill(ds, "Categories") ---> this is the chart that I will use to populate the grid
DataGridView1Form2.DataMember = "Categories"
DataGridView1Form2.DataSource = ds
Related
I have prepared my project in vb.net with access database, but I am getting an error like "syntax error in update statement"
I have used following code:
Dim cn As New OleDb.OleDbConnection
Dim cm As New OleDb.OleDbCommand
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\NAV Vikram\DATABASE NAVPREET.mdb"
cn.Open()
cm.Connection = cn
cm.CommandText = "UPDATE DATAENTRY2 set [DIAGNOSIS]='" & TextBox13.Text & "',WHERE[opdno]='" & TextBox1.Text & "' "
cm.ExecuteNonQuery()
Any help would be appreciated.
omit , before WHERE and add space after it. Change:
cm.CommandText = "UPDATE DATAENTRY2 set [DIAGNOSIS]='" & TextBox13.Text & "',WHERE[opdno]='" & TextBox1.Text & "' "
to:
cm.CommandText = "UPDATE DATAENTRY2 set [DIAGNOSIS]='" & TextBox13.Text & "' WHERE [opdno]='" & TextBox1.Text & "' "
Also Use SQL parameters. (Not very keen to vb to show you example)
You have syntax error in your query. Please remove comma (,) you have used before where clause from query, as it is used to separate two different column
Dim cn As New OleDb.OleDbConnection
Dim cm As New OleDb.OleDbCommand
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\NAV Vikram\DATABASE NAVPREET.mdb"
cn.Open()
cm.Connection = cn
cm.CommandText = "UPDATE DATAENTRY2 set [DIAGNOSIS]='" & TextBox13.Text & "' WHERE[opdno]='" & TextBox1.Text & "' "
cm.ExecuteNonQuery()
I am fresher in VB.NET and I am trying to execute an SQL query in VB.NET but not showing any value at output. Can u please help me to find where am I going Wrong.
Dim sConnectionString As String _
= "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"
Dim objConn As New SqlConnection(sConnectionString)
objConn.Open()
Dim sw ,readerObj
Dim sSQL As String = "select top 1 " & sw & " = e.import from tblrelcoms r , [beant].[dbo].tblequipments e where r.IDEquipment = e.IDEquipment"
Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader
Dim objCmd As New SqlCommand(sSQL, objConn)
objCmd.ExecuteNonQuery()
TextBox1.Text = sw.ToString()
The problem you have is that you cannot just concatenate a variable into SQL and expect it to be updated once the SQL is executed.
ExecuteScalar is probably the easiest way of achieving what you want:
Dim sConnectionString As String _
= "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"
Dim sSQL as string = "SELECT TOP 1 e.import " _
"FROM tblrelcoms r " & _
" INNER JOIN [beant].[dbo].tblequipments e " & _
" ON r.IDEquipment = e.IDEquipment " & _
"ORDER BY e.Import;"
Using connection = new SqlConnection(sConnectionString)
Using command = New SqlCommand(sSQL, connection)
connection.Open()
TextBox1.Text = command.ExecuteScalar().ToString()
End Using
End Using
Although if you need more than one column, then you could use a data reader:
Dim sConnectionString As String _
= "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"
Dim sSQL as string = "SELECT TOP 1 e.import " _
"FROM tblrelcoms r " & _
" INNER JOIN [beant].[dbo].tblequipments e " & _
" ON r.IDEquipment = e.IDEquipment " & _
"ORDER BY e.Import;"
Using connection = new SqlConnection(sConnectionString)
Using command = New SqlCommand(sSQL, connection)
connection.Open()
Using reader = command.ExecuteReader()
If reader.Read()
TextBox1.Text = reader.GetString(0)
End If
End Using
End Using
End Using
I have made a couple of other changes too.
Added Using blocks to ensure IDisposable objects are disposed of properly.
Updated the sql join syntax from ANSI 89 implicit joins to ANSI 92 explicit joins, as the name suggests the syntax you are using is 24 years out of date. There are many reasons to start using the new syntax, which is nicely covered in this article: Bad habits to kick : using old-style JOINs
Added an ORDER BY clause to your sql. TOP 1 without order by will give you indeterminate results (unless you only have one record, in which case top 1 is redundant)
A more complex solution would be to use output parameters, which will work and seems more in line with what you were originally trying to achieve, but is overkill for this situation (in my opinion):
Dim sConnectionString As String _
= "User ID=XXX ;Password=XXX;Initial Catalog=gemXXX;Data Source=SCRBAMSDKBXXXXXX"
Dim sSQL as string = "SELECT TOP 1 #Output = e.import " _
"FROM tblrelcoms r " & _
" INNER JOIN [beant].[dbo].tblequipments e " & _
" ON r.IDEquipment = e.IDEquipment " & _
"ORDER BY e.Import;"
Using connection = new SqlConnection(sConnectionString)
Using command = New SqlCommand(sSQL, connection)
connection.Open()
Dim p As SqlParameter = command.Parameters.Add("#Output", SqlDbType.VarChar, 255)
p.Direction = ParameterDirection.InputOutput
command.ExecuteNonQuery();
TextBox1.Text = p.Value.ToString()
End Using
End Using
*Please excuse any syntax errors, I have not used VB.Net in years, and some c# quirks may be in the below, such as I can't remember if you just don't have to use parentheses for a parameterless method, or if you can't... Hopefully there is enough basic structure to get you started
I have a problem on filtering my data on Data Grid View, whenever I search a the same data on my search box the, it continuously add the same data on my datagridview. How can I fix it?
I have two related table.
Here's my code:
Dim sqlqr As String
Dim sqlqry As String
Dim src As New BindingSource()
Dim tbl As DataTableCollection = ndst.Tables
sqlqry = "SELECT * FROM tblEmployeeInfo WHERE EmpIdNo='" & txtEmpid.Text & "'"
sqlqr = "SELECT * FROM tblAttendance WHERE EmpIdNo='" & txtEmpid.Text & "'"
If sqlqr <> sqlqry Then
cnn = New OleDbConnection(cs)
da = New OleDbDataAdapter(sqlqr, cnn)
cnn.Open()
da.Fill(ndst, "tblAttendance")
Dim vw As New DataView(tbl(0))
cnn.Close()
src.DataSource = vw
dgridDtr.DataSource = src
' dgridDtr.DataMember = "tblAttendance"
src.Filter = String.Format("[EmpIdNo] LIKE '%" & txtEmpid.Text & "%'")
dgridDtr.Refresh()
End If
here's the screenshot:
Heres The image Data Grid
I had my code working just fine, but when I generated new updated versions of the CSV files it suddenly errors out on me and gives me a type mismatch catch.
Here is the code that I have right now.
Dim A As String = "ADusers.csv"
Dim B As String = "MlnExp.csv"
Dim filePath As String = "C:\CSV"
Try
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & (filePath) & "\;Extended Properties='text;HDR=Yes'"
Dim sSql As String = "SELECT *" _
& "FROM ([" & (B) & "] B LEFT JOIN [" & (A) & "] A ON B.EmployeeNumber = A.employeeID)"
Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConnectionString)
Dim Command As OleDb.OleDbCommand = New OleDb.OleDbCommand(sSql, conn)
Command.CommandType = CommandType.Text
conn.Open()
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sSql, conn)
Dim dt As DataTable = New DataTable
da.Fill(dt)
DataGrid1.DataSource = dt
DataGrid1.Update()
conn.Close()
lblStatus.Text = "CSV combined... Now saving to file."
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
End Try
Before it would go through, combine the two CSV files, and then display them in my datagrid. But now my catch is throwing back
Type mismatch in expression
i would check B.EmployeeNumber = A.employeeID
in both of your file one is a text value (left align) and the other is a a interger(right align)
Ran into this error message while trying to select some records off a table.
My mind is shot right now..
Any ideas? Thanks!
myreader is,
Public myReader As SqlCeDataReader
Dim currentMatch(1) As String
cmd.CommandText = "SELECT schoolid,opponent " & _
"FROM TEAM_SCHEDULE WHERE seasonid = " & i & _
" AND gameid = " & 1 & " ORDER BY id"
myReader = cmd.ExecuteReader()
Do While myReader.Read()
currentMatch(0) = myReader.GetString(0)
currentMatch(1) = myReader.GetString(1)
Loop
Educated guess:
schoolid is defined as INT, but for some reason you are using GetString instead of GetInt32 to fetch it.
Do While myReader.Read()
currentMatch(0) = myReader.GetInt32(0).ToString()
currentMatch(1) = myReader.GetString(1)
Loop
why don't use datatable?
Public myReader As SqlCeDataReader
Dim currentMatch(1) As String
cmd.CommandText = "SELECT schoolid,opponent " & _
"FROM TEAM_SCHEDULE WHERE seasonid = " & i & _
" AND gameid = " & 1 & " ORDER BY id"
Dim dt as new DataTable
dt.load(cmd.ExecuteReader())
If you want, you can query result datatable with linq
dim QueryResults = from p as datarow in dt.rows where p.seasonid<10 select p
or select a list
dim MyObjList = (from p as datarow in dt.rows select seasonid = p.seasonid, gameid = p.gameid).ToList