I have a script which is not running on my system. I am not expert in this and the person who made script is no more with the organisation anymore.
Any assistance on this will be appreciated.
After Doing Debug, the highlighted filed is ".Refresh BackgroundQuery:=False"
Below is the script: -
Sub Run_Customs_Report()
'
' Macro4 Macro
'
'
Sheets("Customs_Report").Select
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=GOMS_MEM;UID=OD173;PWD=Adbu765$;DBQ=MDCGOMP.WORLD;DBA=W;APA=T;EXC=F;FEN=T;QTO=T;FRC=10;FDL=10;LOB=T;RST=T;BTD=F;BNF=F;BAM" _
), Array( _
"=IfAllSuccessful;NUM=NLS;DPM=F;MTS=T;MDI=F;CSR=F;FWC=F;FBS=64000;TLO=O;MLD=0;ODA=F;STE=F;TSZ=8192;" _
)), Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT TO_CHAR (D.PRINT_PART_NO) AS ""Print Part"",A.CATALOG_DESC AS ""Descripcion"",B.COUNTRY_OF_ORIGIN AS ""Country"",B" _
, _
".Packing_list_no AS ""Packing List"",TO_CHAR (A.order_no) AS ""Order"", A.Customer_PO_NO AS ""PO"", ROUND(A.Unit_Price) AS ""Unit" _
, _
" Price"", SUM (B.QTY_SHIPPED) AS ""Qty"", ROUND(SUM (A.unit_Price * B.QTY_SHIPPED)) AS ""Ext Total"", TO_CHAR (B.INVOICE_NO) AS" _
, _
" ""Manifest"", TO_CHAR(B.INVOICE_DATE) AS ""Date"", C.note_text AS ""GOMS Ref"", TO_CHAR (B.TRACKING_NO) AS ""Tracking No"", TO_CHAR" _
, _
" (A.SHIP_TO_ABBR) AS ""Ship To Location"" FROM oeorder_detail A INNER JOIN oeorder_shipping B ON A.part_no = B.part_n" _
, _
"o AND A.order_no = B.order_no AND A.line_no = B.line_no INNER JOIN OECUSTOMER_ORDER_NOTES C ON A.order_no = C.order" _
, _
"_no INNER JOIN PART_DESCRIPTION D ON A.part_no = D.part_no AND A.SHIP_TO_ABBR IN ('54827','54734','55945','56169','" _
, _
"55897','56086') AND (TRUNC (SYSDATE) - TRUNC (INVOICE_DATE)) = '1' AND B.tracking_no IS NOT NULL AND note_no = '1' " _
, _
"AND LINE_NO is null GROUP BY D.PRINT_PART_NO,A.CATALOG_DESC,B.COUNTRY_OF_ORIGIN,B.Packing_list_no,A.order_no," _
, _
"Customer_PO_NO,A.Unit_Price,B.INVOICE_NO,B.INVOICE_DATE,C.note_text, B.TRACKING_NO, A.SHIP_TO_ABBR ORDER BY A.Ship_" _
, "To_Abbr, B.INVOICE_NO")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_ExternalData_1"
.Refresh BackgroundQuery = False
end with
end sub
The error is generated by the .Refresh line because it's that line which triggers the execution of the SQL query.
Whilst I find the use of Array() a bit strange, if nothing has changed in the code, I would check with your database administrator whether the user account OD173 is still active.
Related
I recorded a macro that works fine, but I want to use a dynamic date so I don't have to enter it every time. I want to use the WHERE statement to pull records between today and one year prior to today. I had to submit specific date ranges when I recorded the macro, and I want to replace the timestamp with something dynamic, but CURRENT_DATE didn't work (syntax error) and TIMESTAMPADD(SQL_TSI_YEAR, -1, CURRENT_DATE) didn't work (syntax error). The macro recorded as such:
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"ODBC;DRIVER={Centura SQLBase 3.5 32-bit Driver -NT & Win95};UID=ODBC;;DB=CAM32;SRVR=01CAM001A" _
, Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT SALES_LOG_DETAIL.STORE_NUMBER, SALES_LOG_DETAIL.DATE_TIME, SALES_LOG_DETAIL.ITEM_NUMBER, SALES_LOG_DETAIL.QUANTITY, SALES_LOG_DETAIL.PRICE_PAID" & Chr(13) & "" & Chr(10) & "FROM SYSADM.SALES_LOG_DETAIL SALES_LOG_DETAIL" & Chr(13) & "" & Chr(10) & "W" _
, _
"HERE (SALES_LOG_DETAIL.STORE_NUMBER>=2 And SALES_LOG_DETAIL.STORE_NUMBER<=11) AND (SALES_LOG_DETAIL.DATE_TIME>={ts '2017-09-11 00:00:00'} And SALES_LOG_DETAIL.DATE_TIME<={ts '2018-09-11 00:00:00'})" & Chr(13) & "" & Chr(10) & "O" _
, "RDER BY SALES_LOG_DETAIL.STORE_NUMBER, SALES_LOG_DETAIL.ITEM_NUMBER")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Sales_Log_All_Stores"
.Refresh BackgroundQuery:=False
End With
I have no idea why it formats it that way, but when I try to change it I get errors.
How do I change that timestamp code to something that will pull the current date and also look back one year?
My VBA query rightly fetches the data from Access to Excel. However, the values for all the Time related fields are coming as zero. What should I do?
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=D:\Fair\Macro Workings\Genie Trackers\Ac Genie.accdb;DefaultDir=D:\Fair\Macro Workings\Genie Trackers;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout" _
), Array("=5;PWD="";UID=admin;")), Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT * FROM `D:\Fair\Macro Workings\Genie Trackers\Ac Genie.accdb`.DailyActivities DailyActivities" & Chr(13) & "" & Chr(10) & "WHERE (DailyActivities.`Accountant`='" & UName & "') AND (DailyActivities.`Due Date`<={ts '" & BDate & "'}) " _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_MS_Access_Database2"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.ListObjects("Table_Query_from_MS_Access_Database2").Unlist
The dates are coming as numbers but when we format it, that is coming right. However, the time fields are showing zero
Screen shot of the excel result
Screen shot of both database and excel result.
Any help highly appreciated.
The following is the VBA code I am using to query the database. After querying the data base I would like to be able to perform a separate query on the new table I have created. I would like to do this query using TIMES in date format as the parameter. However, when excel loads the information from the database it loads the TIMES column as a text format instead of a date format. This makes querying by saying
.ComandText = Array("SELECT TIMES FROM [QUERY1 WHERE TIMES BETWEEN '24-JUN-17 05:59:00' and '25-JUN-17 02:00:00' ]
give an error.
I know how to change the column format by opening up the query and manually changing the format I am hoping there is a way to do this in VBA
ActiveWorkbook.Queries.Add Name:="Query1", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Odbc.Query(""dsn=Database"", ""SELECT c.IP_VALUE AS """"PRODUCT"""", c.IP_TIME as CTIMES , s.IP_TIME AS TIMES, s.IP_VALUE AS """"Weight""""#(lf)FROM """"Product"""" AS c , """"Weight"""" as s#(lf)#(lf)WHERE c.IP_TIME between '24-JUN-17 05:59:00' AND '29-JUN-17 06:00:00'"")" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " Source"
Sheets.Add After:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Query1" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT TIMES FROM [Query1]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Query1"
End With
I have this Sub with which I receive data from external connection. I already specified a date range :
((infactln.dtransact>{d '2015-09-01'}) AND (infact.dtransact<{d '2015-09-30'}))
I want this sub to give the user the possibility to change this date range without going in "Data - Connections - Properties"
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Visual FoxPro Tables;UID=;;SourceDB=\\serverName\data.dbc;
SourceType=DBC;Exclusive=No;Background" _
), Array("Fetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes;")),
Destination:= _Range("$A$1")).QueryTable.CommandText = Array(
_"SELECT infact.ctransid, infact.ccode, infactln.dtransact,
infactln.ccode, infactln.cdesc, infactln.bqte, infactln.bmontant,
infactln.bcoutant" & Chr(13) & "" & Chr(10) & "FROM infact infact,
infactln infactln" & Chr(13) & "" & Chr(10) & "WHERE infact.kid =" _
, _" infactln.cparentid AND ((infactln.dtransact>{d '2015-09-01'}) AND
(infact.dtransact<{d '2015-09-30'}))" _)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = True
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = False
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_Visual_FoxPro_Tables"
.Refresh BackgroundQuery:=False
End With
You can (and should) use Format function:
https://msdn.microsoft.com/en-us/library/office/gg251755.aspx
Than you just give the user opportunity to specify specific dates in some cells, let's say these are "A1" and "B1". You just substitute the date values in your query with
" infactln.cparentid AND ((infactln.dtransact>{d '" & Format(Cells(1, "A"), "yyyy-MM-dd") & "'}) AND
(infact.dtransact<{d '" & Format(Cells(1, "B"), "yyyy-MM-dd") & "'}))"
I am using VBA in Excel to make query and I want replace the
St" _
), Array( _
"efl\zv162_part1.xls
with constant part1
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Excel Files;DBQ=\\St" _
), Array( _
"efl\zv162_part1.xls;DefaultDir=cesta;DriverId=1046;FIL=excel 12.0;MaxBufferSize=2048;PageTimeout=5;" _
)), Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT `zv162_part1$`.F2, `zv162_part1$`.F4, `zv162_part1$`.F5, `zv162_part1$`.F6, `zv162_pa" _
, _
"rt1$`.F7, `zv162_part1$`.F8, `zv162_part1$`.F9, `zv162_part1$`.F10, `zv162_part1$`.F11, `zv162_part1$`.F12, `zv162_part1$`.F13, `zv162_part1$`.F14, `zv162_part1$`.F15, `zv162_part1$`.F16, `z" _
, _
"v162_part1$`.F17, `zv162_part1$`.F18, `zv162_part1$`.F19, `zv162_part1$`.F20" & Chr(13) & "" & Chr(10) & "FROM `zv162_part1$` `zv162_part1$`" & "WHERE `zv162_part1$`.F2 <> Null" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_Excel_Files13"
.Refresh BackgroundQuery:=False
End With
Thank you for any response!
I add whole command for query
Try this
part1 = "stefl\zv162_part1.xls"
With ActiveSheet.ListObjects.Add( _
SourceType:=0, _
Source:=Array(Array("ODBC;DSN=Excel Files;DBQ=" & _
part1 & _
";DefaultDir=cesta;DriverId=1046;FIL=excel 12.0;MaxBufferSize=2048;PageTimeout=5;") _
), Destination:=Range("$A$1")).QueryTable