473,416 Members | 1,720 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,416 software developers and data experts.

Search form/query with dates - getting mismatch errors

Hello,
I'm not great at VBA but have hammered someone else's code found on the internet into shape except for a couple of issues, the one I'm currently working on is that I can't search for a date. Someone on here mentioned use of #'s but I'm not sure how with the code I currently have - could be I need to re-write the section for the date..?

Currently I have:
Expand|Select|Wrap|Line Numbers
  1. Private Sub btnSearch_Click()
  2.  
  3.     Dim sqlinput As Variant
  4.  
  5.     ' Update the record source
  6.     sqlinput = "SELECT * FROM FileInfQry " & BuildFilter
  7.  
  8.     Debug.Print sqlinput
  9.  
  10.     Me.FileInfSubForm.Form.RecordSource = sqlinput
  11.  
  12.     ' Requery the subform
  13.     Me.FileInfSubForm.Requery
which refers too:
Expand|Select|Wrap|Line Numbers
  1.     ' Check for Date Created
  2.     If Me.txtDateCreated > "" Then
  3.         varWhere = varWhere & "[DateCreated] = '" & Me.txtDateCreated & "' AND "
  4.     End If
Any help is very gratefully received! Thanks :)
Jul 1 '13 #1

✓ answered by zmbd

Yes, anytime you have a "Date" field that you are pulling data from you should normally enclose the criteria within the "#" signs

So in the small block of code you posted, if the field [DateCreated] is formated at the table level as a date field, then you would replace the single quotes ( " ' ") with the "#" to get something like:
Expand|Select|Wrap|Line Numbers
  1.     ' Check for Date Created 
  2.     If Me.txtDateCreated > "" Then 
  3.         varWhere = varWhere & "[DateCreated] = #" & Me.txtDateCreated & "# AND " 
  4.     End If 
  5.  
Mind you, I have not checked your code for any errors etc... I've only showed you where to place the "#"

15 1808
zmbd
5,501 Expert Mod 4TB
Yes, anytime you have a "Date" field that you are pulling data from you should normally enclose the criteria within the "#" signs

So in the small block of code you posted, if the field [DateCreated] is formated at the table level as a date field, then you would replace the single quotes ( " ' ") with the "#" to get something like:
Expand|Select|Wrap|Line Numbers
  1.     ' Check for Date Created 
  2.     If Me.txtDateCreated > "" Then 
  3.         varWhere = varWhere & "[DateCreated] = #" & Me.txtDateCreated & "# AND " 
  4.     End If 
  5.  
Mind you, I have not checked your code for any errors etc... I've only showed you where to place the "#"
Jul 1 '13 #2
That's great, thanks, cleared the initial issue. Now that's highlighted something different - when I search for '08/04/1991' or '07/05/1991' I get nothing (despite there being a record with that date), but when I search '13/08/1991' or '20/08/1991' I get hits... Is it something to do with the preceding '0'? Or is my code just that broken? :P

Thanks for the help :)
Jul 1 '13 #3
zmbd
5,501 Expert Mod 4TB
It shouldn't matter so long as the date is within the "#" If you use a single or double quote you will have issues searching on a date-field. If the field happens to be text, then the search is quite litteral and the entry "00" does not equal "0" etc...

Please go back in to your table in design view and double check what data type has been set for the field.
Jul 1 '13 #4
Hmm, I've checked and the original table is set to Date/Time for that field.

To clarify - I'm not using the quotes as part of the search terms, just typing in: 07/05/1991

Cheers
Jul 1 '13 #5
zmbd
5,501 Expert Mod 4TB
Line 8 of your first code block you have a debug print.
Please post the results of that line, please click on the [CODE/] button first and place the result between the two code tads.
Also, which version of access are you using/
Jul 1 '13 #6
Hi, print out is:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM FileInfQry WHERE [DateCreated] = #08/04/1991#
I'm using Access 2007.

Cheers :)
Jul 2 '13 #7
MikeTheBike
639 Expert 512MB
Hi

The # delimiter for dates is a common problem with dates. I would suggest the most common problem if you are un the UK (?), or a region setting that doesn't use US dates, is the date format when using VB/VBA.

Based on you dates given in you posts this would seem to be the case. ie. dates '13/08/1991' or '20/08/1991' cannot be anything else but August as months 13 and 20 do not exist, hence tyhese work OK. However, '08/04/1991' or '07/05/1991' would be interpreted as 4th August and 5th July.

To overcome this when using variable you need to format the date ie.
Expand|Select|Wrap|Line Numbers
  1. ...#" & Format(DateVariable,"MM/DD/YYYY") & "# ..
HTH

I believe there are articles in this forum explaining this better than this, but not sure where !!??


MTB
Jul 2 '13 #8
Thanks for the hint, soundsl ike you could be right. However, having tried to change the code, I am now gettign an error:

Run time error '2455'
You entered an expression that has an invalid reference to the property RecordSource

I've got no idea what's changed - the line I changed now looks like:

Expand|Select|Wrap|Line Numbers
  1.         varWhere = varWhere & "[DateCreated] #" & Format(Me.txtDateCreated, "DD/MM/YYYY") & "# AND "
So unless I've made a stupid mistake in there I'm lost? :/

Thanks for your help :)
Jul 2 '13 #9
zmbd
5,501 Expert Mod 4TB
MTB,
That was my next statement about the dates... Just wanted to make sure that the string was resolving correctly.
Note the string posted in #7 isn't what I'm expecting from the code in the second block in #1


