473,320 Members | 2,124 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.

vb.net SQL help needed

I am trying to run this query...
Expand|Select|Wrap|Line Numbers
  1. strSql = "SELECT * FROM tblMain WHERE (((tblMain.Verifier_ID)='" & lstVerifier.SelectedItem & "') AND ((tblMain.Week_Ending_Date)='" & lstWeekEndingDate.SelectedItem & "'));"
...but I get an error 'Data type mismatch in expression criteria'. The values are a name (text format) and a date (date/time format). How do I modify the query to pull correctly?? thanks!!
Feb 7 '08 #1
4 1178
DrBunchman
979 Expert 512MB
The problem is the format of the date you are supplying to the query string. SQL Server likes dates in the format YYYY-MM-DD so you need a function to change it to that format before you use it in your query.

Something like:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Function FormatDate(ByVal DateIn As DateTime) As String
  3.     Dim sDate As String = (DateIn.Year & "-" & DateIn.Month & "-" & DateIn.Day).ToString
  4.     Return sDate
  5. End Function
  6.  
which you can call in your code like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. strSql = "SELECT * FROM tblMain WHERE (((tblMain.Verifier_ID)='" & lstVerifier.SelectedItem & "') AND ((tblMain.Week_Ending_Date)='" & FormatDate(lstWeekEndingDate.SelectedItem) & "'));"
  3.  
Let me know if this helps,

Dr B
Feb 7 '08 #2
The problem is the format of the date you are supplying to the query string. SQL Server likes dates in the format YYYY-MM-DD so you need a function to change it to that format before you use it in your query.

Something like:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Function FormatDate(ByVal DateIn As DateTime) As String
  3.     Dim sDate As String = (DateIn.Year & "-" & DateIn.Month & "-" & DateIn.Day).ToString
  4.     Return sDate
  5. End Function
  6.  
which you can call in your code like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. strSql = "SELECT * FROM tblMain WHERE (((tblMain.Verifier_ID)='" & lstVerifier.SelectedItem & "') AND ((tblMain.Week_Ending_Date)='" & FormatDate(lstWeekEndingDate.SelectedItem) & "'));"
  3.  
Let me know if this helps,

Dr B
Does it matter that I am using Access 2003 as the database?
Feb 7 '08 #3
DrBunchman
979 Expert 512MB
[quote=juster21]Does it matter that I am using Access 2003 as the database?[/QUOTE

It requires only a small change. Instead of delimiting the date with apostrophe's you need to use hash symbols.

#YYYY-MM-DD# Instead of 'YYYY-MM-DD'. So your sql statement would be:

Expand|Select|Wrap|Line Numbers
  1. strSql = "SELECT * FROM tblMain WHERE (((tblMain.Verifier_ID)='" & lstVerifier.SelectedItem & "') AND ((tblMain.Week_Ending_Date)=#" & FormatDate(lstWeekEndingDate.SelectedItem) & "#));"
Let me know if this works for you.

Cheers,

Dr B
Feb 8 '08 #4
It requires only a small change. Instead of delimiting the date with apostrophe's you need to use hash symbols.

#YYYY-MM-DD# Instead of 'YYYY-MM-DD'. So your sql statement would be:

Expand|Select|Wrap|Line Numbers
  1. strSql = "SELECT * FROM tblMain WHERE (((tblMain.Verifier_ID)='" & lstVerifier.SelectedItem & "') AND ((tblMain.Week_Ending_Date)=#" & FormatDate(lstWeekEndingDate.SelectedItem) & "#));"
Let me know if this works for you.

Cheers,

Dr B
wonderful!! thank you!!
Feb 8 '08 #5

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

Similar topics

0
by: System | last post by:
Hello All, Redhat 9.0 Mysql 3.23.56 ==> Running I want to upgarde to 4.0.13 but this is the error it says: # rpm -Uvh MySQL-server-4.0.13-0.i386.rpm warning: MySQL-server-4.0.13-0.i386.rpm: V3...
8
by: Stephen | last post by:
I am trying to add some code to below to include a datatable and fill the datatable. The reason for doing this is so as I can check to see whether there are any rows returned by the stored...
13
by: Joe Feldman | last post by:
This position is located in the South Bay Area in Northern California. If you are interested please send me your resume in a word .doc so that I can review it. If this does not look like a match,...
0
by: Cindy B | last post by:
Please send your resume and position to Cindy@AtlanticResource.com! I CAN NOT accept candidates that ARE OUTSIDE OF THE US! NO PHONE CALLS PLEASE! Email your resume to me! Position:SQL...
3
by: Wade | last post by:
I would like to install the .Net 1.1 framework on a Web Server running W2K to be able to run ASP.NET files, but I'm not sure where to find the files I need for the .Net framework. I have ".NET...
17
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce...
5
by: Steve | last post by:
Hi, I am sitting down to design our next set of internal apps. I would like to approach this in a way that would allow me to break logical parts of the application that handle specific tasks...
0
by: ultradiv | last post by:
I have a VB.NET application partly built that produces an xml output (just a file at present) I have a .NET webserver and SQLserver 2000 I need to be able to send the xml to the webserver/database...
28
by: Ian Davies | last post by:
Hello I would appreciate some help from someone who has knowledge of working with css, php javascript and how they interact. Ive been working on a task for the last few days and have started to...
37
by: C_guy | last post by:
Does anyone know of a (hopefully free) tool that can traverse a project and determine which "#include"s are not needed or needed in every .C file? This would be helpful in removing header...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.