How to evaluate stored functions? - sql

I have the following saved in a database.
Function SearchFileForName()
SearchFileForName = False
Set fso = CreateObject("Scripting.FileSystemObject")
Set csvFile = fso.OpenTextFile("ListOfUsers.csv", 1)
Do Until csvFile.AtEndOfStream
if app.userID = csvFile.ReadLine then
SearchFileForName = true
end if
loop
end Function
Function WriteNameToFile()
Set fso = CreateObject("Scripting.FileSystemObject")
Set csvFile = fso.OpenTextFile("ListOfUsers.csv", 8)
csvFile.WriteLine app.userID
end function
Function theMessage()
if weekday(Now()) = vbfriday then
response = msgbox ("Great Job "& split(app.userid, ".")(0) & "!" & _
vbnewline & vbnewline & _
"Keep up the good work and keep that morale high." & _
vbnewline & vbnewline & _
"Would you like a cookie for your efforts?", vbyesno)
if response = vbyes then
msgbox "Enjoy =)" & _
vbnewline & _
vbnewline & _
vbnewline & _
" _ . : : : : : . _" & vbnewline & _
" . : : : ` _ | _ ` : : : ." & vbnewline & _
" / : : ` - - | - - ` : : \" & vbnewline & _
" | : ` . - - - ` - - - . ` : |" & vbnewline & _
" | : ( O R E O ) : |" & vbnewline & _
" | : : ` - - - - - - - ` : : |" & vbnewline & _
" \ : : : . . . . . . . : : : /" & vbnewline & _
" ` : : : : : : : : : : : `" & vbnewline & _
" ` ` ` ` ` ` `" & vbnewline
elseif response = vbno then
msgbox "That is too bad." & vbnewline & _
"Here is a cookie anyways." & vbnewline & _
"Enjoy =)" & _
vbnewline & _
vbnewline & _
vbnewline & _
" _ . : : : : : . _" & vbnewline & _
" . : : : ` _ | _ ` : : : ." & vbnewline & _
" / : : ` - - | - - ` : : \" & vbnewline & _
" | : ` . - - - ` - - - . ` : |" & vbnewline & _
" | : ( O R E O ) : |" & vbnewline & _
" | : : ` - - - - - - - ` : : |" & vbnewline & _
" \ : : : . . . . . . . : : : /" & vbnewline & _
" ` : : : : : : : : : : : `" & vbnewline & _
" ` ` ` ` ` ` `" & vbnewline
end if
end if
End Function
Function easterEgg()
if not SearchFileForName() then
WriteNameToFile
theMessage
end if
end Function
So I call it with the following sql query
Function easterEgg0()
Set rseasterEgg = CreateObject("ADODB.RecordSet")
rseasterEgg.Open _
" SELECT dyCode " & _
" FROM DDCode " & _
" WHERE dyName = 'EasterEggScript'", _
Connection, adOpenStatic, adLockBatchOptimistic, adCmdText
Execute rseasterEgg.fields("dyCode").value
Call easterEgg
End Function
When I print it out it looks exactly as expected. But when I try to run it I get an error saying Typemismatch: 'SearchFileForName'?
What am I doing wrong?

From MSDN - Execute Statement
The context in which the Execute statement is invoked determines what
objects and variables are available to the code being run. In-scope
objects and variables are available to code running in an Execute
statement. However, it is important to understand that if you execute
code that creates a procedure, that procedure does not inherit the
scope of the procedure in which it occurred.
Like any procedure, the new procedure's scope is global, and it
inherits everything in the global scope. Unlike any other procedure,
its context is not global scope, so it can only be executed in the
context of the procedure where the Execute statement occurred.
However, if the same Execute statement is invoked outside of a
procedure (i.e., in global scope), not only does it inherit everything
in global scope, but it can also be called from anywhere, since its
context is global.
To overcome this use ExecuteGlobal instead.
Function easterEgg0()
Set rseasterEgg = CreateObject("ADODB.RecordSet")
rseasterEgg.Open _
" SELECT dyCode " & _
" FROM DDCode " & _
" WHERE dyName = 'EasterEggScript'", _
Connection, adOpenStatic, adLockBatchOptimistic, adCmdText
ExecuteGlobal rseasterEgg.fields("dyCode").value
Call easterEgg
End Function

Related

Having issues with how to use ' or " in sql statement and query using concatrelated function

