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
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi, what exactly do you have in the "dtpFrom" and "dtpTo" text boxes?

What type of database are you trying to query?

If you output the contents of the sql variable to the immediate window with ? sql does it look as expected?
 
Upvote 0
Hi, what exactly do you have in the "dtpFrom" and "dtpTo" text boxes?

What type of database are you trying to query?

If you output the contents of the sql variable to the immediate window with ? sql does it look as expected?

Hi FormR, the dtpFrom and dtpTo is where the date is inputted to filter between dates in DATE BILLED field in the database
And I'm using mysql database
 
Upvote 0
Yes, but what is the actual content of those text boxes when the code errors?

I suspect that you need to bookend the dates in the where clause between single quotes - for example:

Rich (BB 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 "

But you might also need to change their format.
 
Upvote 0
A gentle reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Yes, but what is the actual content of those text boxes when the code errors?

I suspect that you need to bookend the dates in the where clause between single quotes - for example:

Rich (BB 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 "

But you might also need to change their format.

Yes, I tried to put a single quotes on it and No error comes up but it displays no data with the date range of dtpFrom and dtpTo. Anyway, this is the data of DATE BILLED look likes.

1.JPG
 
Upvote 0
Yes, the actual content that is in the text box - what is it exactly?
 
Upvote 0

Forum statistics

Threads
1,215,474
Messages
6,125,024
Members
449,204
Latest member
LKN2GO

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