Allen Browne's site -International Dates in Access
(....)
2. Wrong Formatting in Code
In VBA code, delimit dates with the "#" symbol. Regardless or your regional settings, Access expects these literal dates to be in the American format, e.g. #12/31/1999#.

To demonstrate this, enter any date in the Criteria row under a date field in Query Design, and then switch to SQL View. In Query Design view, you see the date according to your local settings, but the SQL statement uses mm/dd/yyyy format.

SQL clauses are not always obvious, e.g. the Filter of a form, the WhereCondition of OpenReport, or the third argument of a domain aggregate function. Examples:

•DoCmd.OpenReport "MyReport", acViewPreview, , "InvoiceDate > #12/31/2000#"
•Debug.Print DLookup("StudentID", "tblStudent", "EntryDate = #6/30/1953#")
•strSQL = "SELECT * FROM tblDonation WHERE DonationDate > #" & Format(Me.StartDate, "mm\/dd\/yyyy") & "#;" (....)

last part of your code is (...) ") & "# AND "
What follows?
Please post the entire code block.
Jul 2 '13 #10
zmbd
5,501 Expert Mod 4TB
Just noted:Format(Me.txtDateCreated, "DD/MM/YYYY")
should be: Format(Me.txtDateCreated, "MM/DD/YYYY")

One thing to note:
{edit - reworded the following...}
If your PC has the local settings to English(USA) then if you have April 12, 2013 stored in the European format "12/04/2013" then using Format(Me.txtDateCreated, "MM/DD/YYYY") will often return 12/04/2013 not 04/12/2013 as expected. The reason is because format recognizes the 12/04/2013 to be a valid date.
Jul 2 '13 #11
Hi,
I believe the 'AND' at the end of the line is surplus to requirements (possibly somethign to do with the wildcards in the other lines I just copied over?).

Anyway, the whole block is:
Expand|Select|Wrap|Line Numbers
  1.     ' Check for Date Created
  2.     If Me.txtDateCreated > "" Then
  3.         varWhere = varWhere & "[DateCreated] #" & Format(Me.txtDateCreated, "MM/DD/YYYY") & "# AND "
  4.     End If
Cheers :)
Jul 2 '13 #12
zmbd
5,501 Expert Mod 4TB
Try fixing the format command first.

Otherwise, we'll need to start a new thread for the 2455 error - if we do need to, please post all of the code (from sub *() thru end sub)
Jul 2 '13 #13
Ok, I'm a little confused now - My PC is in England, set to DD/MM/YYYY format. Which format do I need to force in VBA?! Tbh, I've tried both with no change in result, so I guess it's likely not that that's the problem?
Jul 2 '13 #14
zmbd
5,501 Expert Mod 4TB
Did you read AB site?

Your date, as set in code, should be in the "mm/dd/yyyy" format.

If your region/local setting (via the OS Control Panel) is set to have the date as "DD/MM/YYYY" then when you use the format function as given, it should convert the date the the "mm/dd/yyyy" format expected by the Access engine.

If you are still getting the 2455 error despite fixing the date format, then there is an issue deeper within your code.

Start with: > Before Posting (VBA or SQL) Code
You may need to repeat the compile step several times as the compiler with stop on the first error it finds.
You will need to start a new thread for the 2455 troubleshooting.
Jul 2 '13 #15
Hi, I had a quick read through, but my brain wasn't accepting the data (been a long day!)... Thanks for clarifying it!

I'll have a look at that link and sort out another thread for the new problem, thanks for your help! :)
Jul 2 '13 #16

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

Similar topics

3
by: screenwriter776 | last post by:
Hi, folks - Perhaps you could help me with a search form I am building. I have a table with a field in it. I want to return dates inside two parameters the user enters into a search form:...
3
by: eddie wang | last post by:
Hello, I am working on an Access database. For some strange reason, my query keeps getting erased by itself. The SQL view of my query object looks like this: SELECT * FROM emp WHERE...
6
by: Howard Kaikow | last post by:
I'm doing a VB 6 project in which I am trying to protect against type mismatch errors. Is the process any different in VB .NET? Here's what I'm doing in VB 6. I have an ActiveX DLL. The...
31
by: DWolff | last post by:
I'm somewhat new to Access, and have no VB experience at all (except for modifying existing code where obvious). I built a data entry form that works fine. Typically, a client will call in and...
1
by: hottoku | last post by:
Hi All, I'm having quite a bit of trouble designing a search tool to work with my database. I have found lots of examples from Microsoft Templates to Allen Browne's sample search form. The...
6
by: OllyJ | last post by:
Hope you can help guys. I have a scheduling database, the main schedule table contains the following fields: AddedDate Day/Night Machine Arm/Head StockCode ToolNumber
2
by: chungiemo | last post by:
Hi Everybody, I am using Alan Brownes Example of a search form example and I keep getting an error of the following:- Enter Parameter Value Msg Box with the entered Value "Mar" and I re-enter...
8
by: munkee | last post by:
Hi everyone, I am using the following code adapted from Allen Browne: 'Purpose: This module illustrates how to create a search form, _ where the user can enter as many or few...
1
by: Travis818 | last post by:
I have created a database for a small gym where they are capturing data on members for personal training purposes. I have created a custom parameter query that grabs its information from a search...
9
f430
by: f430 | last post by:
i have been trying to write a search code for a similar database, and i followed all the steps that were provided above, and my code was close to what lightning had but i have added date range in my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.