Validating a command according to user level - vb.net

I am trying to use this code to validate the delete operation. It should only execute if the code entered in the input box is for a super admin but it is returning an error "There is no row at position 1". Anyone who can give a better code structure will be greatly appreciated.
Private Sub btnD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click
dbProvider = "Provider=Microsoft.Ace.OLEDB.12.0;"
dbSource = "Data Source = C:\Users\Blessing\Documents\IBCARIP.accdb;Persist Security Info=False"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "select UserID from Users where UserID = 'dlass8504'"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds2, "IBCARIP")
con.Close()
If InputBox("Please input your UserID to complete operation").ToString <> ds2.Tables("IBCARIP").Rows(inc).Item("UserID").ToString Then
MsgBox("You do not posess sufficient previlegies to perfom this operation..!", MsgBoxStyle.Critical)
ElseIf MsgBox("Do you really want to Delete this Record?", MsgBoxStyle.YesNo) = MsgBoxResult.Ok Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("IBCARIP").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
da.Update(ds, "IBCARIP")
navigaterecords()
End If
End Sub

For idea only ..
Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'here's code for retrieving level
btnD.Enabled = iif(Level < 3, True, False) '-- this will disabled your delete button
End Sub
Private Sub btnD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnD.Click
Dim r As MsgBoxResult
r = Msgbox("Do you really want to Delete this Record?", MsgBoxStyle.YesNo)
If r = MsgBoxResult.Yes Then
'erasing here
End If
End Sub

Related

Copy System.Data.Datatable to Access DB Programmatically Vb.net [duplicate]

