How to Add/Update/Delete Record using MS Access Database

Submitted by: 
Visitors have accessed this post 365777 times.

The primary purpose of this code is to teach beginner programmer to familiarize the concept of database programming.

This is particularly for beginner but may also applicable for intermediate programmer.


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

hi,
How to insert a data into two table by using acces database with vb.net

Thanx,
Regards,
Sandeep

Kindly study Database Programming Made Easy.

I already discuss everything in there.

Hi,
Follwing is the coding of save and retrive the image from ms access database in vb.net2008.but its through input box.I just want that user click on a account no.from combobox and image is displayed.

  1. Imports System.Data.OleDb
  2. Imports System.IO
  3.  
  4. Public Class Form7
  5. Public ImageSlno As Integer
  6. Public con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =C:\Documents and Settings\Pankaj Arora\My Documents\database.mdb") ' connection srting
  7.  
  8. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  9. Call Insert_Image()
  10. End Sub
  11.  
  12. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  13. Try
  14. OpenFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg"
  15. OpenFileDialog1.ShowDialog()
  16. PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
  17. Catch
  18.  
  19. End Try
  20. End Sub
  21. Public Sub Insert_Image()
  22. Try
  23. Dim st As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
  24. Dim mbr As BinaryReader = New BinaryReader(st)
  25. Dim buffer(st.Length) As Byte
  26. mbr.Read(buffer, 0, CInt(st.Length))
  27. st.Close()
  28. con.Open()
  29. Dim Str As String = "insert into imageTable(photo) values(?)"
  30. Dim Cmd As New System.Data.OleDb.OleDbCommand(Str, con)
  31. Cmd.Parameters.Add("@photo", System.Data.OleDb.OleDbType.Binary, buffer.Length).Value = buffer
  32. Cmd.ExecuteNonQuery()
  33. con.Close()
  34. MsgBox("Image Saved Successfully")
  35. Catch ex As Exception
  36. con.Close()
  37. MsgBox("Error In Insertphoto", MsgBoxStyle.Critical, "Error")
  38. MsgBox(ex.ToString)
  39. End Try
  40. End Sub
  41.  
  42. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  43. Try
  44. ImageSlno = InputBox("Insert Slno ", , "0")
  45.  
  46. Call LoadPhoto(ImageSlno)
  47. Catch ex As Exception
  48. MsgBox(ex.ToString)
  49. End Try
  50. End Sub
  51. Public Sub LoadPhoto(ByVal slno As Decimal)
  52. ' display image from the database
  53. Try
  54. Dim str As String = "SELECT photo FROM ImageTable WHERE slno = " & slno
  55. con.Open()
  56. Dim cmd As New OleDb.OleDbCommand(str, con)
  57. Dim b() As Byte
  58. b = cmd.ExecuteScalar()
  59. con.Close()
  60. If (b.Length > 0) Then
  61. Dim stream As New MemoryStream(b, True)
  62. stream.Write(b, 0, b.Length)
  63. DrawToScale(New Bitmap(stream))
  64. stream.Close()
  65. End If
  66. Catch ex As Exception
  67. 'MsgBox(ex.ToString)
  68. End Try
  69. End Sub
  70. 'Function to Create Instance For the Image From the Buffer
  71. Private Sub DrawToScale(ByVal bmp As Image)
  72. PictureBox1.Image = New Bitmap(bmp)
  73. End Sub
  74.  
  75. Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  76.  
  77. End Sub
  78. End Class

Plz anybody help me.its urgent.

Thanx,
Regards,
Sandeep

Use the following code.

  1. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  2. Try
  3. ImageSlno = ComboBox1.Text
  4.  
  5. Call LoadPhoto(ImageSlno)
  6. Catch ex As Exception
  7. MsgBox(ex.ToString)
  8. End Try
  9. End Sub

But I'm still wondering why you ask this. Because it is very easy. With your code above it seems that you know this already.

Hello, Could you tell me how to be strong in vb.net.I started in vb.net from beginner,but I don't where should i begin.
Anyway Could you tell me which book should i take a look for the first time? or any website ?

Thank

If you want to become a professional vb.net programmer I suggest you take a look at the Books section above.

You may also wish to study "Database Programming Made Easy".

Anyway,would you like tell me where should i pick up that book"Database Programming Make Easy"? I really interesting with your programming code.On the other hand How long did you study with it? where could i finding that course?

Thank you for helping me!

