Solved

Insert into statement using ado in vb6

Posted on 2006-07-13
13
18,004 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 142

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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

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 …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now