I have the following code using the ConcatRelated function. the current sql works with 1 where condition but I can't figure out how to get the sql to work with 2 conditions. It works when use it in a query. I get issues and the code does not debug.
ItemsShpdInvSQL = "SELECT InvoiceAllShippedItems.ShipmentID, InvoiceAllShippedItems.ShipType, " _
& "InvoiceAllShippedItems.BillTo, InvoiceAllShippedItems.CustID AS Consignee, " _
& "InvoiceAllShippedItems.PayMethod, InvoiceAllShippedItems.PaidInFull, " _
& "First(InvoiceAllShippedItems.Units) AS Units, " _
& "'Freight Charges - Ref. WR: ' & (ConcatRelated('[WR]'," _
& "'[InvoiceAllShippedItems]','[BillTo] =' & [BillTo])) & " _
& "' - (' & Sum([Chargeable]) & ' ' & [Units] & ' " _
& ") - Number of Pieces: ' & [NoPieces] AS InvDetails, " _
& "Sum(InvoiceAllShippedItems.[# of Pieces]) AS NoPieces, " _
& "Sum(InvoiceAllShippedItems.Chargeable) AS Charge, " _
& "CustRate([custid],[ShipType]) AS CustomerRate, " _
& "IIf(([Charge])<6.1,20/[Charge],[CustomerRate]) AS RateMin " _
& "FROM InvoiceAllShippedItems " _
& "GROUP BY InvoiceAllShippedItems.ShipmentID, " _
& "InvoiceAllShippedItems.ShipType, InvoiceAllShippedItems.BillTo, " _
& "InvoiceAllShippedItems.CustID, InvoiceAllShippedItems.PayMethod, " _
& "InvoiceAllShippedItems.PaidInFull, InvoiceAllShippedItems.Units, " _
& "CustRate([custid],[ShipType]) " _
& "ORDER BY InvoiceAllShippedItems.BillTo;"
'& "CustRate([custid],[ShipType]);"
with this it works in a query but not in the code above:
'concatrelated("[WR]","InvoiceAllShippedItems","[BillTo]&[PayMethod] ='" & [BillTo] & [PayMethod] & "'")

SQL UPDATE function syntax error

I am attempting to write an UPDATE function based on values that will be input into dropdown boxes and text boxes by a user
My code is as follows:
Private Sub cmdUpdate_Prices_Click()
Dim SQL_string As String
SQL_string = _
"UPDATE tblPrice " & _
"SET [P/N] = " & Me.txtPartNumber.Column(0) & ", " & _
"Type = " & Me.txtPriceType.Column(0) & ", " & _
"Region = " & Me.txtRegion.Column(0) & ", " & _
"Price = " & Me.txtPrice.Value & ", " & _
"WHERE ID = " & Me.txtID.Value & ";"
DoCmd.RunSQL SQL_string
End Sub
I keep getting a syntax error in the UPDATE function and I'm not sure where it is.
Any help is appreciated. Thanks!

Runtime error '1004' General odbc error while refreshing excel sheet to get updated data

We are using shared folder in server where we keep all excel sheets based on our business requirement so whoever requires that document he will picked up that document from that shared folder and he will receive all update/manipulated data by clicking on "Refresh" button in "Data" tab in excel 2007,so in my organization everybody pc is working fine and they are getting updated data by refreshing document but in my pc the movement i click on refresh i am getting this error which is in image below please provide me a clear answer.
Sub TT_Out()
' ' Macro2 Macro
Dim RngFromDate, RngToDate
RngFromDate = InputBox("Enter Start Date !", "TT Out", Date - 1)
RngToDate = InputBox("Enter End Date !", "TT Out", RngFromDate)
With Range("Table_Query_from_ALXORCL[TT_OUT_DATE]").ListObject.QueryTable
.Connection = Array(Array( _
"ODBC;DRIVER={Oracle in instantclient_12_1};" & _
"SERVER=ALXORCL;UID=ALXLIVE;PWD=alx123;" & _
"DBQ=ALXORCL;DBA=W;APA=T;EXC=F;XSM=Default;FEN=T;QTO=T;FRC=10;F"), _
Array("DL=10;LOB=T;RST=T;BTD=F;BNF=F;BAM=IfAllSuccessful;NUM=NLS;" & _
"DPM=F;MTS=T;MDI=Me;CSR=F;FWC=F;FBS=60000;TLO=O;" & _
"MLD=0;ODA=F;STE=F;TSZ=8"), Array("192;"))
.CommandText = Array( _
" SELECT ALX_TT_OUT.TT_OUT_CODE, " & _
" ALX_TT_OUT.TT_OUT_DATE, " & _
" ALX_TT_OUT.F_NAME, " & _
" ALX_TT_OUT.B_F_NAME, " & _
" ALX_TT_OUT.SENDING_PRPS, " & _
" ALX_LOOKUP_DET.LOOKUP_DET_NAME||'-'||ALX_TT_OUT.DOC_NO, " & _
" ALX_PRODUCT.PRODUCT_CODE, " & _
" ALX_TT_OUT.QTY*ALX_TT_OUT.SELL_RATE, " & _
" ALX_CORRESPONDENT.CORRESPONDENT_NAME" & Chr(13) & Chr(10) & _
" FROM ALXTEST.ALX_CORRESPONDENT ALX_CORRESPONDENT, ALXTEST2.ALX_LOOKUP_DET ALX_LOOKUP_DET, ALXTEST2.ALX_PRODUCT ALX_PRODUCT, ALXL", _
" IVE.ALX_TT_OUT ALX_TT_OUT" & Chr(13) & "" & Chr(10) & _
" WHERE ALX_PRODUCT.PRODUCT_ID = ALX_TT_OUT.PRODUCT_ID " & _
" AND ALX_TT_OUT.CORRESPONDENT_ID = ALX_CORRESPONDENT.CORRESPONDENT_ID " & _
" AND ALX_LOOKUP_DET.LOOKUP_DET_ID = ALX_TT_OUT.DOC_TYPE_L ", _
" AND ((ALX_TT_OUT.TT_OUT_CODE Not Like '%HOF%') " & _
" AND (to_date(TT_OUT_DATE) Between '" & RngFromDate & "' And '" & RngToDate & "') " & _
" )")
.Refresh BackgroundQuery:=False
End With
End Sub

