Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-28
11
Medium Priority
?
273 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 400 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 400 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 600 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 52

Assisted Solution

by:Gustav Brock
Gustav Brock earned 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

824 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