Solved

create new records using adodb and sql

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now