I suggest you take a loot at this book Beginning VB 2008 Databases: From Novice to Professional.

Since I'm not new to VB I leart VB.NET almost a month after studying its concept.

hi,
Thanx for the replied.I want to ask that how the data is view through the list view.I mean like customer id shown in list view and when user click on the id,it shows the full detail of that customer in different fields that fields like cus id,name ,add.i saw ur list view project.but in this fields are also shown from database.i just want to show only data from database and not the fields name.thats field name,i put throgh the components like textboxes.labels and button.will u plz tell how its done,when user clicked on cust id in list view and records would be shown of that customer.

Thanx,
Regards,
Sandeep

There's a lot of sample of this in almost of my code. Download it and study how it works.

hi,
your source code is very userful as im making a project in vb.net2008 with access database.its really very appriciated.will u plz upload the coding of granting the permisiion of different user for accessing the software or data or it send on my id '[email protected]'

Thanx,
Regards,
Sandeep

Please use the code in hotel reservation system. It has a user permission.

Hi! It's been a while since I coded something and that was VB6 then. Do you have time to share some codes/programs using VB2008 as I am using it now. I just need to refresh my knowledge. thank you. My email: yan(at)yanspeaks(dot)com
Thank you kabayan!

Hi kababayan,

Yes you can download the hotel reservation system, enrollment system and you can also study some of my tutorials about .net.

Thank for this code it help us, , ,

and i was edited this program, , to improve it.

Thanks you very much, , ,

JHERVY

thankyou for the code

I have been coding for several years and now using vb.net 2008. When I try to run your code I get an error on Object reference not set to an instance of an object,

newRow = dt.NewRow() Line 98 I set dt = new datatable because 99% of the time when I see this error it is because the the instance was not created as a new instance. Any Ideas? What version did you code this in ? Thanks Dwain

I'm using vb.net 2008, too. I tested it several times without error.

do you have simple code in finding record in the database?

That's pretty simple. Just add a textbox and modify the SQL string. If you still don't know how to do this then reply to this msg and I will create one for you if I have time.

can you give us any idea how to use query using vb.net and access?? thank you sir..

please post the coding.....for finding a record using datagrid or textbox....

Browse Visual Basic .NET section. I have already done this.

Hi sir, i just view the video about how to add,edit,save,delete,and search..

can you teach me sir what are the codes for that buttons
because i really want to know sir on what are the codes for those buttons sir for my studies.

I'm just a second year college student willing to know about visual basic studio 2005.

Pls. answer me sir or pls help me sir..

Thank you

Sir, i will be waiting for your respond.. just send it to me sir in my e-mail
[email protected]

hi gud pm., can i ask? how can i run the program that ive download already?? i need ur reply now., thank you..

Don't be too rush when asking question. You need to be polite sometimes to be answered.

You need vb.net to run this source code.

can u make same program in vb6?

Search for multi column combobox above. The concept is the same with this program.

If TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "" And TextBox4.Text <> "" And TextBox5.Text <> "" Then
con.Open()
Dim up As String = "Update guadalupe set [email protected],[email protected],[email protected],[email protected],[email protected]"
Dim cmd As New OleDbCommand(up, con)
cmd.Parameters.Add(New OleDbParameter("@DR", OleDbType.VarChar, 15)).Value = TextBox1.Text
'cmd.Parameters.Add(New OleDbParameter("@DR", OleDbType.VarChar, 15)).Value = TextBox1.Text
cmd.Parameters.Add(New OleDbParameter("@address", OleDbType.VarChar, 15)).Value = TextBox2.Text
cmd.Parameters.Add(New OleDbParameter("@description", OleDbType.VarChar, 15)).Value = TextBox3.Text
cmd.Parameters.Add(New OleDbParameter("@price", OleDbType.VarChar, 15)).Value = TextBox4.Text
cmd.Parameters.Add(New OleDbParameter("@petsa", OleDbType.VarChar, 15)).Value = TextBox5.Text

cmd.ExecuteNonQuery()
con.Close()
MsgBox("Successfully updated")
Else
MsgBox("Please complete the form")
End If

MY CODE IN UPDATING IT WORKS BUT THE PROBLEM IS ALL ROWS WERE UPDATED AND CHANGES EVERYTHING.. PLEASE I NEED SOME ADVICE

i read ur code ...and there is a problem that
there is needed a 'where ' clause to update a specific row , that's why ur code updating all rows.
ex: Update table1 set name='xyz' where id='123'

Pages

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.