VBA sql conversion failed when converting the varchar value to data type int in FILTER BETWEEN dates

geloader0

Board Regular
Joined
Dec 21, 2022
Messages
52
Office Version
  1. 2016
Platform
  1. Windows
Hi Good day, I have a form with data and search box. I want to filter dates between 2 fields/textbox From and To. But this error appear as shown in the image below.
This is my SQL query . Thank you for the help.


VBA Code:
sql = "select [Row Number],[ITEM],[NAME],[SO #],[DATE BILLED] from tblData  " & _
  " where [DATE BILLED] BETWEEN " & dtpFrom.Text & " AND " & dtpTo.Text & "  " & _
  " AND [SYS Payment Rcvd]='' order by [Row Number] DESC "





1.JPG
 
But when I use LIKE query it works using this line
Excel Formula:
[SYS Payment Rcvd]=''
. But in the dates filter using between the 2 dates in the textbox. Results not found
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Here's one more effort then - I'm running out of ideas though.

VBA Code:
sql = "select [Row Number],[ITEM],[NAME],[SO #],[DATE BILLED] from tblData  " & _
  " where CAST([DATE BILLED] AS DATE) BETWEEN '" & Format(CDate(dtpFrom.Text), "yyyy-mm-dd") & "' AND '" & Format(CDate(dtpTo.Text), "yyyy-mm-dd") & "'  " & _
  " AND ISNULL([SYS Payment Rcvd],'') = '' order by [Row Number] DESC "
 
Upvote 0
Solution
Here's one more effort then - I'm running out of ideas though.

VBA Code:
sql = "select [Row Number],[ITEM],[NAME],[SO #],[DATE BILLED] from tblData  " & _
  " where CAST([DATE BILLED] AS DATE) BETWEEN '" & Format(CDate(dtpFrom.Text), "yyyy-mm-dd") & "' AND '" & Format(CDate(dtpTo.Text), "yyyy-mm-dd") & "'  " & _
  " AND ISNULL([SYS Payment Rcvd],'') = '' order by [Row Number] DESC "

Now, This error appears.

1.JPG
 
Upvote 0
This whole problem is nothing to do with VBA or Excel and it's difficult to help without knowing anything about your database/table set-up/data.

You really need to get a working SQL statement from someone who knows SQL and knows your database - then me (or someone else here) can then help you to create it in VBA.

Last try..
VBA Code:
sql = "select [Row Number],[ITEM],[NAME],[SO #],[DATE BILLED] from tblData  " & _
  " where str_to_date([DATE BILLED] AS DATE, '%m/%d/%y') BETWEEN " & Format(CDate(dtpFrom.Text), "yyyy-mm-dd") & "' AND '" & Format(CDate(dtpTo.Text), "yyyy-mm-dd") & "'  " & _
  " AND ISNULL([SYS Payment Rcvd],'') = '' order by [Row Number] DESC "
 
Upvote 0
This whole problem is nothing to do with VBA or Excel and it's difficult to help without knowing anything about your database/table set-up/data.

You really need to get a working SQL statement from someone who knows SQL and knows your database - then me (or someone else here) can then help you to create it in VBA.

Last try..
VBA Code:
sql = "select [Row Number],[ITEM],[NAME],[SO #],[DATE BILLED] from tblData  " & _
  " where str_to_date([DATE BILLED] AS DATE, '%m/%d/%y') BETWEEN " & Format(CDate(dtpFrom.Text), "yyyy-mm-dd") & "' AND '" & Format(CDate(dtpTo.Text), "yyyy-mm-dd") & "'  " & _
  " AND ISNULL([SYS Payment Rcvd],'') = '' order by [Row Number] DESC "


Anyway, The format of DATE BILLED in the table is VARCHAR not a date.
 
Upvote 0
The format of DATE BILLED in the table is VARCHAR not a date.

Yes, so maybe you've got entries in there that can't be converted to a date! The last post# should ignore those - but I don't have access to a mysql database to be able to do any testing.
 
Upvote 0
Yes, so maybe you've got entries in there that can't be converted to a date! The last post# should ignore those - but I don't have access to a mysql database to be able to do any testing.

Anyway in your SQL statement gives me this error.

1675267833952.png
 
Upvote 0
My mistake (like I say, I have no way of testing what I'm posting). .

VBA Code:
sql = "select [Row Number],[ITEM],[NAME],[SO #],[DATE BILLED] from tblData  " & _
  " where str_to_date([DATE BILLED], '%m/%d/%y') BETWEEN " & Format(CDate(dtpFrom.Text), "yyyy-mm-dd") & "' AND '" & Format(CDate(dtpTo.Text), "yyyy-mm-dd") & "'  " & _
  " AND ISNULL([SYS Payment Rcvd],'') = '' order by [Row Number] DESC "
 
Upvote 0
Yes, so maybe you've got entries in there that can't be converted to a date! The last post# should ignore those - but I don't have access to a mysql database to be able to do any testing.

Yeah. I thought the CAST thing is the solution in order to convert it to a date. But nothing works. Maybe you're right. That there is a record that cannot be converted into a date. there is 58,000 rows and it is hard to find that record.
 
Upvote 0
I think the DATE BILLED field in the database is the problem because when I tried to search other DATE field that has a DATE format not a VARCHAR... The search works. So, in order to achieve problem this I need to convert DATE BILLED field to a DATE from the SQL statement
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top