Error adding code to workbook via VBA

I am trying to use VBA in Excel to add conditional formatting to a column of a pivot table. The issue is that whenever the pivot table is refreshed, or a filter is changed, etc. the conditional formatting is lost. My solution was to add a macro to the pivot table update event in the workbook, which works ... kinda. It seems that when I run the code that creates the pivot table and adds the code to handle conditional formatting an error occurs but ONLY when the VBA window is NOT open. If the VBA window is open the code executes normally - despite no code changes or reference changes.
Private Sub setupConditionalFormattingForStatusColumn()
Dim thisSheetModule As vbcomponent
Dim formattingCodeString As String
On Error GoTo conditionalFormattingError
formattingCodeString = _
"Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)" & vbNewLine & _
" With Target.parent.Columns(" & harReportColumn("Status") & ")" & vbNewLine & _
" .FormatConditions.AddIconSetCondition" & vbNewLine & _
" .FormatConditions(.FormatConditions.Count).SetFirstPriority" & vbNewLine & _
vbNewLine & _
" With .FormatConditions(1)" & vbNewLine & _
" .IconSet = ActiveWorkbook.IconSets(xl4TrafficLights)" & vbNewLine & _
" .IconCriteria(1).Icon = xlIconYellowExclamation" & vbNewLine & _
vbNewLine & _
" With .IconCriteria(2) " & vbNewLine & _
" .Type = xlConditionValueNumber" & vbNewLine & _
" .value = -1" & vbNewLine & _
" .Operator = 5" & vbNewLine & _
" .Icon = xlIconGreenCircle" & vbNewLine & _
" End With" & vbNewLine & _
vbNewLine & _
" With .IconCriteria(3)" & vbNewLine & _
" .Type = xlConditionValueNumber" & vbNewLine & _
" .value = 1.05" & vbNewLine & _
" .Operator = 7" & vbNewLine & _
" .Icon = xlIconYellowCircle" & vbNewLine & _
" End With" & vbNewLine
formattingCodeString = formattingCodeString & vbNewLine & _
" With .IconCriteria(4)" & vbNewLine & _
" .Type = xlConditionValueNumber" & vbNewLine & _
" .value = 1.15" & vbNewLine & _
" .Operator = 7" & vbNewLine & _
" .Icon = xlIconRedCircleWithBorder" & vbNewLine & _
" End With" & vbNewLine & _
vbNewLine & _
" .ShowIconOnly = True" & vbNewLine & _
" End With" & vbNewLine & _
vbNewLine & _
" .HorizontalAlignment = xlCenter" & vbNewLine & _
" .VerticalAlignment = xlCenter" & vbNewLine & _
" End With" & vbNewLine & _
"End Sub"
Set thisSheetModule = ThisWorkbook.VBProject.VBComponents(harReportSheet.CodeName)
thisSheetModule.CodeModule.AddFromString formattingCodeString
Exit Sub
conditionalFormattingError:
errorLog.logError "WARNING: An error occured while applying the conditional formatting code for the ""Status"" column."
Err.Clear
Resume Next
End Sub
The line which generates the error is: thisSheetModule.CodeModule.AddFromString formattingCodeString but the error is only generated if the VBA window is closed.
Any ideas?
So I was able to find an answer to this issue. Evidently Excel does not properly initialize the codename property of newly created worksheets when the VBA window is not open (the why here is beyond me) but only when it recompiles. A work-around is to force Excel to recompile prior to any calls to the codename property. The solution which worked for me was to place the following code:
On Error Resume Next
Application.VBE.CommandBars.ActiveMenuBar.FindControl(ID:=578).Execute
On Error GoTo conditionalFormattingError
above the line beginning with Set thisSheetModule = ... . Oddly enough the line of code which forces the recompile also throws an error for me which I was able to safely ignore with the surrounding error handling.
More information can be found here: http://www.office-archive.com/2-excel/d334bf65aeafc392.htm
Hope that helps someone out there. :-)

