-1

Anyone can help me with this please ?

The app you see rn is nothing like the app im working on , i made it way simple.

** because of the versions ?? I'm 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 too many radio buttons( equipments status to check if they on or fail ) and more than 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 , and i dont want create the gridviewcolumns myself because i got over 50 columns that would be long , i just need to use simple code as possible .

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 ]

pic1(click me): example of error i keep getting

pic2 : the simple form1 with it code below .

pic3 : my Ms-AccessDb

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 

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
iifast2
  • 303
  • 1
  • 3
  • 9
  • Can you include the exact error you're getting? You should be using parameters to update databases. Also, read up on the `Using` statement to effectively close your disposable objects. – Andrew Mortimer Aug 13 '18 at 06:35
  • Did you have the Access design window open when you try to use the database from code? – Steve Aug 13 '18 at 06:47

1 Answers1

0

You can't have spaces in field names, and a date value must be formatted properly. Also, Status may be a reserved word, and - if s1 and s2 are numeric - no quotes:

Label_currentdate.Text = DateTime.Today.ToString("yyyy'/'MM'/'dd")

<snip>

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 & ")"

But do consider using parameters. Much cleaner and simpler to maintain.

Gustav
  • 53,498
  • 7
  • 29
  • 55