Solved

create new records using adodb and sql

Posted on 2004-10-16
2
164 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 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

Industry Leaders: 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!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

729 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