Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA 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 May 16th, 2005, 06:20 PM
Registered User
 
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL INSERT INTO Error

 I am fairly new to message boards so any help would be appreciated. I am trying to create some code that will read the value of one column to the end of another and take that data and place it into a new table.

Example

Field 1: 1
Field 2: 49

All values between 1 and 49 will be plugged into the new table.
All my code is below; If you need any more info please let me know.



Private Sub Command3_Enter()

Dim DBS As Database
Dim RST As Recordset
Dim Book As Single
Dim FirstDoc As Single
Dim LastDoc As Single
Dim I As Single
Dim INTableName As String
Dim OutTableName As String

INTableName = "Ocean_Missings_1"
OutTableName = "Ocean_Complete_Missings"

Set DBS = CurrentDb
Set RST = DBS.OpenRecordset(INTableName)
If RST.BOF = False Then RST.MoveFirst
While RST.EOF = False

    Book = Left(RST.Fields("Book").Value, 4)
    FirstDoc = Right(RST.Fields("Start").Value, 4)
    If IsNull(RST.Fields("Last").Value) = False Then
        LastDoc = Right(RST.Fields("Last").Value, 4)
        DoCmd.SetWarnings True
            For I = FirstDoc To LastDoc
                DoCmd.RunSQL ("INSERT INTO" & OutTableName & "(Book, Start) Values (" & Book & "'" & I & ")")

            Next I

        Else

            DoCmd.RunSQL ("Insert Into " & OutTableName & "(Book, Start) VALUES (" & Book & "'" & Right(RST.Fields("Book").Value, 5) & ")")
        End If

        RST.MoveNext
        I = 0
    Wend
    DoCmd.SetWarnings True

End Sub

 
Old May 16th, 2005, 08:50 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

Unfortunately you haven't told us what the error or problem is.

But I do see errors in your Insert statement.

DoCmd.RunSQL ("INSERT INTO" & OutTableName & "(Book, Start) Values (" & Book & "'" & I & ")")

should be

DoCmd.RunSQL ("INSERT INTO " & OutTableName & " (Book, Start) Values ('" & Book & "', " & I & ")")

And

DoCmd.RunSQL ("Insert Into " & OutTableName & "(Book, Start) VALUES (" & Book & "'" & Right(RST.Fields("Book").Value, 5) & ")")

should be

DoCmd.RunSQL ("Insert Into " & OutTableName & " (Book, Start) VALUES ('" & Book & "', " & Right(RST.Fields("Book").Value, 5) & ")")

I'm assuming that the field Book doesn't contain any apostrophies ('). If so you need to change the apostrophies to double quotes ("") like this:

DoCmd.RunSQL ("Insert Into " & OutTableName & " (Book, Start) VALUES (""" & Book & """, " & Right(RST.Fields("Book").Value, 5) & ")")

Of course that syntax assumes that Book doesn't contain any quotes (").

Also, on the last, I'm assuming that Right(RST.Fields("Book").Value, 5) is a numeric value.

Recommendation: Instead of trying to write the RunSQL statement altogther, try this for debugging,

Dim sql as String

sql = "INSERT INTO " & OutTableName & " (Book, Start) Values ('" & Book & "', " & I & ")"

Debug.Print sql

DoCmd.RunSQL sql

The Debug.Print will show the Insert statement that is executing. It will be a little more obvious what the problem is.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old May 17th, 2005, 12:15 PM
Registered User
 
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your help sometime all you need is a second set of eyes.

Quote:
quote:Originally posted by rjweers
 Unfortunately you haven't told us what the error or problem is.

But I do see errors in your Insert statement.

DoCmd.RunSQL ("INSERT INTO" & OutTableName & "(Book, Start) Values (" & Book & "'" & I & ")")

should be

DoCmd.RunSQL ("INSERT INTO " & OutTableName & " (Book, Start) Values ('" & Book & "', " & I & ")")

And

DoCmd.RunSQL ("Insert Into " & OutTableName & "(Book, Start) VALUES (" & Book & "'" & Right(RST.Fields("Book").Value, 5) & ")")

should be

DoCmd.RunSQL ("Insert Into " & OutTableName & " (Book, Start) VALUES ('" & Book & "', " & Right(RST.Fields("Book").Value, 5) & ")")

I'm assuming that the field Book doesn't contain any apostrophies ('). If so you need to change the apostrophies to double quotes ("") like this:

DoCmd.RunSQL ("Insert Into " & OutTableName & " (Book, Start) VALUES (""" & Book & """, " & Right(RST.Fields("Book").Value, 5) & ")")

Of course that syntax assumes that Book doesn't contain any quotes (").

Also, on the last, I'm assuming that Right(RST.Fields("Book").Value, 5) is a numeric value.

Recommendation: Instead of trying to write the RunSQL statement altogther, try this for debugging,

Dim sql as String

sql = "INSERT INTO " & OutTableName & " (Book, Start) Values ('" & Book & "', " & I & ")"

Debug.Print sql

DoCmd.RunSQL sql

The Debug.Print will show the Insert statement that is executing. It will be a little more obvious what the problem is.

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old May 17th, 2005, 12:23 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

When you open a recordset, it is always positioned at the 1st record if any records are returned.

So
Code:
    If RST.BOF = False Then RST.MoveFirst
is unnecessary.



Code:
    While RST.EOF = False
        . . .
    Wend
    is much better as
Code:
    Do Until RST.EOF
Code:
        . . .
    Loop



The .Value property is the default, so
Code:
    Book = Left(RST.Fields("Book").Value, 4)
could be
Code:
    Book = Left(RST.Fields("Book"), 4)
(a little easier to read). Additionally, the .Fields collection is the default property of a recordset object, so it could be
Code:
    Book = Left(RST("Book"), 4)
(even easier to read), or better (since your field name has no spaces in it), you could use the syntax
Code:
    Book = Left(RST!Book, 4)


I think that you will probably get the results you want with Randall’s suggestions. Your code as posted didn’t put spaces between elements that needed them, for one thing, and didn't delineate literal strings in the SQL from references in the SQL to tables, fields, and so on.

For instance, the first point that Randall makes:
Code:
    DoCmd.RunSQL ("INSERT INTO" & OutTableName & "(Book, Start) Values (" & Book & "'" & I & ")")
should be
Code:
    DoCmd.RunSQL ("INSERT INTO " & OutTableName & " (Book, Start) Values ('" & Book & "', " & I & ")")
is because your code would evaluate to
Code:
  "INSERT INTOOcean_Complete_Missings(Book, Start) Values (John Henry'1)"
(if Book equaled "John Henry" and I equaled 1), whereas Randall’s repair of the code evaluates to
Code:
  "INSERT INTO Ocean_Complete_Missings (Book, Start) Values ('John Henry', 1)

Is what you want to do to make a whole series of records with the same book title, but a sequential series of numbers?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 11:17 AM
T-SQL statement causing error with insert/update saf SQL Language 0 September 7th, 2007 04:48 PM
SQL Insert C# N4th SQL Server 2000 6 December 5th, 2006 05:28 AM
Error in SQL INSERT query lawsoncobol Access VBA 1 August 4th, 2006 04:25 AM
SQL INSERT mattastic Classic ASP Databases 3 February 24th, 2005 07:56 AM





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