[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

ACCESS

How can I accomplish this with Access database? The name of the database is Testonly.mdb The name of the fields are Address, city, state and zip.


 Imports System.Data.SqlClient
Public Class Form2 Inherits System.Windows.Forms.Form
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim ra as Integer
'integer holds the number of records inserted
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles Button1.Click
myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs")
'you need to provide password for sql server
myConnection.Open()
myCommand = New SqlCommand("Insert into Jobs values 12,'IT Manager',100,300,_
myConnection)
ra=myCommand.ExecuteNonQuery()
MessageBox.Show("New Row Inserted" & ra)
myConnection.Close()
End Sub
End Class
0
VBdotnet2005
Asked:
VBdotnet2005
  • 2
2 Solutions
 
Arthur_WoodCommented:
in Access VBA, this would be:

Dim myConnection As ADODB.Connection
Dim ra as Integer
'integer holds the number of records inserted

Private Sub Button1_Click()

myConnection = CurrentProject.Connection

myConection.Execute("Insert into Jobs values 12,'IT Manager',100,300)

MsgBox "New Row Inserted", vbOkOnly
myConnection = Nothing
End Sub


AW
0
 
Arthur_WoodCommented:
That was IN ACCESS, itself.

If you are going to do this from a VB.NET application connecting to a Acceess Back-end Database:

Imports System.Data.OleDb
Public Class Form2 Inherits System.Windows.Forms.Form
Dim myConnection As OleDbConnection
Dim myCommand As OleDbCommand
Dim ra as Integer
'integer holds the number of records inserted
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e_
As System.EventArgs) Handles Button1.Click
myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\testonly.mdb;User Id=admin;Password=;" )

myConnection.Open()
myCommand = New OleDbCommand("Insert into Jobs values 12,'IT Manager',100,300,_
myConnection)
ra=myCommand.ExecuteNonQuery()
MessageBox.Show("New Row Inserted" & ra)
myConnection.Close()
End Sub
End Class

Notice the mimimal nature of the changes (the OleDb objects, and the Connection string)

AW
0
 
VBdotnet2005Author Commented:
Since, Testonly.mdb is a database file, do I still need to provide user name and password?
0
 
KudzullcCommented:
<<"Since, Testonly.mdb is a database file, do I still need to provide user name and password? >>

Only if you have manually set one up inside Access.  If you have only a password, you can leave off the "User Id=;".

Lucas
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now