Trying to automatically run an access query in excel and update the date criteria based on user input. Not sure how to code in vba a parameter for entering the between date and getting the query to post automatically in excel under a header column in a2.
Access Query:
'''SELECT hosp_chg.HOSP_ACCT_ID, hosp_chg.SVC_DATE, hosp_chg.POST_DATE, [hosp_chg.POST_DATE]-
[hosp_chg.SVC_DATE] AS [Lag Days], hosp_chg.ACCT_CLASS_ID, acct_class_master.TITLE, "" AS
[Inpatient or Outpatient], hosp_chg.REVENUE_CODE_ID, hosp_chg.COST_CENTER_ID,
cost_center_master.COST_CENTER_CODE, cost_center_master.COST_CENTER_NAME, "" AS [WDY Cost
Center Mapping], hosp_chg.DEPT_ID, department_master.DEPT_NAME, hosp_chg.PROC_ID,
procedure_master.PROC_CODE, procedure_master.PROC_NAME, hosp_chg.ORIG_AMT, hosp_chg.QUANTITY,
hosp_chg.LATE_CHG_YN, user_master.USER_ID, user_master.USER_NAME,
user_master.DEFAULT_USERROLE, "" AS [Include/Exclude in Reporting],
hosp_chg.REVERSE_ORIG_TRAN_ID, hosp_chg.REPOST_ORIG_TRAN_ID,
hosp_chg.LATE_CHG_CRED_ORIG_TRAN_ID, hosp_chg.LATE_CHG_CORRECT_ORIG_TRAN_ID
FROM acct_class_master INNER JOIN ((((hosp_chg INNER JOIN cost_center_master ON
hosp_chg.COST_CENTER_ID = cost_center_master.COST_CENTER_ID) INNER JOIN department_master ON
hosp_chg.DEPT_ID = department_master.DEPT_ID) INNER JOIN procedure_master ON hosp_chg.PROC_ID
= procedure_master.PROC_ID) INNER JOIN user_master ON hosp_chg.USER_ID = user_master.USER_ID)
ON acct_class_master.ID = hosp_chg.ACCT_CLASS_ID
GROUP BY hosp_chg.HOSP_ACCT_ID, hosp_chg.SVC_DATE, hosp_chg.POST_DATE, [hosp_chg.POST_DATE]-
[hosp_chg.SVC_DATE], hosp_chg.ACCT_CLASS_ID, acct_class_master.TITLE,
hosp_chg.REVENUE_CODE_ID, hosp_chg.COST_CENTER_ID, cost_center_master.COST_CENTER_CODE,
cost_center_master.COST_CENTER_NAME, hosp_chg.DEPT_ID, department_master.DEPT_NAME,
hosp_chg.PROC_ID, procedure_master.PROC_CODE, procedure_master.PROC_NAME, hosp_chg.ORIG_AMT,
hosp_chg.QUANTITY, hosp_chg.LATE_CHG_YN, user_master.USER_ID, user_master.USER_NAME,
user_master.DEFAULT_USERROLE, "", hosp_chg.REVERSE_ORIG_TRAN_ID, hosp_chg.REPOST_ORIG_TRAN_ID,
hosp_chg.LATE_CHG_CRED_ORIG_TRAN_ID, hosp_chg.LATE_CHG_CORRECT_ORIG_TRAN_ID, ""
HAVING (((hosp_chg.POST_DATE) Between [Enter the beginning date MM/DD/YY] And [Enter the
ending date MM/DD/YY]) AND ((hosp_chg.ORIG_AMT)>0) AND ((hosp_chg.LATE_CHG_YN)="Y") AND
((hosp_chg.REVERSE_ORIG_TRAN_ID) Is Null) AND ((hosp_chg.REPOST_ORIG_TRAN_ID) Is Null) AND
((hosp_chg.LATE_CHG_CRED_ORIG_TRAN_ID) Is Null) AND ((hosp_chg.LATE_CHG_CORRECT_ORIG_TRAN_ID)
Is Null))
ORDER BY hosp_chg.REVENUE_CODE_ID;'''
'
My error is on the parameters but i do not know how to fix any help would be greatly appreciated. THANKS IN ADVANCE!!
Ricardo A has given you a good answer. Here's the VBA code to do what he suggests:
Have the user enter the beginning and ending dates in two cells of the workbook, perhaps cells X1 and Y1
Use the data from those cells to put the values in the query as follows (assuming your query is a variable named SQL):
SQL = Replace(SQL, "[Enter the beginning date MM/DD/YY]", "#" & range("X1").text & "#")
SQL = Replace(SQL, "[Enter the ending date MM/DD/YY]", "#" & range("Y1").text & "#")
That will put the values into your query as date literals.
Related
Lets suppose, vba code has
Dim SQL as String
SQL ="select * from table"
I found function that helps to combine values from related rows into a single concatenated string value.But when i implemented to my code it didn't work because double quotation inside query creating errors.What could be best format to make syntax valid in vba.The function is given below and link is (Combine values from related rows into a single concatenated string value).
SELECT
i.N_ID,
i.F_Name,
i.L_Name,
ConcatRelated(
"Course_ID",
"tbl_Courses",
"N_ID = '" & [N_ID] & "'"
) AS Course_IDs
FROM tbl_Instructors AS i;
Most likely, your ID is numeric, not text, thus no single-quotes:
SELECT
i.N_ID,
i.F_Name,
i.L_Name,
ConcatRelated(
"Course_ID",
"tbl_Courses",
"N_ID = " & [N_ID] & ""
) AS Course_IDs
FROM tbl_Instructors AS i;
I have a workbook where I fetch data from SQL Server using fixed parameter values for a SQL query.
I want to make another sheet and have the parameter for the SQL query be taken from the cell values.
I didn't find anything on this regard.
Also I would like to refresh the data as soon as the cell values changes in the other sheet.
For this to work, you need to set up three different parts:
1) A parameter table in an Excel sheet
2) Changes to the advanced editor in PowerQuery
3) A macro to refresh the PQ when any cells in the parameter table are changed
1) Excel Table
You can see I included a column called param which can hold a Parameter name to help keep straight which parameter is which.
2) PQ Advanced Editor
let
ParamTable = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Param = "'" & Text.From(ParamTable[value]{0}) & "'",
Source = Sql.Database("IP Address", "Database Name", [Query="Select * from weeks#(lf)where date >= '2018-01-01' and date < " &Param])
in
Source
Equivalent alternative: (Difference in location of variable used in SQL query.)
let
ParamTable = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Param = "'" & Text.From(ParamTable[value]{0}) & "'",
Source = Sql.Database("IP Address", "Database Name", [Query="Select * from weeks#(lf)where date < " &Param & " and date >= '2018-01-01'"])
in
Source
Alternative Variable Type: (If dealing with numbers, the string markers ' aren't required)
let
ParamTable = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Param = Text.From(ParamTable[value]{0}),
Source = Sql.Database("IP Address", "Database Name", [Query="Select * from weeks#(lf)where cnt < " &Param & " and date >= '2018-01-01'"])
in
Source
Explanation:
After pulling the Parameter Table into the PQ Query (ParamTable = Excel.CurrentWorkbook(){[Name="Table1"]}[Content]), the columns can be accessed by column name [value] and the rows by a zero-index number {0}. Since I was pulling in a date-value. I needed to convert it to a string value I could insert into the SQL Query -- thus the Text.From() and the appended ''s to the ends (SQL marks strings with single ' rather than the double ")
Since I named the variable Param, to use it in the string, I substituted &Param for the value which had originally been there.
2.1 Power Query's Value.NativeQuery
let
ParamTable = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Param = ParamTable[value]{0},
Source = Value.NativeQuery(Sql.Database("IP Address", "Database Name"), "Select * from weeks where date < #dateSel and date >= '2018-01-01'",[dateSel = Param])
in
Source
Alternative Formatting:
let
ParamTable = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Param = ParamTable[value]{0},
Source = Sql.Database("IP Address", "Database Name"),
Data = Value.NativeQuery(Source, "
Select * from weeks
where date < #dateSel and date >= '2018-01-01'
",[dateSel = Param])
in
Source
Notes:
When using Value.NativeQuery(), you can pass a date or datetime value directly in as a variable without having to include the single apostrophes.
Sometimes splitting the data retrieval into a Source step and a NativeQuery step can help with PQ's sporadic firewall issues.
3) Macro
This works for a simple check if anything in the table has changed, then runs the refresh. You will need to make sure this is placed in the correct module. Items you will need to change are:
Sheet1 is the codename of the worksheet with the parameter table.
"Table" is the name of the Parameter table
"Query - Query1" is the name of the connection to be refreshed
Note: Query1 is the name of the query. Default names for connections are usually Query - & query name
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Sheet1.ListObjects("Table1").DataBodyRange) Is Nothing Then
ThisWorkbook.Connections("Query - Query1").Refresh
End If
End Sub
I'm trying to read excel data into datatable object.But, some columns in excel is has corrupted data specifically of type DATE .
I'm using VB code to read the data with
OLEDB Connection
dtData = objExcelReader.ReadWorksheet(strFilePath.ToString, "select * from sheet1$", True)
With this code I get 'Not a legal OleAut date' error.
With ExcelReader,
objExcelReader = New ExcelIO.ExcelReader
objExcelReader.UseHeaders = True
objExcelReader.AllowSpacesInColumnName = blnAllowSpacesInColumnName
objExcelReader.ReadFormatting = True
objExcelReader.TrimStrings = True
objExcelReader.ClearColumnDatatypes()
objExcelReader.DefaultStartRow = intStartRow
dtData = objExcelReader.Read(strFilePath.ToString, thisApplication, strInputSheetName)
I get exception - 'Out of present range. (Exception from HRESULT: 0x8002000A (DISP_E_OVERFLOW))'
Using range in query, I'm able to fetch records. I need to include multiple range in that case.
dtData = objExcelReader.ReadWorksheet(strFilePath.ToString, "select * from sheet1$O:AK", True)
Can someone help me to include multiple range here like A:K & O:AK
Or to fix any of above mentioned issues and ignore those exceptional column.
Try not to put column containing dates in the first column of table.
Sort data in that way that
data column has data (with proper data format) in the fist row.
See more on corrupt data formats here:
https://dutchgemini.wordpress.com/2011/04/21/two-serious-flaws-with-excels-copyfromrecordset-method/
Check this out how I have overcome corrupt formatting in open recordset.
https://stackoverflow.com/a/33807584/1903793
Good Afternoon,
I have an access query that contains a list of all my customers lets call that CUS
I have another query that has a list of ORDERS
I would like to write some VBS that cycles through the customer list and exports a csv file containing all orders that belong to that customer.
The vba would then move on to the next customer on the list and perform the same action.
Any help would be great.
Snippet of code below
almost there cant get the WHERE condition working it keeps displaying a popup for me to populate however the same string is feeding the msgbox fine here is a snippet below tht is within the loop
strcustcode = rs!OCUSTCODE
ordercount = rs!orders
TIMEFILE = Format$(Time, "HHMM")
MsgBox ([strcustcode] & " has " & [ordercount] & " orders")
StrSQL = "Select * From [24-ND_Cus] where [24-ND_Cus].[OCUSTCODE] = strcustcode "
Set qd = db.CreateQueryDef("tmpExport", StrSQL)
DoCmd.TransferText acExportDelim, , "tmpExport", "c:file.csv" db.QueryDefs.Delete "tmpExport" –
Don't use [ ] around VBA variables. Don't use parens for the MsgBox when you just want to give user a message. The parens make it a function that requires a response by user to set a variable.
MsgBox strcustcode & " has " & ordercount & " orders"
Concatenate the variable into the SQL statement. If OCUSTCODE is a text type field, use apostrophe delimiters for the parameter.
StrSQL = "Select * From [24-ND_Cus] Where [OCUSTCODE] = '" & strcustcode & "'"
I don't advise code that routinely modifies design and changing a query SQL statement is changing design. If the only change is filter criteria and a dynamic parameterized query won't work, I suggest a 'temp' table - table is permanent, data is temporary. Delete and write records to the table and export the table.
I am querying an external database with power query through Excel. What I am wanting to do is populate my where clause based off of cell values so for example my sql statement would read something like
Select *
From employees
where hiredate between Sheet1!A1 and Sheet2!A2
I tried the above syntax and it does not work, but I think it illustrates what I am wanting to achieve.
How can I pass parameters from my Excel sheet into my power query syntax?
EDIT
I tried following the blog.oraylis.de (not actual link, link is in comments), but I get the below error when I try to execute my statement
DataSource.Error ODBC:Error[4203] ERROR: column "start_p" does not exist;
I created a table named it parameters added headers & Values, and the full SQL I try to run is this:
let
startp_Param = Excel.CurrentWorkbook(){[Name="Parameter"]}[Content],
startp_Value = startp_Param {0}[Value],
endp_Param = Excel.CurrentWorkbook(){[Name="Parameter"]}[Content],
endp_Value = endp_Param {1}[Value],
Source = Odbc.Query("dsn=postgresql", "Select * FROM employees where hiredate BETWEEN startp_Value AND endp_Value")
in
Source
EDIT # 2
I altered my query to read like this
"Select *
FROM employees
where hiredate BETWEEN " & startp_Value & " AND " & endp_Value
However this now presents an error of:
"We cannot apply operator & to types Text and Number"
startp_Value and endp_Value are steps in Power Query, so you have to use & to add it to the string, like this:
Source = Odbc.Query("dsn=postgresql", "Select * FROM employees where hiredate BETWEEN " & startp_Value & " AND " & endp_Value
This doesn't handle sanitizating your SQL query, so you may want to do it to startp_Value and endp_Value (for example, by converting to a Date and then back to a Text).