The OLE DB provider "MSDASQL" for linked server "(null)" reported an error

I'm very very stuck here, help is greatly appreciated.
What am I trying to do?
There is an ASP-page (classic ASP) with a CSV upload that gives the following error:
Microsoft OLE DB Provider for SQL Server error '80040e14'
The OLE DB provider "MSDASQL" for linked server "(null)" reported an error. The provider did not give any information about the error.
/ùùù.stocklist.be/importCSVProcess.asp, line 86
I have created two linked servers:
EXEC master.dbo.sp_addlinkedserver
#server = N'txtsrv'
, #srvproduct=N'Jet 4.0'
, #provider=N'Microsoft Text Driver (*.txt; *.csv)'
, #datasrc=N'D:\WEBSITES\ùùù.stocklist.be\csv\upload'
, #provstr=N'Text'
and
EXEC sp_addlinkedserver
#server = 'Server1',
#srvproduct = '',
#provider = 'MSDASQL',
#datasrc = '
'
When I replace MSDASQL by txtsrv in the following query I get this error:
The OLE DB provider "txtsrv" has not been registered.
This is the code where the insert is done:
If upl.Form("rdbType") = "lot" Then
updFileNameSQL ="insert into cos_lot(lot_vin) " & _
"SELECT vin " & _
"FROM OPENROWSET " & _
"('MSDASQL', " & _
"'Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=" & sServerPath & ";', " & _
"'SELECT vin from " & UplFileName & "') " & _
"where not vin IS NULL AND vin collate SQL_Latin1_General_CP1_CI_AS not in (select lot_vin from cos_lot) "
Set updCmd = Server.CreateObject("ADODB.Command")
updCmd.ActiveConnection = MM_COS_STRING
updCmd.CommandText = updFileNameSQL
updCmd.Execute
ElseIf upl.Form("rdbType") = "premie" Then
updFileNameSQL ="INSERT INTO [ùùùSTOCKLIST].[dbo].[COS_ANNEX] " & _
"([annex_type] " & _
",[annex_chassisnr] " & _
",[annex_datum] " & _
",[annex_userid] " & _
",[annex_stockid] " & _
",[annex_commentNL] " & _
",[annex_commentFR] " & _
",[annex_premie] " & _
",[annex_premie_type] " & _
",[annex_consignatie] " & _
",[annex_eindeconsignatie] " & _
",[annex_online] " & _
",[annex_onlinefrom] " & _
",[annex_onlineto] " & _
",[annex_libelle]) " & _
"SELECT 2 " & _
",[VIN] " & _
",Getdate() " & _
", " & Session("user_id") & " " & _
",[STOCK] " & _
",'' " & _
",'' " & _
",Replace([PREMIE],',','.') " & _
",'E' " & _
",Null " & _
",Null " & _
",1 " & _
",cast([ONLINEFROM] as smalldatetime) " & _
",cast([ONLINETO] as smalldatetime) " & _
",[TYPE] " & _
"FROM OPENROWSET " & _
"('MSDASQL', " & _
"'Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=" & sServerPath & ";', " & _
"'SELECT vin,stock,type,premie,onlinefrom,onlineto from " & UplFileName & "') " & _
"WHERE NOT VIN IS NULL "
Set updCmd = Server.CreateObject("ADODB.Command")
updCmd.ActiveConnection = MM_COS_STRING
updCmd.CommandText = updFileNameSQL
updCmd.Execute
End If
at the end: updCmd.Execute I get the above error.
What could I be doing wrong?
Thanks in advance!
PS: server is Win2003 R2 x86
I wonder would it be worth trying a different connection string?
updFileNameSQL ="insert into cos_lot(lot_vin) " & _
"SELECT vin " & _
"FROM OPENROWSET " & _
"('Microsoft.Jet.OLEDB.4.0'," & _
"'Text;HDR=Yes;FMT=Delimited;DATABASE=" & sServerPath & ";'," & _
"'SELECT vin from [" & UplFileName & "]') " & _
"where not vin IS NULL AND vin collate SQL_Latin1_General_CP1_CI_AS " & _
"not in (select lot_vin from cos_lot) "