Solved

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

Posted on 2011-02-28
11
266 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
 
LVL 119

Accepted Solution

by:
Rey Obrero 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 119

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now