?
Solved

Insert into statement using ado in vb6

Posted on 2006-07-13
13
Medium Priority
?
18,311 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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 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…
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…
Suggested Courses

770 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