3 Attachment(s)
Vb.net _ Ms-AccessDb /insert into Query errors / exception : 'incorrect file name'.
can someone please help me with this ? :sick:
im using VB.net2017 / Ms-AccessDb 2010
this code below is a simple version that i made , of my main app that im working on , because my main code has 48 radio buttons( 24 equipments status to check if they on or fail ) and 15 textbox , and i did put them in same Db table as columns that the insert query looks so long in vb , cuz i didnt know any other method :( ,
the thing is , i keep getting the errors in these lines below :
connection.open() [error said cant find the file , but my file path is not wrong]
mycommand.CommandText = "INSERT INTO Status (.... ect )
mycommand.ExecuteNonQuery() [ error : exception .... ]
da.Fill(ds, "[Status]") [also error in the select query coudnt find the table or incorrect file name ]
Attachment 160937
Attachment 160939
Attachment 160941
HERE's The Form1.vb Code :
Code:
Imports System.Data.OleDb
Public Class Form1
Dim s1 As Boolean
Dim s2 As Boolean
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label_currentdate.Text = DateTime.Now.ToString()
End Sub
Private Sub BTN_Save_Click(sender As Object, e As EventArgs) Handles BTN_Save.Click
Dim myconnexion As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data source=C:\Users\acer\Desktop\ABC.accdb")
Dim mycommand As OleDbCommand = myconnexion.CreateCommand()
myconnexion.Open()
mycommand.CommandText = "INSERT INTO Status (item-Number,CurrentDate,Employee_Name,Equip2 status,Equip1 status)
VALUES('" & Label_items.Text & "','" & Label_currentdate.Text & "','" & TextBox_EmployeeName.Text & "','" & s1 & "','" & s2 & "')"
mycommand.ExecuteNonQuery()
MsgBox(" successfully saved !")
myconnexion.Close()
End Sub
Private Sub RB1_CheckedChanged(sender As Object, e As EventArgs) Handles RB1.CheckedChanged
s1 = True
End Sub
Private Sub RB2_CheckedChanged(sender As Object, e As EventArgs) Handles RB2.CheckedChanged
s1 = False
End Sub
Private Sub RB3_CheckedChanged(sender As Object, e As EventArgs) Handles RB3.CheckedChanged
s2 = True
End Sub
Private Sub RB4_CheckedChanged(sender As Object, e As EventArgs) Handles RB4.CheckedChanged
s2 = False
End Sub
Private Sub BTN_reset_Click(sender As Object, e As EventArgs) Handles BTN_reset.Click
Dim RB As RadioButton
TextBox_EmployeeName.Text = ""
For Each RB In GBX1.Controls
RB.Checked = False
Next
For Each RB In GBX2.Controls
RB.Checked = False
Next
End Sub
Private Sub Label_currentdate_Click(sender As Object, e As EventArgs)
End Sub
Private Sub BTN_History_Click(sender As Object, e As EventArgs) Handles BTN_History.Click
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim table As DataTableCollection
Dim source1 As New BindingSource
'
'
conn = New OleDbConnection With {.ConnectionString = "provider=Microsoft.ACE.OLEDB.12.0;Data source=C:\Users\acer\Desktop\ABC.accdb"}
ds = New DataSet
table = ds.Tables
da = New OleDbDataAdapter("Select * from [Status]", conn)
da.Fill(ds, "[Status]")
Dim view As New DataView(table(0))
source1.DataSource = view
DataGridView.DataSource = view
End Sub [/COLOR]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label_items.Text = "item number 1 "
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label_items.Text = "item number 2"
End Sub
End Class
Re: Vb.net _ Ms-AccessDb /insert into Query errors / exception : 'incorrect file name
Welcome to VBForums :wave:
For the file name issue, check that the database file is actually stored in C:\Users\acer\Desktop\ , and that the name is exactly ABC.accdb
The exceptions from ExecuteNonQuery are about various issues with the CommandText, which include:
- You have field names with characters other than numbers and letters (and underscores "_"). Using other characters (like "-" and " ") in field/table names causes problems, so if you have any choice at all, change the names (in the database and your code).
- The table name Status might also be a problem, as it is likely to be a Reserved word. For more information (including lists of Reserved words), see the article What names should I NOT use for tables/fields/views/stored procedures/...? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)
I suspect it is a problem, because other parts of your code put square brackets around it (eg: "Select * from [Status]"). Doing the same here would be a good idea.
- You are putting values into the CommandText, rather than using Parameters... that makes it easy to make mistakes, and also risks extra problems depending on what the data contains.
For an explanation of why you should be using parameters (and links to code examples), see the article Why should I use Parameters instead of putting values into my SQL string? from our Database Development FAQs/Tutorials (at the top of the Database Development forum).