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 April 19th, 2005, 05:27 PM
Registered User
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Kevinsharrison
Default RunSQL (insert into... ) HELP

For some reason this statement is not can you see any reason why

DoCmd.RunSQL ("insert into broadcast_orders (stn_call, start_date, " _
        & "end_date, total_weeks, employee, client, WEMG_rate, spotperweek, date, " _
        & "order_num) value ('" & lstAllOwner.ItemData(i) & "" _
        & "',#" & datestart & "#,#" & dateend & "#,'" & txtTotWeeks & "" _
        & "','" & cboemployee & "','" & txtclient & "','" & txtRate & "" _
        & "','" & txtspotwk & "',#" & txttodaydate & "#,'" & txtordernumb & "')")


I would appreciate any help

Thanks
Kevin

 
Old April 20th, 2005, 09:50 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

Several things...

You are using "Date" as a field name (after spotperweek). If your table truly has a field called "Date" you will have to put [] around the field like
Code:
spotperweek, [date],
Frankly, I recommend you rename the field.

You need to change "value" to "Select".

You need to drop the parenthesis around the Select. So, after you change "value" to "Select", remove the next ( and remove the ) inside the quote on the last line.

This isn't causing the problem, but the 3rd, 4th and 5th lines don't need the
Code:
& ""
at the end of the line because you put the close quote for your text on the next line.

To debug this yourself, open the query builder and paste the statement. Remove all of the VB syntax (the quotes on the strings, the ampersands and the underscores). Change the the field names to sample values. E.g.
Code:
'" & txtspotwk & "'
becomes
Code:
'spotwksample'
Then try to view the query.

If I was writing this, it would be like:
Code:
Dim strSQL as string

strSQL = "insert into broadcast_orders (stn_call, start_date, " _
        & "end_date, total_weeks, employee, client, WEMG_rate, spotperweek, " _
        & "abetternamedfieldfordate, " _
        & "order_num) Select '" & lstAllOwner.ItemData(i) & "', " _
        & "#" & datestart & "#, #" & dateend & "#, '" & txtTotWeeks & "', " _
        & "'" & cboemployee & "', '" & txtclient & "', '" & txtRate & "', " _
        & "'" & txtspotwk & "', #" & txttodaydate & "#, '" & txtordernumb & "'")
Debug.print strSQL
DoCmd.RunSQL strSQL
Notice, by building the string before the RunSQL, I can use "Debug.Print" to see the actual SQL statement. I can then take that SQL statement to query builder and find out what is wrong. When the code is correct, I remove (or comment) the "Debug.Print".

NOTE: Beware of your text fields having immbedded quotes (") or apostrophies ('). This will hose your SQL statement. If that's a possibility, you should open an RecordSet for append and set the fields one at a time.

Give the man a fish, he eats for a day. Teach the man to fish...

Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old April 20th, 2005, 10:01 AM
Registered User
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Kevinsharrison
Default

Thanks for the info rjweers. I am a new database builder so any info is good. I know that i don't know everything.

I think that i will try something like that. I finally got it to work.

I apreciate the help and the knowledge.

Kevin

 
Old April 20th, 2005, 02:21 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Randall,
Code:
INSERT INTO tblTarget (      fld1,       fld2,  num1)
VALUES                ('Text one', 'Text two', 12345)
is valid syntax.

You would replace VALUE with SELECT (and remove the parens) if the values to go into the target table are coming from a select query:
Code:
INSERT INTO tblTarget ( fld1, fld2, num1)
  SELECT fldA, fldB, numA
  FROM   tblSrc
  WHERE  fldTest='Test Value'
(Note that the field count, and the fields’ types must match, or conversion functions of the database system must be employed [TODATE(), TOCHAR(), etc.].)


But you are spot-on about the field name “Date” being problematic, and a prime candidate for a renaming.

Brian
 
Old April 26th, 2005, 07:45 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

Thanks Brian. I suppose the trouble I had after correcting the original query in Query Builder was that it was "VALUE" not "VALUES". Since I often use Query Builder to get my SQL Syntax correct, it uses SELECT. I guess I knew there was a different syntax. I just never bothered to check into. Hey, if it works, use it!
 
Old April 26th, 2005, 11:17 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Yeah, query builder is the greatest!

For instance, I can never remember how to specify finding duplicates, but query builder doesn't forget.

I often use query builder to get to the syntax that I wind up using in Oracle!





Similar Threads
Thread Thread Starter Forum Replies Last Post
RunSql action rashi Access 1 September 28th, 2007 01:24 PM
Docmd.runsql anne.burrows VB How-To 2 October 25th, 2006 08:21 AM
Question about DoCmd.RUNSQL using an insert statem Nasem VB Databases Basics 1 May 5th, 2006 10:15 AM
docmd.runsql "select RodMead Access VBA 2 July 31st, 2004 02:55 PM
Help with DoCmd.RunSQL command ricmar Access VBA 3 July 21st, 2004 03:32 PM





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