[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Insert into statement using ado in vb6

Posted on 2006-07-13
13
Medium Priority
?
18,377 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
Independent Software Vendors: 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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 …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

649 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