Solved

dates in SQL-clauses

Posted on 1998-08-03
8
145 Views
Last Modified: 2010-05-03
I have dBase IV database where is date field and date format is like this: 20.7.1998 (day.month.year).

Then I have Data component and I wanna set components RecordSource property in a runtime. I tryed this but this isn't working.
Data1.RecordSource = "SELECT * FROM " + myDB + " WHERE Date >= #24.7.1998# AND Date <= #24.7.1998#"

Why this isn't working? Could you give me an example?

How I set it if those Dates are variables?

Thanks in advance!
0
Comment
Question by:majala
8 Comments
 
LVL 14

Expert Comment

by:waty
ID: 1467565
Did you tried your SQL like this :

Data1.RecordSource = "SELECT * FROM " + myDB + " WHERE Date >= #24/7/1998# AND Date <= #24/7/1998#"

/ instead of . between day,month, year.
0
 

Expert Comment

by:vinoopauls
ID: 1467566
Try this:

Data1.RecordSource = "SELECT * FROM  tablename  WHERE Date >= ' 24/7/1998' AND
      Date <= '24/7/1998' " 

Data1.RecordSource ="SELECT * FROM  tablename  WHERE Date >= ' " & datevar1 &" '                                   AND  Date <= ' " & datevar2 &" ' "

The datatype in the database should be date and the format being sent should match.
Also your system date format should match the database.
0
 

Expert Comment

by:jaimereed
ID: 1467567
When you set a record source property you must use valid Jet SQL syntax (similar to ANSI). For dates this means that you must use the American "mm/dd/yy" format. You must rephrase your date using something like:

dim d as date
d= THEDATEINQUESTION
Data1.RecordSource = ".....#" + Format("mm/dd/yy", d) +"#..."

You can avoid this by specifying the month as a string Jan/Feb/etc.

You should try to avoid periods (although you can use them with Format() if the Windows country settings are set correctly). The database engine will provide the neccessary conversion to the format of your database.
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:majala
ID: 1467568
Vinoopauls: Answer you gave didn't work. I guess there's nothing wrong in your answer (I didn't got any errors when I ran my program) but I think that I have to use those #-marks if play with dates?

That comment given by WATY did work. If you send your answer as a question then I will grade your answer. Would you at a same time give me an example how to do if those dates are variables?
0
 
LVL 14

Accepted Solution

by:
waty earned 30 total points
ID: 1467569
Did you tried your SQL like this :

Data1.RecordSource = "SELECT * FROM " + myDB + " WHERE (Date >= #" & Format("DD/MM/YYYY", yourDate1)& ") AND (Date <= #" & Format("DD/MM/YYYY", yourDate2) & "#)"

0
 

Author Comment

by:majala
ID: 1467570
Actually I got it work without those format-clauses:

criteria = "SELECT * FROM " + myDB + " WHERE Date >=#" + myDate1 + "# AND Date <= #" + myDate2 + "#"

0
 
LVL 14

Expert Comment

by:waty
ID: 1467571
It is quite usefule to use the format when you users coming from different countries (like I have here) and having different date format
0
 
LVL 14

Expert Comment

by:waty
ID: 1467572
It is quite usefule to use the format when you users coming from different countries (like I have here) and having different date format
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

803 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