Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
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 25th, 2005, 01:46 PM
Registered User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using SQL Insert into with a variable

Hi all,

I have the following simple statements below. I kept getting a dialog box required to enter in a value for the variable START. How do I use the SQL Insert into with a variable?

    start = Now()


    StrSQL = "INSERT INTO log (start_date) VALUES (start);"
    DoCmd.RunSQL StrSQL

Appreciate for your help. Thanks.

 
Old April 25th, 2005, 04:45 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Change to:
Code:
    start = Now()  

    StrSQL = "INSERT INTO log (start_date) VALUES  (" & start & ")"
    DoCmd.RunSQL StrSQL
    You might need to format start a bit to get it to go into a date-type field. the example I have creates SQL like

“INSERT INTO tbl (fld1, date_fld)
    VALUES ('Hey', TO_DATE('FEB 3, 2003', 'MON DD, YYYY');”

(When entering SQL into SQL-Plus for Oracle, the SQL must be terminated with a “;” but in code, when building an SQL string, the “;” generates a syntax error.)

Access uses “#”s to delineate dates, so perhaps this will work for you
Code:
    start = Now()  

    StrSQL = "INSERT INTO log (start_date) VALUES  (#" & Format(start, "mm/dd/yyyy") & "#)"
    DoCmd.RunSQL StrSQL
    What you posted causes Access’ SQL interpreter to try to assess a variable in the SQL named start, whereas what you are trying to do is to feed a VBA variable into the string that will be sent to the SQL interpreter.
 
Old April 30th, 2005, 08:07 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 248
Thanks: 0
Thanked 1 Time in 1 Post
Default

It seems that you definitely want the time as part of your value. So you'll want to avoid any formatting.

If "start_date" is defined as a date field in "log" and "start" is DIM-med as a "Date" in your procedure, use something similar to Brian's last suggestion:

StrSQL = "INSERT INTO log (start_date) VALUES (#" & start & "#);"

Or you can skip DIM-ming "start" and just go to:

StrSQL = "INSERT INTO log (start_date) VALUES (#" & Now() & "#);"

The next question you'll ask is "how do I keep the 'Warning you are about to insert...' message from showing?". So... the short form of the whole looks like this:
Code:
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO log (start_date) VALUES  (#" & Now() & "#);"
DoCmd.SetWarnings True
Randall J Weers
Membership Vice President
Pacific NorthWest Access Developers Group
http://www.pnwadg.org
 
Old May 3rd, 2005, 01:40 PM
Registered User
 
Join Date: Apr 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Problem solved thanks to Brian & RJ!!!

here is my final statements:

   start = Now()


    StrSQL = "INSERT INTO log (start_date) VALUES ( #" & start & "#);"
    DoCmd.RunSQL StrSQL

In my table, I then set my column start_date as date. The symbol # is required to indicate date values . I want to capture the complete date value including time value so I did not use any formatting.
cheers !!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Using INSERT INTO with a string Variable Suspect Access VBA 2 January 6th, 2015 11:25 AM
Variable Usage in Insert Command tara0308 Access VBA 7 July 30th, 2007 12:50 PM
Insert Variable Value into DB Deano252 ASP.NET 2.0 Basics 1 June 24th, 2006 03:21 AM
How to insert variable value into regex.pattern? Batch Classic ASP Basics 3 January 3rd, 2005 05:07 PM
insert value of variable into an existing table mountaindew7612 SQL Server 2000 5 October 16th, 2004 12:48 PM





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