Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
|
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 14th, 2007, 03:30 PM
Authorized User
 
Join Date: Oct 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default A standard way to add records to a database?

This particular app is being done in VB6.
I am confronted with a programming problem.
I would like to capture data in my textboxes.
At this point I would like to feed this data to a recordset object. However when I run the app I get the following msg:

"Object variable or With variable not set."

I understand I could have set up a 'With' statement but why am I being compelled to ?
Also is my logic sound here or am I missing something?



Thank-you so much for any assistance.
-----------------CODE BELOW----------------------
Public rstAlianza As ADODB.Recordset
Private Sub Form_Load()

Dim cnn1 As ADODB.Connection
Dim connStr As String
'open ADO connection / DSN is data source name
connStr = "DSN=Alianza;UID = sa;PWD=pwd;"
Set cnn1 = New ADODB.Connection


'set rs=command.execu
End Sub

Private Sub Command1_Click()
Dim cnn1 As ADODB.Connection
Dim connStr As String
Dim sSql As String
Dim lRecordsAffected As Long
Dim I As Integer
Dim txtFields(5)
cnn1.Open connStr
rstAlianza.Fields(0) = Text1
rstAlianza.Fields(1) = Text2
rstAlianza.Fields(2) = Text3
rstAlianza.Fields(3) = Text4
rstAlianza.Fields(4) = Text5
rstAlianza.Fields(5) = Text6
For I = 0 To 5
txtFields(I) = rstAlianza.Fields(I)
Next
'Construct SQL statement
sSql = "INSERT INTO Authors(au_id,au_lname," _
& "au_fname,phone,address,city,state,zip,contrac t)" _
& "VALUES ('" _
& txtFields(0) & "','" _
& txtFields(1) & "','" _
& txtFields(2) & "','" _
& txtFields(3) & "','" _
& txtFields(4) & "','" _
& txtFields(5) & "')'"
cnn1.Execute sSql, lRecordsAffected
End Sub

 
Old April 20th, 2007, 05:11 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

On which line is your error occuring?

However, you declare the cnn1 object, but you never instantiate it. So the first time you try to use it, you would get the error you indicate.

You need to add this line before you attempt to use the cnn1 object:
Set cnn1 = new ADODB.Connection

You might think you are doing this in your Form_Load event, but this isn't correct. You declare an object with the same name there, and instantiate it, but then the Form_Load event ends without you doing anything with it. This is a scope problem. You have declared two separate cnn1 objects - one in Form_Load, and one in Command1_Click. They are not the same object - the are two objects of the same type.

You are also going to have problems with your rstAlianza object - you declare it outside the scope of any method, but you never instantiate it.

There are numerous other problems with your code. I would advise that you get a beginners book on doing data access with VB6 and go through all the exercises. You'll learn a lot.

Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems





Similar Threads
Thread Thread Starter Forum Replies Last Post
Add/Update/Delete Database records MANUALLY jn148 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 December 14th, 2008 10:18 AM
Add records with VBA rohit_ghosh Access VBA 2 June 22nd, 2007 04:13 AM
Need help with a query to add records chacquard Access VBA 1 February 28th, 2005 04:23 PM
Add records from a Datagrid dkomo VB.NET 0 May 6th, 2004 01:08 PM
How to add records hdvan SQL Server ASP 2 September 24th, 2003 12:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.