How to Make VB.NET Console Application with MS Access Databse 2007

This is a VB.Net Console Application program using Access Database 2007. It is very helpful in learning VB.Net with Database Programming. Instead of using Windows Forms, this program is using console application to insert record into MS Access database. Please follow the steps below on how to create a console application from scratch. 1. Create the database file called "database1.mdb" by opening the MS Word file called "How to Create DB in 2007.doc" and follow the instruction. 2. Open Visual Studio Editor. 3. Click File >> New Project. 4. Select Visual Basic >> Console Application. 5. Click OK. 6. Paste the following code in module1.vb
  1. Imports System.Data.OleDb
  2.  
  3. Module Module1
  4.  
  5.     Sub Main()
  6.         Dim MyInput As String
  7.         Call MainMenu()
  8.  
  9.         'Main Menu
  10.         Console.WriteLine("Press r or R to Return in Main:")
  11.         Console.WriteLine("Press x or X to Exit Program:")
  12.         MyInput = Console.ReadLine
  13.         If MyInput = "r" Then
  14.             Console.Clear()
  15.             Call MainMenu()
  16.         ElseIf MyInput = "x" Then
  17.             End
  18.         End If
  19.         Console.Read()
  20.     End Sub
  21.  
  22.     Sub MainMenu()
  23.         'We Declare a String
  24.         Dim MyISBN As String
  25.         Dim MyAuthor As String
  26.         Dim MyTitle As String
  27.         Dim Category As String
  28.         Dim DatePublished As String
  29.  
  30.         'We Print an Output
  31.         Console.Write("Please Enter ISBN: ")
  32.         MyISBN = Console.ReadLine
  33.         Console.Clear()
  34.         Console.Write("Please Enter Author Name: ")
  35.         MyAuthor = Console.ReadLine
  36.         Console.Clear()
  37.         Console.Write("Please Enter Book Title: ")
  38.         MyTitle = Console.ReadLine
  39.         Console.Clear()
  40.         Console.Write("Please Enter Book Category: ")
  41.         Category = Console.ReadLine
  42.         Console.Clear()
  43.         Console.Write("Please Enter Book Dat Published: ")
  44.         DatePublished = Console.ReadLine
  45.         Console.Clear()
  46.  
  47.         'We Create a 2 Space
  48.         Console.WriteLine()
  49.  
  50.         Dim MyConnection As New OleDbConnection
  51.         Dim MyCommand As New OleDbCommand
  52.         Dim MySQL As String
  53.         MySQL = "Insert Into books(isbn,author,title,category,datepublished) Values('" & MyISBN.ToString & "','" & MyAuthor.ToString & "','" & MyTitle.ToString & "','" & Category.ToString & "','" & DatePublished.ToString & "') "
  54.         MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database1.mdb")
  55.         MyCommand = New OleDbCommand(MySQL, MyConnection)
  56.         MyConnection.Open()
  57.         MyCommand.ExecuteNonQuery()
  58.         MyConnection.Close()
  59.         Console.WriteLine("Data save in the Database!!!")
  60.         Console.WriteLine()
  61.     End Sub
  62.  
  63. End Module

Embed database1.mdb in the project.

1. Right click ConsoleApplication1 >> Click Add >> Click Existing Item. 2. Browse the database1.mdb >> Click Add. 3. Click Cancel if the Data Source Configuration Wizard appear on your screen. I hope it is very helpful. Teody Banagan

Add new comment