Click here to Skip to main content
12,549,732 members (44,778 online)
Rate this:
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, "")
Set rst1 = dbs1.OpenRecordset(strSQL1)
Posted 27-Aug-12 21:59pm
Updated 28-Aug-12 3:39am
Wes Aday104.2K
Wes Aday 28-Aug-12 9:43am
Does it work if you format your data to be in the form dd/MM/yyyy?
losmac 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 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 28-Aug-12 12:07pm
Yes, and this is the reason of his trouble. ;)
losmac 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 Is this default format for "AllProjects" table?
ryanb31 28-Aug-12 12:05pm
Just put single quotes around the date. WHERE [StartDate] = '" & ... & "'"
ChandraRam 30-Aug-12 8:57am
I think the delimiter is # (not single quotes) for Access tables.
ChandraRam 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
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:
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, "") & "#"
MsgBox strSQL1
What you see now?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.161021.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2016
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