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
 
Try like this:

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

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try like this:

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

Still there is no data found.
 
Upvote 0
If you wrote the query outside of VBA - what would it look like? Can you post it here?
 
Upvote 0
Your trying to dynamically create an SQL statement using VBA - what should that final SQL statement look like?
 
Upvote 0
Your trying to dynamically create an SQL statement using VBA - what should that final SQL statement look like?

This is my code including the SQL statement that you give me.
 
Last edited:
Upvote 0
I think you are missing my point - or perhaps you don't know the answer.

So, back to guessing..

Is [SYS Payment Rcvd] really blank - or does it actually contain NULL's?
 
Upvote 0
I think you are missing my point - or perhaps you don't know the answer.

So, back to guessing..

Is [SYS Payment Rcvd] really blank - or does it actually contain NULL's?

Sorry. Yes it is, blank or null. The purpose of this line
[SYS Payment Rcvd]=''

is to search in the database that no payment received yet.
 
Upvote 0
blank or null

They are not quite the same thing though.

What happens with:
VBA Code:
Sql = "select [Row Number],[ITEM],[NAME],[SO #],[DATE BILLED] from tblData  " & _
  " where [DATE BILLED] BETWEEN '" & Format(CDate(dtpFrom.Text), "yyyy-mm-dd") & "' AND '" & Format(CDate(dtpTo.Text), "yyyy-mm-dd") & "'  " & _
  " AND [SYS Payment Rcvd] is null order by [Row Number] DESC "
 
Upvote 0
They are not quite the same thing though.

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

Still no results found. I tried different formats in the textbox just to match but still not results found.
 
Upvote 0

Forum statistics

Threads
1,216,095
Messages
6,128,794
Members
449,468
Latest member
AGreen17

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