Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

create new records using adodb and sql

Posted on 2004-10-16
2
Medium Priority
?
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
pique_tech earned 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

609 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