Solved

Insert into statement using ado in vb6

Posted on 2006-07-13
13
18,248 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
[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
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
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!

 
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 66

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 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…

728 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