Forum Discussion
Add inputbox value into a where clause in odbc.query
- Apr 23, 2023
Hi dlc04
Little add. to NikolinoDE
Assuming the value in B2 is like dd/mm/yyyy your selectedDate step returns a DateTime value (i.e. 01/01/2023 00:00:00). If SnowFlake expects a Date value (i.e. 01/01/2023) only you'll have to convert the DateTime value to a Date value before converting it to a Text value. Can be done as follow:
selectedDate = Text.From( DateTime.Date( Table.FirstValue(Excel.CurrentWorkbook(){[Name="mydate"]}[Content]) ) ),
Hi dlc04
Little add. to NikolinoDE
Assuming the value in B2 is like dd/mm/yyyy your selectedDate step returns a DateTime value (i.e. 01/01/2023 00:00:00). If SnowFlake expects a Date value (i.e. 01/01/2023) only you'll have to convert the DateTime value to a Date value before converting it to a Text value. Can be done as follow:
selectedDate = Text.From(
DateTime.Date(
Table.FirstValue(Excel.CurrentWorkbook(){[Name="mydate"]}[Content])
)
),
- dlc04Apr 26, 2023Copper Contributor
Thank you both @NikolinoDE and Lorenzo
Once I changed the format to text, in the Power Query Editor I'm getting the following:
DataSource.Error: ODBC: ERROR [22007] Date '4/26/2023 12:00:00 AM' is not recognized
Details:
DataSourceKind=Odbc
DataSourcePath=dsn=Snowflake_Source
OdbcErrors=[Table]So I tried the DateTime option since in Snowflake that is a date data type and I changed the B3 cell format to Date (MM/DD/YYYY), and it worked when I refreshed it in the Editor, but when I closed the Excel file and reopen it again, the inputbox popped out, inserted the date, and the file freezes completely and do not run.
Could it be something in the vb code about the refresh that is causing some trouble?
Here is what I have in the vb code for the inputbox and the refresh.
Private Sub Workbook_Open() 'PURPOSE: Refresh All function when file is opened 'InputBox Range("A2:AL500").ClearContents inputvalue = InputBox("Enter date(MM/DD/YYYY):") Range("B3").Value = inputvalue 'Refresh ThisWorkbook.RefreshAll Application.CalculateUntilAsyncQueriesDone MsgBox "Data has been refreshed!" End Sub
Thanks again!
- dlc04Apr 27, 2023Copper Contributor
Found a way to do it. I was using the refreshAll along with the AsyncQueriesDone
ThisWorkbook.RefreshAll Application.CalculateUntilAsyncQueriesDone
which I'm guessing it was running the query while it was trying to update such query at the same time.
So I used
ThisWorkbook.Connections("Query - Query1").Refresh
And in the query properties I unchecked the option "Enable background refresh".
- LorenzoApr 27, 2023Silver Contributor
Hi dlc04
VBA isn't my cup to tea and the doc. of Application.CalculateUntilAsyncQueriesDone didn't help me understand what this method actually does. I was going to suggest you try disabling Background Refresh though...
Glad you found a suitable solution & Thanks for documenting it, hopefully this will help sooner or later
FYI an old case: Application.CalculateUntilAsyncQueriesDone Causes Excel to freeze and crash