473,503 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with syntax (missing operator?)

I have a piece of code (below) that is giving me fits. I copied this code from elsewhere on the net, and I have been fiddling with the quotations in the Where clause, as that seems to be the problem. Depending on what I change, I get compile errors, syntax errors, etc.

I have an unbound combo box (cboEffDate). The recordsource for this box is a simple query that shows the effective dates stored in tblCapacity.EffDate. What I want to happen is for the user to select an Effective Date from the drop down, then have my text boxes on the form populate with the field values associated with the effective date. Once those fields are populated with the correct set of variables, there are some calculations.

The error is in the "where" clause.
Also, the field [EffDate] is a short date type in the table. The cboEffDate is also formated for short date.


Expand|Select|Wrap|Line Numbers
  1. Private Sub cboEffDate_AfterUpdate()
  2.     ' Pull info out of SQL View.
  3.  
  4.     Dim db As Database
  5.     Dim rs As Recordset
  6.  
  7.     Set db = CurrentDb
  8.     Set rs = db.OpenRecordset("SELECT * " & _
  9.     "FROM tblCapacity " & _
  10.     "WHERE [EffDate] = " & Me.cboEffDate.Value & "")
  11.     Me.txtFieldCap = rs![Field Inlet Capacity]
  12.     Me.txtT1Start = rs![T1 Start Date]
  13.     Me.txtT1End = rs![T1 End Date]
  14.     Me.txtT1Inlet = rs![T1 Inlet Gas]
  15.     Me.txtT1Residue = rs![T1 Residue Design]
  16.     Me.txtT1Fuel = rs![T1 Fuel]
  17. End Sub
Oct 21 '10 #1
23 1649
dsatino
393 Contributor
"WHERE [EffDate] = #" & Me.cboEffDate.Value & "#")

You need to enclose dates with the # character for Access queries.
Oct 21 '10 #2
I typed:
"WHERE [EffDate] = #" & Me.cboEffDate.Value & "#")

Now I get runtime 3075:
Syntax error in date in query expression 'EffDate = #'

I wonder if I have an extra space somewhere that I should not.
Oct 21 '10 #3
Now I am getting a different error:
Expand|Select|Wrap|Line Numbers
  1. RunTime 3075
  2. Syntax error in date in query expression '[EffDate] = #'
here is what I have currently:
"WHERE [EffDate] = #" & Me.cboEffDate.Value & "#")
Oct 21 '10 #4
dsatino
393 Contributor
Sorry, try this:

Expand|Select|Wrap|Line Numbers
  1. "WHERE tblCapacity.EffDate = #" & Me.cboEffDate.Value & "#") 
  2.  
Oct 21 '10 #5
Same error, unfortunately.

Syntax error in date in query expression 'tblCapacity.EffDate = #'

Any other ideas/approaches are appreciated!
Oct 21 '10 #6
NeoPa
32,557 Recognized Expert Moderator MVP
Audrey,

