How to use VBA to change a date in SQL query in excel - sql

I need help if it is possible where I can upload SQL query in excel via Get Data>From Database>From SQL Server Database, and be able to change the date in this uploaded SQL query.
My hope is to use three cells within tab where i enter a date, and either use a vba or SQL looks cell value automatically and provide results when i refresh SQL.
Right now i have these sets of dates.
set #eomdate = '2022-10-31'
set #startdate = '2016-01-01'
set #enddate = '2022-10-31'
I want to use cells in excel that lookup date entered in cell A1, B1, and C1. My goal is to change the dates to get results based on dates entered.
set #eomdate = Sheet1A1
set #startdate = Sheet1B1
set #enddate = Sheet1C1
is there a way to use a VBA code that will change the dates based on cells mentioned above? I am researching but have been unlucky.
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = Worksheets("Query1")
`ActiveWorkbook.RefreshAll`
End Sub
Any help is greatly appreciated.
Thank you,
Kosta
This is the first time I am trying to do this but I am hoping that when I enter a date in cell A1, and i run a vba CommandButton1, the SQL that was uploaded in excel, will change the date prior running the SQL query.

Related

MS Access date range search leaving out records that should be within date range

I have a lookup form with text boxes for commodity, start date and end date
The commodity section has criteria here
`[Forms]![Traceability_F]![Material]`
also an OR criteria for if the text box is blank
`[Forms]![Traceability_F]![Material] is null`
The date range criteria is
`Between [Forms]![Traceability_F]![StartDate] And [Forms]![Traceability_F]![EndDate]`
also an OR criteria for if date range is blank
`([Inventory Usage].[Date Used]) Between [Forms]![Traceability_F]![StartDate] And [Forms]![Traceability_F]![EndDate]`
When the search button is clicked and nothing is entered into the text boxes the query opens with all records. This is what I wanted
When the search button is clicked and nothing is in the commodity text box the query shows up blank. Not what I wanted. I was trying to get it to show all records for that date range
When commodity and date range is inputted the query shows the commodity but the dates of the commodity are sometimes not in the date range or the query omits records that should be within the date range. For example the table that is being pulled from has records from 9/26/22-10/14/22 when those dates are inputted a few records in that range do not show up. Records from 10/11,10/13 do not show up.
Clearly something is wrong with the date range function of my form. I would like to be able to enter a date range and see all records entered in that date range. Also when a commodity is entered along with the date range I want to be able to see all records of that specific commodity in that date range.
The commodity section has criteria shown in image 3. Having trouble entering it in this section.
Any help would be greatly appreciated. I have attached an image of my form and can add in the SQL information but I am new to SQL.
Thanks,
Kyle
[search form][1]
[SQL for Query][2]
[Query Design][3]
[1]: https://i.stack.imgur.com/4WdsY.png
[2]: https://i.stack.imgur.com/a82rx.png
[3]: https://i.stack.imgur.com/P6Bkg.png
Try to declare the form fields as DateTime in the query SQL:
Parameters
[Forms]![Traceability_F]![StartDate] DateTime,
[Forms]![Traceability_F]![EndDate] DateTime;
Select ... <snip>

Pass dates from an Excel cell reference into ODBC SQL PowerQuery

I have several MySQL queries, including some that aggregate but don't necessarily return dates in the results, that have either a date or date range hardcoded into the query. I've loaded these into separate Excel worksheets through ODBC to output the results. Instead of going into the query and editing the source as needed to change the dates, how can I have the 'start' or 'start and end' date(s) of the query be a cell reference on that particular worksheet?
The date part of my SQL predicates look like:
WHERE post.trans_date >= '2020-03-01'
or
WHERE post.trans_date >= '2020-03-01'
AND post.trans_date <= '2020-03-31'
I'd like to just be able to change the dates directly in the cell(s), without VBA, and click Refresh to return the correct results. Thanks!
You can read cell values into PowerQuery and then use those cell values within your query similarly to the example I give in the linked post.

