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 15th, 2007, 05:46 PM
Authorized User
 
Join Date: Oct 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Is a control array useful for INSERT into a table?

I'm trying to capture data in my table. I'm not sure if I am going about capturing this data properly.I was hoping a control array might be a possible answer.At this point i'm not sure.When I aatempt to execute the app by clicking the 'ADD' button. I get 'Expected Array' at the Execute method line.(last line of code below)VB6 expects sSql to be an array of some type.I hoped the execute method would take into account that the data was being captured in a control array.I am aware I could simply use a stored proc to accomplish this task but I am experimenting. Should this work?And if so,how? Thank-you in advance for any insight.

---------------------------------------------------------------------
Option Explicit
Public rstAlianza As ADODB.Recordset
Private Sub Form_Load()
'Adding a row to my table should NOT require a recordset object since I am
'simply INSERTNG data to the tabe directly.
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
cnn1.Open connStr
    'Construct SQL statement
    sSql = "INSERT INTO Authors(au_id,au_lname," _
        & "au_fname,phone,address,city,state,zip,contrac t)" _
    & "VALUES ('" _
    & Text1(0).Text & "','" _
    & Text1(1).Text & "','" _
    & Text1(2).Text & "','" _
    & Text1(3).Text & "','" _
    & Text1(4).Text & "','" _
    & Text1(5).Text & "')'"
cnn1.Execute sSql(lRecordsAffected, adCmdText) 'USING EMBEDDED SQL
End Sub



 
Old April 16th, 2007, 01:50 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Code:
Option Explicit

Public rstAlianza As ADODB.Recordset

Private Sub Form_Load()
    ' Adding a row to my table should NOT require a 
    ' recordset object since I am simply INSERTNG 
    ' data to the tabe directly.

    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

    cnn1.Open connStr

    ' Construct SQL statement
    sSql = "INSERT INTO Authors(au_id,au_lname, au_fname, " & _
                               "phone, address, city, state, " & _
                               "zip, contract)" & _
           "VALUES ('" & Text1(0).Text & "', " & _
                   "'" & Text1(1).Text & "', " & _
                   "'" & Text1(2).Text & "', " & _
                   "'" & Text1(3).Text & "', " & _
                   "'" & Text1(4).Text & "', " & _
                   "'" & Text1(5).Text & "') " & _
                   "'"                      ' the bold brown part is extraneous

    cnn1.Execute sSql(lRecordsAffected, adCmdText) ' Using embedded SQL
    ' the sSql(..., ...) syntax is being interpreted as a string
    ' array (  StrName(dimension_1, dimension_2) )

End Sub
 
Old April 16th, 2007, 09:33 PM
Authorized User
 
Join Date: Oct 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How can I correct this misinterpretation?

 
Old April 17th, 2007, 01:41 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Follow each argument with a comma.
Code:
    cnn1.Execute sSql, lRecordsAffected, adCmdText





Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert table records into another table. hewstone999 Access VBA 2 March 5th, 2008 11:01 AM
Control Array vikrantbaliyan Pro Visual Basic 2005 0 October 5th, 2007 05:38 AM
BULK INSERT FOR ARRAY MathLearner Beginning VB 6 4 September 28th, 2007 07:18 AM
How do I INSERT INTO multiple tables using array? aspiretech PHP Databases 0 December 10th, 2005 12:53 AM
Dynamic Control array [email protected] Visual C++ 6 September 3rd, 2003 08:48 AM





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