Convertion failed when converting DateTime to character stringto string not converting

jwp

New Member
Joined
Aug 31, 2011
Messages
33
Here is my simple SQL query in Excel 2003

Sub Date_Range()


Dim Start_date As String, strH As String


Start_date = Application.InputBox(Prompt:="Start date?", Type:=2)



strH = "Select MATTER_NO, DETAIL1, DATE_OPENED" _

& " From Matter Where DATE_OPENED >= 'Start_date' " _
& " Group by MATTER_NO, DETAIL1, DATE_OPENED Order by DATE_OPENED ASC"

With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Worksheets("Sheet5").Range("A2"), Sql:=strH)
.Refresh
End With
'
End Sub

Can someone tell me how to overcome this error when I am writing sql queries with VB in Excel.

Thanks
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
welcome to the board. Please can you be more specific when explaining your problem, e.g. what type or error are you getting?

However, in this case, I note you declare the variable Start_date, but then use it as 'Start_date' within a text string. Try changing your code to the following:

strH = "Select MATTER_NO, DETAIL1, DATE_OPENED" _
& " From Matter Where DATE_OPENED >= " & Start_date & _
"
Group by MATTER_NO, DETAIL1, DATE_OPENED Order by DATE_OPENED ASC"

 
Upvote 0
Thank you for the replies I found the problem the extra ' around the input parameter and declaring it as a string has solved my problem.

What a relief now back to actually generating these reports thanks guys! :biggrin:



strH = "Select MATTER_NO, DETAIL1, DATE_OPENED" _
& " From Matter Where DATE_OPENED >= ' " & Start_date & _
" '
Group by MATTER_NO, DETAIL1, DATE_OPENED Order by DATE_OPENED ASC"
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,284
Members
452,902
Latest member
Knuddeluff

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