Can someone please explain why, when I click the "Commit" button, I get the error
Syntax error in INSERT INTO statement.
Here's the code.
Public Class Form3
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\TA_Officers.mdb"
con.Open()
sql = "SELECT * from TA_OFFICER"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "TA_Officers")
con.Close()
MaxRows = ds.Tables("TA_Officers").Rows.Count
inc = -1
End Sub
Private Sub NavigateRecords()
txtFName.Text = ds.Tables("TA_Officers").Rows(inc).Item(1)
txtMInitial.Text = ds.Tables("TA_Officers").Rows(inc).Item(2)
txtLName.Text = ds.Tables("TA_Officers").Rows(inc).Item(3)
txtContact.Text = ds.Tables("TA_Officers").Rows(inc).Item(4)
txtEmail.Text = ds.Tables("TA_Officers").Rows(inc).Item(5)
txtPosition.Text = ds.Tables("TA_Officers").Rows(inc).Item(6)
txtCourse.Text = ds.Tables("TA_Officers").Rows(inc).Item(7)
txtAddress.Text = ds.Tables("TA_Officers").Rows(inc).Item(8)
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
BtnCommit.Enabled = True
BtnAdd.Enabled = False
BtnUpdate.Enabled = False
BtnDel.Enabled = False
txtPosition.Clear()
txtLName.Clear()
txtFName.Clear()
txtMInitial.Clear()
txtAddress.Clear()
txtCourse.Clear()
txtEmail.Clear()
txtContact.Clear()
End Sub
Private Sub RdMember_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdMember.CheckedChanged
Label2.Visible = False
txtPosition.Visible = False
End Sub
Private Sub RdOfficer_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdOfficer.CheckedChanged
Label2.Visible = True
txtPosition.Visible = True
End Sub
Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("TA_Officers").Rows(inc).Item(1) = txtFName.Text
ds.Tables("TA_Officers").Rows(inc).Item(2) = txtMInitial.Text
ds.Tables("TA_Officers").Rows(inc).Item(3) = txtLName.Text
ds.Tables("TA_Officers").Rows(inc).Item(4) = txtContact.Text
ds.Tables("TA_Officers").Rows(inc).Item(5) = txtEmail.Text
ds.Tables("TA_Officers").Rows(inc).Item(6) = txtPosition.Text
ds.Tables("TA_Officers").Rows(inc).Item(7) = txtCourse.Text
ds.Tables("TA_Officers").Rows(inc).Item(8) = txtAddress.Text
da.Update(ds, "TA_Officers")
MsgBox("Data Updated!")
End Sub
Private Sub BtnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDel.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("TA_Officers").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
NavigateRecords()
da.Update(ds, "TA_Officers")
End Sub
Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDel.Enabled = True
inc = 0
NavigateRecords()
End Sub
Private Sub BtnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCommit.Click
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("TA_Officers").NewRow()
dsNewRow.Item("Firstname") = txtFName.Text
dsNewRow.Item("Middleinitial") = txtMInitial.Text
dsNewRow.Item("Lastname") = txtLName.Text
dsNewRow.Item("Mobilenumber") = txtContact.Text
dsNewRow.Item("Emailaddress") = txtEmail.Text
dsNewRow.Item("Position") = TxtPosition.Text
dsNewRow.Item("Course") = txtCourse.Text
dsNewRow.Item("Address") = txtAddress.Text
ds.Tables("TA_Officers").Rows.Add(dsNewRow)
**da.Update(ds, "TA_Officers")**
MsgBox("New Record added to the Database")
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDel.Enabled = True
End If
End Sub
Private Sub BtnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBack.Click
Form2.Show()
Me.Close()
End Sub
Private Sub TA_MEMBERSBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.TA_MEMBERSBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.TA_OfficersDataSet)
End Sub
End Class
Position is a reserved word for JET 4.0 - try to rename the column something else such as prog_position. Access will often let you create the column with a reserved name, but when you go to access it in code JET blocks it and gives you a generic Syntax error in INSERT INTO Statement error without being specific. Check out http://support.microsoft.com/kb/248738 for more information on reserved words. It is goods practice to always prefix your column names with something short so that you never run into conflicts or reserved word issues.
As Makita mentions, your problem stems from the fact that Position is a reserved word in Jet/ACE SQL. The solution is to add the following two lines of code after you create the OleDbCommandBuilder object:
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
That will tell the OleDbCommandBuilder to generate SQL statements like this
INSERT INTO [TA_OFFICER] ([FirstName], ...
...instead of
INSERT INTO TA_OFFICER (FirstName, ...
Wrapping the Position column name in those square brackets will tell the Jet database engine that it is a column name, not a keyword.
This kind of error can happen when you have different data type in the code and in the DB.
Checking the log can give some light on this.

Using a combo box as switch to load 2 different databases

Hello I have a database management program in Visual Studio 2013 that I would like to use a combo box as a switch for loading one database or the other depending on the decision... I managed to do the switch but I am having several problems when editing the databases from VS 2013:
The information entered in those list(Add, Delete)don't update the Access Database, they just update on the datagrid of VS2013...
The DataGrid starts counting at -1 then -2 and so on...
When I hit the Edit button, the program returns me a OleDbException Unhandled...this exception states this : " An INSERT INTO query cannot contain a multi-valued field."
Here are my codes of the edit add and delete buttons respectively, what I am missing?
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If ComboBox1.SelectedIndex = -1 Then
MsgBox("Please specify a category first.", MsgBoxStyle.Information)
ComboBox1.Focus()
End If
Me.Validate()
Me.ContactsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Database_de_EmpleadosDataSet)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'category err
If ComboBox1.SelectedIndex = -1 Then
MsgBox("Please specify a category first.", MsgBoxStyle.Information)
ComboBox1.Focus()
End If
'codes for add a new field
ContactsBindingSource.AddNew()
'ContactsBindingSource1.AddNew()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'code for err category
If ComboBox1.SelectedIndex = -1 Then
MsgBox("Please specify a category first.", MsgBoxStyle.Information)
ComboBox1.Focus()
End If
'code for removing field/row
ContactsBindingSource.RemoveCurrent()
ContactsBindingSource1.RemoveCurrent()
End Sub
And Here is how I made the switch in the combo box:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex = 0 Then
Dim dbprovider As String = "PROVIDER = Microsoft.ACE.OleDb.12.0;"
Dim dbsource As String = "DATA SOURCE = \\Mac\Home\Desktop\Dalangs\Database De Empleados.accdb"
DataGridView1.Show()
DataGridView2.Hide()
con.ConnectionString = dbprovider & dbsource
con.Open()
da.GetFillParameters()
con2.Close()
MsgBox("Employees successfully loaded")
ElseIf ComboBox1.SelectedIndex = 1 Then
Dim dbprovider As String = "PROVIDER = Microsoft.ACE.OleDb.12.0;"
Dim dbsource As String = "DATA SOURCE = \\Mac\Home\Desktop\Dalangs\Database De Clientes.accdb"
DataGridView2.Show()
DataGridView1.Hide()
con2.ConnectionString = dbprovider & dbsource
con2.Open()
da2.GetFillParameters()
con.Close()
MsgBox("Clients successfully loaded")
End If
Button4.Visible = True
End Sub
what I am missing? :S

OleDbCommandBuilder creates SQL statements that result in "syntax error"

Can someone please explain why, when I click the "Commit" button, I get the error
Syntax error in INSERT INTO statement.
Here's the code.
Public Class Form3
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\TA_Officers.mdb"
con.Open()
sql = "SELECT * from TA_OFFICER"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "TA_Officers")
con.Close()
MaxRows = ds.Tables("TA_Officers").Rows.Count
inc = -1
End Sub
Private Sub NavigateRecords()
txtFName.Text = ds.Tables("TA_Officers").Rows(inc).Item(1)
txtMInitial.Text = ds.Tables("TA_Officers").Rows(inc).Item(2)
txtLName.Text = ds.Tables("TA_Officers").Rows(inc).Item(3)
txtContact.Text = ds.Tables("TA_Officers").Rows(inc).Item(4)
txtEmail.Text = ds.Tables("TA_Officers").Rows(inc).Item(5)
txtPosition.Text = ds.Tables("TA_Officers").Rows(inc).Item(6)
txtCourse.Text = ds.Tables("TA_Officers").Rows(inc).Item(7)
txtAddress.Text = ds.Tables("TA_Officers").Rows(inc).Item(8)
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
BtnCommit.Enabled = True
BtnAdd.Enabled = False
BtnUpdate.Enabled = False
BtnDel.Enabled = False
txtPosition.Clear()
txtLName.Clear()
txtFName.Clear()
txtMInitial.Clear()
txtAddress.Clear()
txtCourse.Clear()
txtEmail.Clear()
txtContact.Clear()
End Sub
Private Sub RdMember_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdMember.CheckedChanged
Label2.Visible = False
txtPosition.Visible = False
End Sub
Private Sub RdOfficer_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdOfficer.CheckedChanged
Label2.Visible = True
txtPosition.Visible = True
End Sub
Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("TA_Officers").Rows(inc).Item(1) = txtFName.Text
ds.Tables("TA_Officers").Rows(inc).Item(2) = txtMInitial.Text
ds.Tables("TA_Officers").Rows(inc).Item(3) = txtLName.Text
ds.Tables("TA_Officers").Rows(inc).Item(4) = txtContact.Text
ds.Tables("TA_Officers").Rows(inc).Item(5) = txtEmail.Text
ds.Tables("TA_Officers").Rows(inc).Item(6) = txtPosition.Text
ds.Tables("TA_Officers").Rows(inc).Item(7) = txtCourse.Text
ds.Tables("TA_Officers").Rows(inc).Item(8) = txtAddress.Text
da.Update(ds, "TA_Officers")
MsgBox("Data Updated!")
End Sub
Private Sub BtnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDel.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("TA_Officers").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
NavigateRecords()
da.Update(ds, "TA_Officers")
End Sub
Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDel.Enabled = True
inc = 0
NavigateRecords()
End Sub
Private Sub BtnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCommit.Click
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("TA_Officers").NewRow()
dsNewRow.Item("Firstname") = txtFName.Text
dsNewRow.Item("Middleinitial") = txtMInitial.Text
dsNewRow.Item("Lastname") = txtLName.Text
dsNewRow.Item("Mobilenumber") = txtContact.Text
dsNewRow.Item("Emailaddress") = txtEmail.Text
dsNewRow.Item("Position") = TxtPosition.Text
dsNewRow.Item("Course") = txtCourse.Text
dsNewRow.Item("Address") = txtAddress.Text
ds.Tables("TA_Officers").Rows.Add(dsNewRow)
**da.Update(ds, "TA_Officers")**
MsgBox("New Record added to the Database")
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDel.Enabled = True
End If
End Sub
Private Sub BtnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBack.Click
Form2.Show()
Me.Close()
End Sub
Private Sub TA_MEMBERSBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.TA_MEMBERSBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.TA_OfficersDataSet)
End Sub
End Class
Position is a reserved word for JET 4.0 - try to rename the column something else such as prog_position. Access will often let you create the column with a reserved name, but when you go to access it in code JET blocks it and gives you a generic Syntax error in INSERT INTO Statement error without being specific. Check out http://support.microsoft.com/kb/248738 for more information on reserved words. It is goods practice to always prefix your column names with something short so that you never run into conflicts or reserved word issues.
As Makita mentions, your problem stems from the fact that Position is a reserved word in Jet/ACE SQL. The solution is to add the following two lines of code after you create the OleDbCommandBuilder object:
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
That will tell the OleDbCommandBuilder to generate SQL statements like this
INSERT INTO [TA_OFFICER] ([FirstName], ...
...instead of
INSERT INTO TA_OFFICER (FirstName, ...
Wrapping the Position column name in those square brackets will tell the Jet database engine that it is a column name, not a keyword.
This kind of error can happen when you have different data type in the code and in the DB.
Checking the log can give some light on this.

da.Update() "syntax error" when updating database VB.NET Access 2010

I am a beginner at databases with VB.Net and am trying to update a password (field) in a database and I keep getting an error:
"Syntax error in query expression '((UID = ?) AND ((? = 1 AND Username IS NULL) OR (Username = ?)) AND ((? = 1 AND Firstname} IS NULL) OR (Firstname} = ?)) AND ((? = 1 AND Surname IS NULL) OR (Surname = ?)) AND ((? = 1 AND UPassword IS NULL) OR (UPassword = ?)) AND ((? = 1 AND DOB IS NULL)'."
Here is the code for the form it corresponds to and happens when the reset button is clicked.
Public Class ResetPW
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim sql As String
Dim dbProvider As String
Dim dbSource As String
Dim da As New OleDb.OleDbDataAdapter
Private Sub cbxShowchar_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxShowchar.CheckedChanged
If cbxShowchar.Checked = True Then
txtNewpassword.PasswordChar = ""
txtVNewpassword.PasswordChar = ""
Else
txtNewpassword.PasswordChar = "*"
txtVNewpassword.PasswordChar = "*"
End If
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
Me.Close()
Form2.Show()
End Sub
Private Sub ResetPW_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
dbSource = "DATA Source = C:\Users\Luke\Dropbox\FdSc Computing\Year 2\Project\Product\Book Database.accdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
sql = "SELECT * FROM T_Users"
da = New OleDb.OleDbDataAdapter(Sql, con)
da.Fill(ds, "Users")
MsgBox("The database is now open")
con.Close()
MsgBox("The database is now closed")
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
If txtNewpassword.Text = txtVNewpassword.Text Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("Users").Rows(0).Item(4) = txtNewpassword.Text
da.Update(ds, "Users") 'ERROR HAPPENS HERE'
MsgBox("Password Reset", vbInformation)
Else
MsgBox("The passwords do not match", vbExclamation)
End If
End Sub
End Class
I would be very grateful for any help. Thanks guys!

The ConnectionString property has not been initialized help databases

I have been trying to work this out for the last 4 weeks and cannot find the answer I've just been going around in circles. I have had multiple errors with con As New OleDb.OleDbConnection and connection string and ISAM's
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim da As OleDb.OleDbDataAdapter
Public sql As String
Dim ds As New DataSet
Dim dbPassword As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "C:\Users\Edward\Desktop\Edward\DataBase Login Window\Users.mdb;Jet OLEDB:Database"
con.ConnectionString = dbProvider & dbSource
con.Open()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
sql = "SELECT * FROM users WHERE accUsername='" & TextBox1.Text & "'AND accPass='" & TextBox2.Text & "'"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "users")
If ds.Tables("users").Rows.Count = 1 Then
Dim isAdmin As Boolean = ds.Tables("users").Rows(0).Item(4)
If isAdmin = True Then
MsgBox("Logged in.")
Form4.Show()
Form4.Username = TextBox1.Text
Form4.Password = TextBox2.Text
ElseIf isAdmin = False Then
MsgBox("Logged in user.")
Form6.Show()
Form6.Username = TextBox1.Text
Form6.Password = TextBox2.Text
End If
Else
MsgBox("Incorrect Username or Password")
End If
Me.Hide()
ds.Clear()
Me.Refresh()
End Sub
Private Sub createanaccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles createanaccount.Click
Form2.ShowDialog()
End Sub
End Class
in the button you should set the connectionString here is an example.
dim con as new SQLConnection/OLEDBConnection
con.ConnectionString="Provide your connection string"
con.open
I hope this will definitely solve your problem