473,320 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

SQL search using Year() function

I have a popup form opened as acDialog which prompts the user to enter just the year with which to display all relevant records in a continuous form. The variable BidYear passes the correct value back to the main form where the SQL code is executed but Access throws up another input box to enter a year. If I remove the variable BidYear and instead type in a year such as 2016 in its place, all records whose date field contain a date in 2016 are displayed. I found one article that states to enclose the variable in single quotes but all that does is display 0 records.
Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource = "SELECT tblJobs.*, tblIsland.* FROM tblJobs INNER JOIN tblIsland ON tblJobs.JobID = tblIsland.JobID WHERE Year([BidDate])=BidYear ORDER BY BidDate ASC;"
Apr 5 '17 #1

✓ answered by NeoPa

You may find Literal DateTimes and Their Delimiters (#) helpful.

I would advise filtering by the date value itself if it's indexed for more efficient SQL.

EG :
Expand|Select|Wrap|Line Numbers
  1. WHERE ([DateField] Between #2017-1-1# And #2017-12-31#)
Obviously your code would need to create that string based on wherever you have your year value stored.

5 856
PhilOfWalton
1,430 Expert 1GB
Try

WHERE CInt(Year(BidDate)) = CInt(BidYear)

This should ensure they are both integers.
Note that you will have a problem if the operator does not enter a 4 DIGIT number in the input box.

My own preference would be to have a table of relevant years - 2000, 2001, 2002..... 2017, 2018 and a Combo box to select the appropriate year.

Phil
Apr 5 '17 #2
Phil, thanks for your reply. I tried that but got the same result of Access popping up with another parameter prompt. I did discover that by changing the Where clause to reflect the form instead of the variable, it worked as expected. I actually did start out with a combo box in the header but the form was too crowded with just that little extra....
Expand|Select|Wrap|Line Numbers
  1. WHERE Year([BidDate])=Forms![frmBidYearInput].[txtBidYear] ORDER BY BidDate ASC;"
Apr 5 '17 #3
NeoPa
32,556 Expert Mod 16PB
You may find Literal DateTimes and Their Delimiters (#) helpful.

I would advise filtering by the date value itself if it's indexed for more efficient SQL.

EG :
Expand|Select|Wrap|Line Numbers
  1. WHERE ([DateField] Between #2017-1-1# And #2017-12-31#)
Obviously your code would need to create that string based on wherever you have your year value stored.
Apr 6 '17 #4
I actually started it this way but wasn't 100% sure of how to convert the year that was input into the sql string. I did see several articles about using variables within the sql code but could not get the global variable to pass the value and avoid sending up a second prompt. Interestingly enough that by referring to the form worked first time. Thanks for the article tip, is very helpful.
Apr 6 '17 #5
NeoPa
32,556 Expert Mod 16PB
Hi Jal.

You're very welcome :-)

-Ade.
Apr 6 '17 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x";...
2
by: Paolo | last post by:
I have a form with three fields: STARTINGDATE ENDINGDATE YEARS I would like the YEARS field to show the number of years which elaps from the STARTINGDATE TO THE ENDINGDATE fields. Please...
12
by: Itay | last post by:
While going through the code of the open source project PCSX2 (www.pcsx2.net - PS2 emulator for windows and linux. source code can be d/led from here: http://tinyurl.com/6242p), I saw a strange (at...
0
by: Johan | last post by:
Hi How do I preform a paged search using the Directory searcher in VB.NET? I'm getting the first 1000 entries in the AD when I'm doing a normal search using the Directory searcher, and then...
4
by: Vasilis X | last post by:
Hello. I want to use the excel function Year( ) (or Day, Month, Minute etc...) in a Visual Basic .net application. How can i do this? I tried Excel.WorksheetFunction but this class doesn't...
0
by: jojo41300000 | last post by:
Hi all, Is there any ideas how to implement the fuzzy search using soundex in SQL server 2005? Could you please post some suggestions, ideas, or source codes if you have already...
8
by: Warren Moxley | last post by:
Hi there, i've been searching for a C String search and replace function. I need to find all occurrences of " " in a char* array, and replace them with another char, I know how to do this in...
7
by: ewwachi | last post by:
how does one search using date "from to" while still selecting a user from a drop down list
2
Prasant jinaga
by: Prasant jinaga | last post by:
Hi, I have a query to fetch out some records based on the year,In the where clause when i am fetching on the basis of Date range i.e,1st Jan'2011 and Today's Date like ...
10
by: mrijet | last post by:
Hi everybody, Can anyone guide me how to create an advanced search using combo box? For right now, I just can search using only one field which is my primary key. So now, I want to search by...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.