Wrong data types when loading sql query with vba in excel

I have a SQL Query, which I want to use in VBA to load the data into Excel, the problem is that when the load is done the data in Excel is with the wrong type. For example, where it needs to be a date, it remains an integer, where an integer is a date. Is there any way to fix this?
Example my query:
sql = " SELECT DATA, ITEM, COMPANY, SALES FROM SALES "
You will have to format cells as per date format. Also ensure that values are correct in your query output.
Sheet1.Range("A2", "A100").NumberFormat = "yyyy-mm-dd"

Copy record in Excel up to specific date

I'm trying to copy a unique row (Row with PUMP for example), "x" amount of times. Eventually, the interval should be based on the column frequency (Frequentie). A record should be copied until the mentioned date, for example, when 01-01-2023 has been reached. This firstly should be completed for the first row and when that has been accomplished it needs to be done for the second row. This to create a dataset which can be used in Power BI. Can anybody help me with this problem?
In the image attached is an example given of the current data set.
Many thanks in advance!
It is always a good idea to show what have you tried so far, because the way the question is asked it looks like you are asking for the whole solution, without any input from your site. And this is a bit against the rules of SO. However, this is something to get you started:
Read the dates from the excel table. Like firstDate = Range("L2")
Locate the max and the min dates. They will be firstDate and lastDate.
Read about loops and copying of Excel range.
Try to do a loop between firstDate and lastDate and copy range valeus in the loop.
This is some loop example, writing dates between two dates on a new row:
Public Sub TestMe()
Dim firstDate As Date
Dim lastDate As Date
Dim cnt As Long
firstDate = DateSerial(2010, 12, 1)
lastDate = DateSerial(2011, 12, 1)
For cnt = 1 To lastDate - firstDate
Cells(cnt, 1) = CDate(firstDate + cnt)
Next cnt
End Sub

Loop that changes a date, copy & pastes resulting data, then continues these processes

I have a workbook with 2 sheets. Sheet 1 has a column with a series of dates that are the same day in a sequence of years (for example 5/1/1986, 5/1/1987, 5/1/1988, ... 5/1/2017).
When the first date at the top of the column is changed (for example from 5/1/1986 to 5/2/1986), all of the other years below will update to that given day and a series of data will be produced in two separate columns through a calculation that is depends on the day as a variable.
The 2nd worksheet has two tables where I need the data resulting from each day between 5/1 and 8/28 for 1986 - 2017 to be pasted from the 1st worksheet after the date is changed (5/1 to 5/2 to 5/3) and so on. The tricky part is the copy and paste needs to happen each time before the date is changed in order to record all of the subsequent data.
I know that I will likely need to use a DateAdd function to loop the addition of one day to the starting date at the top of the column, and I know that I will probably need to utilize a Do Until function to have the sequence stopped when the final date of 8/28 is input. I have been tweaking around the code but I cannot get the formatting correct.
To clarify I need a loop that will do two processes, add one day to 5/1/1986 (until 8/28) and copy the data that each change of day yields and paste it into a 2nd worksheet, before the date is changed again and new data appears. Thanks.
Below is the code I wrote that successfully sets up the first step of the process, setting the start date to 5/1/1986 and copying and pasting the data into the tables in sheet2.
'StartDate is input as 5/1/1986 and plugged into first cell of data range, DK7'
Dim StartDate As String
StartDate = Range("DO50").Value
Range("DK7").Value = StartDate
'EndDate is input as 8/28/1986 and used as reference to end sequence of Copy & Pasting'
Dim EndDate As String
EndDate = Range("DO51").Value
'Corn Z-Score results for StartDate are copied and pasted into Sheet2'
Range("DS7:DS38").Copy
Worksheets(2).Range("D4:D36").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Soy Z-Score results for StartDate are copied and pasted into Sheet2'
Range("DT7:DT38").Copy
Worksheets(2).Range("D41:D73").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False