Solved

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

Posted on 2011-02-28
11
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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 51

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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 …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

617 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