Hello programmers.. i need some help for this code in vb language. the code is below (for add).?

Public Class Tambah_Maklumat

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If txtRef.Text = “” Or txtQty.Text = “” Or txtYear.Text = “” Or txtAuthor.Text = “” Or txtPrice.Text Or
txtPub.Text = “” Then MsgBox(“please fill in the blank”, MsgBoxStyle.Information)

Else

If MessageBox.Show(“confrim add?”, “Add books”, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
Dim cmdinfo As New OleDb.OleDbCommand
Dim con As New OleDb.OleDbConnection(“Provider= Microsoft.Jet.OLEHDB.4.0;” & “data source=” & AppDomain.CurrentDomain.BaseDirectory & “Database2.mdb”)

cmdinfo.CommandText = “insert into MECHATRONICS” _
& “(REFNO,QTY,BOOKTITLE,YEAR,AUTHOR,PRICE,… _
& “VALUES(” _
& “‘” & txtRef.Text & “‘,” _
& “‘” & txtQty.Text & “‘,” _
& “‘” & txtBook.Text & “‘,” _
& “‘” & txtYear.Text & “‘,” _
& “‘” & txtAuthor.Text & “‘,” _
& “‘” & txtPrice.Text & “‘,” _
& “‘” & txtPub.Text & “‘)”””

cmdinfo.Connection = con
cmdinfo.Connection.Open()
cmdinfo.ExecuteNonQuery()

con.Close()

MsgBox(“new data has been save!”, MsgBoxStyle.Information)
txtRef.Text = “”
Me.Close()

Else
MsgBox(“information has save”)
txtRef.Text = “”
txtQty.Text = “”
txtBook.Text = “”
txtAuthor.Text = “”
txtPrice.Text = “”
txtPub.Text = “”

End If

End Sub
End Class

✅ Answers

? Best Answer

  • Take a look at this code/ScreenShot, I have modified your code and tested the connection string (you need only put your DB name in) and i have modified the syntax of the SQL INSERT INTO statement…

    Here is a ScreenShot
    http://www.flickr.com/photos/95021141@N0…

    You will need
    7 TextBoxes
    7 Labels
    1 button

    And here is the Code
    http://pastebin.com/iqtLfwr3

    The code is written to run with the names of the controls that you had, SO…. you will need to change the control names (for TextBoxes & Button) if you add them to you form…
    Any questions you can Email me

    NOTE :: when you start the program, the ‘Add Record’ button is disabled at first, it will become enabled once ALL the TextBoxes have some text in them… take a look at the code in TextBox_TextChanged…

    Hope that helps
    – Chosen by Asker

  • Leave a Comment