Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL VBA
I want this code to work, I have tried all possible combinations and finally posting here. All this code is supposed to do is to get a row from the table using 3 input parameters. The problem comes when using the date in Sql statement, it dosent work but if I use a variable "nam" and change the condition criteria accordingly it works.
 
Your help will be greatly appreciated.
 
Dim dbs1 As DAO.Database
Dim rst1 As DAO.Recordset
Dim strSQL1 As String
Dim temp As Date
nam = "DeEx"
 temp = [Forms]![AllProjects1]![Combo3].Value
 temp = Format(temp, "dd-mm-yyyy")
Set dbs1 = CurrentDb
'strSQL1 = "SELECT * FROM AllProjects WHERE Team= '" & nam & "'" & " AND Team= '" & nam & "'"
strSQL1 = "SELECT * FROM AllProjects " & "WHERE [StartDate]=" & Format(temp, "dd.mm.yyyy")
Set rst1 = dbs1.OpenRecordset(strSQL1)
Posted 27-Aug-12 21:59pm
Edited 28-Aug-12 3:39am
Wes Aday90.2K
v4
Comments
Wes Aday at 28-Aug-12 9:43am
   
Does it work if you format your data to be in the form dd/MM/yyyy?
losmac at 28-Aug-12 11:48am
   
It should works for dd/mm/yyyy, but default, prefered MS format for date is: MM/dd/yyyy.
Wes Aday at 28-Aug-12 11:50am
   
And he is formatting his string to something very weird. Which may or may not be the format of his database.
losmac at 28-Aug-12 12:07pm
   
Yes, and this is the reason of his trouble. ;)
losmac at 28-Aug-12 11:45am
   
Please, answer me few simple questions:
1) Why do you use DAO? Access 2k7 default engine: ADO!
2) Is the above code calling from standard module, class module or from UserForm module?
3) Why do you format data as dd.mm.yyy? Is this default format for "AllProjects" table?
ryanb31 at 28-Aug-12 12:05pm
   
Just put single quotes around the date. WHERE [StartDate] = '" & ... & "'"
ChandraRam at 30-Aug-12 8:57am
   
I think the delimiter is # (not single quotes) for Access tables.
ChandraRam at 30-Aug-12 8:59am
   
The first format statement
temp = Format(temp, "dd-mm-yyyy")
is not really necessary.
 
Try this:
strSQL1 = "SELECT * FROM AllProjects WHERE [StartDate]=#" & temp & "#"

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First of all, read this article[^] and thread on ozgrid forum[^] and all related articles.
 
To get data from table, use the following query:
SELECT *
FROM AllProjects
WHERE [StartDate]= #09/18/2012#
 
I recommend you to build a query and check its content in the run-time using MsgBox, for example:
strSQL1 = "SELECT * FROM AllProjects " & "WHERE [StartDate]=#" & Format(temp, "dd.mm.yyyy") & "#"
MsgBox strSQL1
What you see now?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100