Solved

Insert into statement using ado in vb6

Posted on 2006-07-13
13
18,144 Views
Last Modified: 2013-12-25
Hello all!

When I try to insert values into sql in vb6 using ADO I receive an error of "Obeject variable or with block variable not set"

Here is my code :
Private Sub TestConnect_Click()
    On Error GoTo bdconnection:
    Dim conn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim sql As String
   
    conn.Open "Driver={SQL Server};Server=10.20.XX.XXX;Database=ENFN_Test;Uid=XX;Pwd=XX;"
   
    sql = "Insert into Serialhistdetail VALUES('123456', '0', 'This is a Test', '100', '1')"
   
    Set rst = Server.CreateObject("ADODB.Recordset")
    rst.Open sql, conn
    rst.MoveFirst
    response.Write rst!serialhistdetail
   
    conn.Close
    Set conn = Nothing
   
bdconnection:
    MsgBox ("No connection could be established")
End Sub

What am I doing wrong ?
0
Comment
Question by:abennett10
13 Comments
 
LVL 11

Expert Comment

by:leclairm
ID: 17099743
Dim conn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim sql As String

set conn = new adodb.connection  '<<<<<  Your missing this
   
    conn.Open "Driver={SQL Server};Server=10.20.XX.XXX;Database=ENFN_Test;Uid=XX;Pwd=XX;"
   
    sql = "Insert into Serialhistdetail VALUES('123456', '0', 'This is a Test', '100', '1')"
....
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17099831
Private Sub TestConnect_Click()
    On Error GoTo bdconnection:
    Dim conn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim sql As String
   
    set conn = new adodb.connection
    conn.Open "Driver={SQL Server};Server=10.20.XX.XXX;Database=ENFN_Test;Uid=XX;Pwd=XX;"
   
    sql = "set nocount on Insert into Serialhistdetail VALUES('123456', '0', 'This is a Test', '100', '1')  select scope_identity() as serialhistdetail"
   
    Set rst = Server.CreateObject("ADODB.Recordset")
    rst.Open sql, conn
    rst.MoveFirst  
    response.Write rst!serialhistdetail
   
    conn.Close
    Set conn = Nothing
   
bdconnection:
    MsgBox ("No connection could be established")
End Sub
0
 

Author Comment

by:abennett10
ID: 17099835
That worked but now I'm getting an "object required" error on this line :

Set rst = Server.CreqateObject("ADODB.Recordset")

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:leclairm
ID: 17099856
Set rst = CreateObject("ADODB.Recordset")
0
 
LVL 11

Expert Comment

by:leclairm
ID: 17099870
Did you copy this from an asp page into vb??
0
 

Author Comment

by:abennett10
ID: 17099872
I made a typo I still get that error message.
0
 

Author Comment

by:abennett10
ID: 17099891
No actually I found it at a website. What I'm trying to accomplish is simple but I'm having a hard time. I want to insert those values into an sql database using ado and vb6.

Would have a code example on how to accomplish that ?
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 17099934
(Wild guess)

Set Conn = New ADODB.Connection   'After the Dim, before the Conn.Open
Set rst = New ADODB.Recordset 'Replaces your Server.CreateObject line

0
 
LVL 11

Expert Comment

by:leclairm
ID: 17099963
Try:

 Dim conn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim sql As String
   
    set conn = new adodb.connection
    conn.Open "Driver={SQL Server};Server=10.20.XX.XXX;Database=ENFN_Test;Uid=XX;Pwd=XX;"
   
    sql = "Insert into Serialhistdetail VALUES('123456', '0', 'This is a Test', '100', '1')"
    conn.execute sql
    Set rst = CreateObject("ADODB.Recordset") ' or set rst = new adodb.recordset (early binding)
    sql= "select scope_identity() as serialhistdetail""
    rst.Open sql, conn
    rst.MoveFirst  
    response.Write rst!serialhistdetail
   
    conn.Close
    Set conn = Nothing
0
 
LVL 11

Accepted Solution

by:
leclairm earned 500 total points
ID: 17099979
Actually:

Dim conn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim sql As String
   
    set conn = new adodb.connection
    conn.Open "Driver={SQL Server};Server=10.20.XX.XXX;Database=ENFN_Test;Uid=XX;Pwd=XX;"
   
    sql = "Insert into Serialhistdetail VALUES('123456', '0', 'This is a Test', '100', '1')"
    conn.execute sql
    Set rst = CreateObject("ADODB.Recordset") ' or set rst = new adodb.recordset (early binding)
    sql= "select scope_identity() as serialhistdetail""
    rst.Open sql, conn
    rst.MoveFirst  
    debug.print rst!serialhistdetail  'Can't use response.write in vb
   
    conn.Close
    Set conn = Nothing
0
 

Author Comment

by:abennett10
ID: 17100035
Thank you leclairm! That worked! you get the points.

Thank you also jimhorn.

I still have an error message but I think it's my database :

"Cannot use empty objects or column names, use a single space if necessary"

Have you ever seen this?
0
 
LVL 11

Expert Comment

by:leclairm
ID: 17100063
do your values in your insert statement match the number of columns in your table SerialHistDetail???
0
 

Author Comment

by:abennett10
ID: 17100073
Actually they do......I did some error handling and did a resume next on error.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

828 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