Solved

create new records using adodb and sql

Posted on 2004-10-16
2
162 Views
Last Modified: 2010-05-02
Another newbie question.
I am using ADODB.Connection and ADODB.Recordset withMicrosoft SQL
Lets say I have two tables, one company and one employee and they are related.
What call do I make to ADODB to create a new record in the two tables. How does the primary key get placed into the foreign key of the child table?


Dim comp as string
Dim emp as string
Comp = “Albertsons”
Emp = “shayne”

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
 Set cn = New ADODB.Connection
 set rs = New ADODB.Recordset
cn.Open '"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=test;Data Source=DURGA"

cn.Open


now I do not know what to do

Thanks in advance
shayne
0
Comment
Question by:shayne23d
2 Comments
 
LVL 12

Accepted Solution

by:
pique_tech earned 125 total points
ID: 12330518
You should be able to use ADO to add the record to the "main" table, get its PK value, then immediately add the record to the "child" table using the PK from the "main" record.  Broadly,

Dim rs1 As ADODB.Recordset: Set rs1 = New ADODB.Recordset
Dim rs2 As ADODB.Recordset: Set rs1 = New ADODB.Recordset
Dim GetKey As Long
rs1.Open "MainTableName", cn, adOpenDynamic, adLockOptimistic, adCmdTable
rs2.Open "MainChildName", cn, adOpenDynamic, adLockOptimistic, adCmdTable

rs1.AddNew
    rs1("Field1Name") = "blah"
    ...
rs1.Update
GetKey = rs1("PrimaryKeyFieldName")

rs2.AddNew
    rs2("ForeignKey") = GetKey
    ...
rs2.Update
...
'Cleanup
rs1.Close
rs2.Close
Set rs1 = Nothing
Set rs2 = Nothing

This should work because after calling the recordset's Update method, the new record becomes the current record (docmumented here:  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmthaddnew.asp)


0
 

Author Comment

by:shayne23d
ID: 12330590
yeah! I was going to use a function to return the primary key of the record in sql, this is what I was looking for.
Thank you
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
String manipulation in Visual Basic 7 71
MS Access - Capture pressed key onclick 4 41
Passing a Text Box name to a Sub 6 99
Problem to With line 4 63
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question