Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple Insert statement

Status
Not open for further replies.

sfunk

Technical User
Jan 22, 2002
107
US
Hello,

I am writing a small app in VB6sp5. I am using an Access2000 database. I am using the Microsoft ADO Data Control (SP4)(OLEDB). This Sub will reside in a Module and be called from a Form_Load Sub so I assume this all needs to be done programatically. This is where my lack of skill comes in.

I need help writing a simple Sub that when called passes a Long variable and performs an insert and then closes. This happens behind the interface. The user doesn't need to know it happened.

The DSN name is DSNCount.
The Table is tblCount.
The Field is Count.

This should be a self contained Sub (or function) db set up, execution, and teardown all need to happen in the Sub.

Thank you for everyone's help.

Sincerely,
Steve
 
Hi,

Try the following sub in a module:

Public Sub insertValue(ByVal passedLongValue as long)

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
cn.ConnectionString = ";DSN=DSNCount"
cn.Open
rs.ActiveConnection = cn
cn.BeginTrans
rs.LockType = adLockOptimistic
rs.CursorType = adOpenKeyset
rs.Open "tblCount"
rs.AddNew
rs.Fields("Count") = passedLongValue
rs.Update
cn.CommitTrans
rs.Close
cn.Close

End Sub

Set a reference to the Microsoft ActiveX Objects Library 2.0 in the references panel.

Call the procedure from the form load event with the value that you want to enter in the table.

Post if you want anything else.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top