This is a problem many have trouble with, and few understand fully. The hashes (#) are important, but not the only issue when dealing with date literals. See Literal DateTimes and Their Delimiters (#) for the full explanation.

To help with ypour specific problem (should you still need it after reviewing the article) it would help if you were to post the SQL itself, rather than the VBA which creates the SQL indirectly. To get this replace your lines #8 through #10 with :
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * " & _
  2.          "FROM [tblCapacity] " & _
  3.          "WHERE ([EffDate]=#" & Format(, "m/d/yyyy") & "#)"
  4. Debug.Print strSQL
  5. Set rs = db.OpenRecordset(strSQL)
and also add this line after #5 :
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
I assume your error is occurring at line #8. This is always well worth including in the description of your problem of course.
Oct 21 '10 #7
NeoPa - Thank you for taking the time to help me.

I did as you suggested, and now I get a compile error.

Here is the code as it is currently (with your changes):

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboEffDate_AfterUpdate()
  2.     ' Pull info out of SQL View.
  3.  
  4.     Dim db As Database
  5.     Dim rs As Recordset
  6.     Dim strSQL As String
  7.  
  8.     Set db = CurrentDb
  9.     strSQL = "SELECT * " & _
  10.              "FROM [tblCapacity] " & _
  11.              "WHERE ([EffDate]=#" & Format(, "m/d/yyyy") & "#)"
  12.     Debug.Print strSQL
  13.     Set rs = db.OpenRecordset(strSQL)
  14.     Me.txtFieldCap = rs![Field Inlet Capacity]
  15.     Me.txtT1Start = rs![T1 Start Date]
  16.     Me.txtT1End = rs![T1 End Date]
  17.     Me.txtT1Inlet = rs![T1 Inlet Gas]
  18.     Me.txtT1Residue = rs![T1 Residue Design]
  19.     Me.txtT1Fuel = rs![T1 Fuel]
  20. End Sub
The error is:
Compile Error: Argument not optional

The line:
Expand|Select|Wrap|Line Numbers
  1.    strSQL = "SELECT * " & _
  2. is highlighted in yellow. 
There is nothing in the "immediate" box when I do a control G either.
Oct 21 '10 #8
dsatino
393 Contributor
your format function is missing the item it's supposed to be formatting. You need to reference the date you want to format.
Oct 21 '10 #9
Ok that makes sense.

However, the date will be selected from a combo box that is already formated to the short date. Perhaps the point of NeoPa's code was to make sure it is in the proper form.

Not sure where to put the reference to the combo box though. After the formating code?
Oct 21 '10 #10
Tried to reference the cboEffDate, but still getting the compile error.
Oct 21 '10 #11
Ok, I think I am getting somewhere.

Here is the code as it is currently:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboEffDate_AfterUpdate()
  2.     ' Pull info out of SQL View.
  3.  
  4.     Dim db As Database
  5.     Dim rs As Recordset
  6.     Dim strSQL As String
  7.  
  8.     Set db = CurrentDb
  9.     strSQL = "SELECT * " & _
  10.              "FROM [tblCapacity] " & _
  11.              "WHERE ([EffDate]=#" & Format(Me.cboEffDate.Value, "m/d/yyyy") & "#)"
  12.     Debug.Print strSQL
  13.     Set rs = db.OpenRecordset(strSQL)
  14.     Me.txtFieldCap = rs![Field Inlet Capacity]
  15.     Me.txtT1Start = rs![T1 Start Date]
  16.     Me.txtT1End = rs![T1 End Date]
  17.     Me.txtT1Inlet = rs![T1 Inlet Gas]
  18.     Me.txtT1Residue = rs![T1 Residue Design]
  19.     Me.txtT1Fuel = rs![T1 Fuel]
  20. End Sub
What is being captured in the Immediate window is this:

SELECT * FROM [tblCapacity] WHERE ([EffDate]=##)
Oct 21 '10 #12
If I am reading this correctly (and I may not be) it appears that my code:

"WHERE ([EffDate]=#" & Format(Me.cboEffDate, "m/d/yyyy") & "#)"

is being interpreted as:
WHERE ([EffDate]=##)

Apparently, it is ignoring everything between the two hash marks. I don't understand why, however.
Oct 21 '10 #13
dsatino
393 Contributor
So it's not seeing a value in your field. Or maybe your field name is wrong. Or maybe you're misusing the Me keyword.

Type this into the immediate window:

?Form_YourFormName.cboEffDate.value

Obviously you need to change the "YourFormName" part.
Oct 21 '10 #14
dsatino
393 Contributor
Oh, and I think NeoPa's point of formatting your date despite it already being a date is because he's from the UK where date's are formatted d/m/yyyy and Access (being an American product) doesn't recognize this as a valid date. So if your date field was 21/10/2010, Access wouldn't see this as a valid date.
Oct 21 '10 #15
Ok, I did that and it returned "Null" - so I assume that means it is not seeing the value that I selected from the drop down list.

Since I am selecting a date from a combo box, does that mean the event is firing before my selection is recognized?

Should I move this to a different event, or did I miss a step?
Oct 21 '10 #16
Ok, I have resolved the issue and everything seems to be working correctly. Finally.

The Bound Column on the combo box was changed to "2" at some point (probably trying to resolve a different issue).

Changing it to 1 allows the code to run perfectly. I do not know why. If anyone has a short and sweet explanation they would like to share, that would be awesome. Fixing a problem is great. Understanding why the fixed worked...prevents more problems in the future!

Thank you both for your time and assistance (and patience)!

Here is what the code ended up
Oct 21 '10 #17
NeoPa
32,557 Recognized Expert Moderator MVP
DSatino:
Oh, and I think NeoPa's point of formatting your date despite it already being a date is because he's from the UK where date's are formatted d/m/yyyy and Access (being an American product) doesn't recognize this as a valid date. So if your date field was 21/10/2010, Access wouldn't see this as a valid date.
This is so missing the point. Unfortunately this is often misunderstood as I mentioned earlier. Certainly I am from the UK, but code should be portable even if you're from the USA. I repeat this over and over again but no-one seems to notice as the code seems to work when run in the USA.

Access is not USA specific, but SQL, as a standard, is. The SQL standard for dates (in both ANSI-89 & ANSI-92) is m/d/yyyy. It is probably not a coincidence that it matches the US format, but it is a SQL standard for the whole world. Therefore, code written using the local format is always incorrect. It may work in the US, but it's not good practice, and should be formatted specifically for SQL if it's to be reliable, portable code. Does that make sense?

Moving on. I left out the reference to the ComboBox in my earlier post by mistake. It should have read Format(Me.cboEffDate, "m/d/yyyy"). It doesn't need a .Value after it, as that is the default property and it certainly doesn't need to be a reference to the form's class (Form_blahblahblah). The Me. approach works properly and there's no need for such a clumsy fix.

Audrey, your last post indicates you intended to post your code, but it's not there. If you'd like to post it I will happily run my eyes over it for you. There are a number of items that can easily be wrong even though testing is unlikely to show it up. It's better to catch these now, than later on when those rare occurrences cause a problem.
Oct 22 '10 #18
NeoPa
32,557 Recognized Expert Moderator MVP
Audrey:
Changing it to 1 allows the code to run perfectly. I do not know why.
This indicates which column of the ComboBox is considered to reflect its .Value. As mentioned before, the .Value is the default property. If the Bound Column is set to 1 then I expect this is the column that contains the date. We have no knowledge of any other columns in your ComboBox, so we cannot say what is in column #2, but it is unlikely to be the date. It may even be without a second column, in which case the Null value would always be returned, and this would explain the empty string between the hashes (#) in your SQL string.
Oct 22 '10 #19
Oops. I must have gotten in a hurry. Here is what I finally ended up with (there are many more lines as I was just using a small set of fields to test with):
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboEffDate_AfterUpdate()
  2.     ' Pull info out of SQL View.
  3.  
  4.     Dim db As Database
  5.     Dim rs As Recordset
  6.     Dim strSQL As String
  7.  
  8.     Set db = CurrentDb
  9.     strSQL = "SELECT * " & _
  10.              "FROM [tblCapacity] " & _
  11.              "WHERE [EffDate]=#" & Me.cboEffDate.Value & "#"
  12.     Debug.Print strSQL
  13.     Set rs = db.OpenRecordset(strSQL)
  14.     Me.txtFieldCap = rs![Field Inlet Capacity]
  15.     Me.txtT1Start = rs![T1 Start Date]
  16.     Me.txtT1End = rs![T1 End Date]
  17.     Me.txtT1Inlet = rs![T1 Inlet Gas]
  18.     Me.txtT1Residue = rs![T1 Residue Design]
  19.     Me.txtT1Fuel = rs![T1 Fuel]
  20.     Me.txtT2Start = rs![T2 Start Date]
  21.     Me.txtT2End = rs![T2 End Date]
  22.     Me.txtT2Inlet = rs![T2 Inlet Gas]
  23.     Me.txtT2Residue = rs![T2 Residue Design]
  24.     Me.txtT2Fuel = rs![T2 Fuel]
  25.     Me.txtT3Start = rs![T3 Start Date]
  26.     Me.txtT3End = rs![T3 End Date]
  27.     Me.txtT3Inlet = rs![T3 Inlet Gas]
  28.     Me.txtT3Residue = rs![T3 Residue Design]
  29.     Me.txtT3Fuel = rs![T3 Fuel]
  30. End Sub
  31.  
Oct 22 '10 #20
NeoPa
32,557 Recognized Expert Moderator MVP
Thanks for posting Audrey.

For lines #8 through #11 I would reiterate that simply using the date value as returned, formatted to the local date format, is not a reliable or portable approach. It will work if the PC is set up for USA and the date format chosen is the standard one, but there are various situations where it may fail. Access is pretty clever at hiding such problems from you, and will determine what you mean with many dates even when incorrectly specified, but reliable it is not.

Line #12 proved very helpful in the debugging stage but is no longer required and can be removed without fear of causing any problems.

Good luck with your project.
Oct 22 '10 #21
Thanks again, NeoPa. I really appreciate the help.

I will fix the stuff you mentioned. Now that I understand what you are doing with the date format, I am going to use that everywhere. An ounce of prevention is worth a pound of cure!

Besides this database is for an international company. We are in the US but you never know when they may try to use the database somewhere else.
Oct 22 '10 #22
dsatino
393 Contributor
Audrey, let me start by saying. Always listen to NeoPa because he's always right.

Now let me get to where I was going with all of this, and I'm not the expert that NeoPa is, but what follows is based on experience.

The date format he gave you is the SQL standard as it applies to the whole world and Access will always recognize it in that format. But if you ever start using Access to to open SQL recordsets on other types of database servers, they may not recognize this format. (i.e. the backend database is not Access). For instance, my company has a Sybase server that can't interpret the 'm/d/yyyy' format. I have no idea why, but it can't.

As for the .Value reference, it's certainly not necessary because it's the default but I think if you're new to code it leads your thought process down a road that will cause you to wonder/explore other properties that are available for the control. You may not need them in this instance, but you'll know that there are other properties that can be modified/controlled programmatically.

As for the 'form_' class reference, I wasn't suggesting that you substitute it for the 'Me' keyword, rather you'd use it in the immediate window since the 'Me' keyword doesn't work in the immediate window. The immediate window is your friend.
Oct 22 '10 #23
NeoPa
32,557 Recognized Expert Moderator MVP
DSatino:
Always listen to NeoPa because he's always right.
This is going to sound strange, but I was very impressed by this post generally. OK The quoted bit may be a bit of a strectch, though I try to avoid mistakes where possible, but the rest of it is all sound. Good stuff.
Oct 22 '10 #24

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

Similar topics

29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
3
9513
by: Mark | last post by:
Hi - I can get this to work in SQL Server - but when also trying to make the application compatible with MS Access I get an error: Select tblfaqnetgroups.group_name from tblfaqnetroles Inner...
4
18029
by: dcarson | last post by:
I've read about this error in several other discussions, but still can't seem to pinpoint the problem with my code. Everything seemed to be working fine for some time, but it now tends to bomb out...
1
6846
by: Alan Murrell | last post by:
Hello, One of our web hosting clients is getting the following error when someone tried to log in form their login page: --- ODBC ERROR --- Microsoft OLE DB Provider for ODBC Drivers error...
3
5516
by: Lumpierbritches | last post by:
Syntax Error (missing operator) in query expression =BLANK'S MOLLIE-PRINCE BRUISER-3/14/2004-03 AnimalID is correct. I'm trying to open with a command button or double click the frmAnimal from a...
4
18198
by: khan | last post by:
getting, error, 3075 Syntax Error (Missing Operator) in query expression '8WHERE .=1' Dim TotalQty As Integer TotalQty = DLookup("", "", "=" & Forms!!)
3
4920
by: access baby | last post by:
I hava a date parameter filter query but it shows error Syntax error missing operator in query experssion can some one please help where am i going wrong in expression SELECT copyorderdtl * ...
4
2317
by: nerd4access | last post by:
Hello all! I am not new to access, but new to coding (and posting). I have a database that I have created and need some help with a login form. When a user opens the database, a form pops up...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
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
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
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
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
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...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.