Solved

One line of VBA ... frustrating , quotes, single and double etc

Posted on 2011-02-28
11
268 Views
Last Modified: 2012-05-11
This nex t line below works fine

CurrentDb.Execute "insert into [tblAuditlog] (Auditstr) values ( ' " & AuditDetails & " ')"


However, I also have a field AuditDate in tblAuditlog and I want to put now() into this field.

How do I change my VBA to add this date.

... tried multiple things so far but failed..
0
Comment
Question by:Patrick O'Dea
  • 2
  • 2
  • 2
  • +3
11 Comments
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 100 total points
ID: 34996912
If it's truly a date then try date() rather than now()

Chris
0
 
LVL 18

Assisted Solution

by:Cluskitt
Cluskitt earned 100 total points
ID: 34996932
CurrentDb.Execute "insert into [tblAuditlog] (Auditstr,AuditDate) values ( ' " & AuditDetails & " ','" & Now() & "')"

Or use date()
0
 
LVL 12

Assisted Solution

by:Paul_Harris_Fusion
Paul_Harris_Fusion earned 100 total points
ID: 34996935
Build a string and include the datetime  as a formatted string (using the format function)

e.g.    
dim s as string
s = "INSERT INTO [tblAuditLog]  mydateField VALUES "
s = s & "(#" & format$(Now() , "ddd/mmm/yyyy hh:nn:ss")  & "#)"

CurrentDb.Execute s
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 150 total points
ID: 34997031
CurrentDb.Execute "insert into [tblAuditlog] (Auditstr,auditDate) values ( '" & AuditDetails & "',Now())"

or this


CurrentDb.Execute "insert into [tblAuditlog] (Auditstr,auditDate) values ( '" & AuditDetails & "',#" & Now() &" #)"

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34997067
Depending on string or numeric (I think) it would be :

CurrentDb.Execute "insert into [tblAuditlog] (AuditDate) values ( ' " & date() & " ')"
or
CurrentDb.Execute "insert into [tblAuditlog] (AuditDate) values (date())"

Chris
0
 

Author Comment

by:Patrick O'Dea
ID: 34997108
Thanks for you help.
I have lots of solutions.

Before I close the query...

Can anybody refer me to a site that explains the the usage of quotes and single quotes when inserting into a table.
I understand most of this area but seem to struggle (and guess) everytime.
0
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 34997135
SQL uses single quotes to denote strings.
VBA uses double qotes to denote strings

So a VBA string that contains an SQL statement will use double-quotes for the vb part but single quotes internally.

e.g.    
dim sql as string
sql = " INSERT INTO MYTABLE (FIRST_NAME)  VALUES (  'paul' )   "
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 34997140
Access accepts both quotes and double quotes. SQL only accepts quotes. Quotes force the value to be considered as string. Usually, when you use quotes with dates, it gets converted automatically (same as numbers when inserting the string '5' into a numeric field, for example).
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 50 total points
ID: 34997270
None seems complete:

CurrentDb.Execute "Insert Into tblAuditlog (Auditstr, AuditDate) Values ('" & AuditDetails & "', Now())"

/gustav
0
 

Author Closing Comment

by:Patrick O'Dea
ID: 34997461
thanks all
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34997707
<None seems complete:>
post at http:#a34997270  a copy of post from http:#a34997031
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question