Learn how to a build a cloud-first strategyRegister Now

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

VB.Net Adding records to a database using code not a datagrid.

I am new to VB.Net but have done some VB6 Programming and alot of VBA Programming.  I have created a program that acts as a stop watch for different tasks that I partake in.  On the Stop Button Click Event I want the Program to record a couple of different things in an access database.  I have been looking for how to add a new record to a table but have yet to come across anything. So far I have OleDataAdapter1, OleDbConnection1, and an dsActivity1 dataset.  Where do I go from here?  Here is a sample of what I got so far:

Private Sub btnOMStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOMStart.Click
        StartTime = Now()
    End Sub

    Private Sub btnOMStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOMStop.Click
        StopTime = Now()
        lblResultOM.Text = DateDiff(DateInterval.Second, StartTime, StopTime)
       
    End Sub

Thanks
Mike

0
EXTRHMAN
Asked:
EXTRHMAN
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Jeff CertainCommented:
Mike,

The simplest method is to create an INSERT SQL statement.

Dim conn As OleDbConnection = New OleDbConnection(connectionString)
Dim cmd As OleDbCommand
Dim strSQL as String = "INSERT INTO tableName(columnA,columnB,...) VALUES (value1, value2, ...)"
cmd = New OleDbCommand(strSQL, conn)
cmd.ExecuteNonQuery()
cmd.Dispose()

Jeff
0
 
planoczCommented:
Hi,

Here is where I found good examples for getting started in VB.Net

.....http://msdn.microsoft.com/vbasic/downloads/samples/101samples.aspx

Link for ASP programs...

http://msdn.microsoft.com/asp.net/downloads/kits/default.aspx 


0
 
EXTRHMANAuthor Commented:
Jeff,

That looks like it will work.

I am getting an error though  "Type "OleDbConnection" not defined" and "Type "OleDbCommand" not defined'

This might sound stupid but am I missing a reference or something/

Thanks
Mike
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Arthur_WoodCommented:
Mike, If you look at Chaosian's code, it is ALMOST line for line the precise equivalent of what you would have done in VB 6, using ADO to connect to the database:

create a ADODB.Connection object, and assign the connection string
Open the Connection object
create an ADODB.Command object, and assign the SQL to the command object
assign the Connection to the command object
then use the Execute method of the Command object to execute the non-record returning SQL
then close the connection, and set it to nothing.

conceptually, the two sequences are identical, the differences are semantics.

AW
0
 
planoczCommented:
add
Imports System.Data.SqlClient
0
 
Jeff CertainCommented:
You need to add the line "Imports System.Data.OleDb" at the very top of your class/form. This keeps you from having to type System.Data.OleDB.OleDbConnection, etc.

Importing SQLClient won't help in this case... you're using an Access database.

Jeff
0
 
Jeff CertainCommented:
Also... forgot to add at the end of the code
conn.Close
to close the database connection.
0
 
planoczCommented:
Right I forgot his was using access
0
 
EXTRHMANAuthor Commented:
Thanks for the help.  It works just as planned.
0
 
Jeff CertainCommented:
Mike,

My pleasure. Been doing too much of that stuff lately, so I had the code more or less handy...

Jeff
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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