How to fix 'Undefined function 'xxx' in expression' (Excel Function in SQL Query) - sql

I am trying to call an Excel function to a SQL query, but I keep getting the same error : "Undefined function xxx in expression".
I'm working with Excel only due to customer choices.
I've checked if references were missing,
My function is in a valid Module wiht Option Explicit, and it is declared as Public.
And i made sure I had a return value.
Thank you for your time
There is my function
Public Function CalculRamasseCarriste(ByVal lng_QTE As Integer, ByVal str_CodeArticle As String, ByVal lng_QTEmax As Integer) As Boolean
Static lng_cumulQTE As Integer
Static str_Text As String
Static bln_Prendre As Boolean
If str_Text = "" Then
str_Text = str_CodeArticle
ElseIf str_Text <> "" Then
If str_Text <> str_CodeArticle Then
str_Text = str_CodeArticle
lng_cumulQTE = 0
End If
End If
lng_cumulQTE = lng_cumulQTE + lng_QTE
If lng_cumulQTE < lng_QTEmax Then
CalculRamasseCarriste = True
bln_Prendre = True
ElseIf lng_cumulQTE >= lng_QTEmax And bln_Prendre = True Then
CalculRamasseCarriste = True
bln_Prendre = False
Else
CalculRamasseCarriste = False
End If
End Function
And there is my query :
Return_Qry_Ramasse = "SELECT T1.[Adresse], T1.[N° Support], T1.[Code_Article], T1.[Libellé_article] " & _
" FROM [V_Stock$] as T1 " & _
" LEFT JOIN [Cmd$] as T2 on T1.[Code_Article] = T2.[Code_Article] " & _
" WHERE CalculRamasseCarriste(T1.[Nb Box-Colis / Pal], T1.[Code_Article], T2.[UVC_cmd])= True " & _
" ORDER BY [Code_Article], [Adresse], [N° Support]"

Related

Latebinding issue for object

Following code causes an error at oDataObject.lastError and oDataObject.clearerror() of object ,while setting the option strict On a late binding issue persists.
Before option strict set to on the oDataObject.lastError and oDataObject.clearerror() showing as inbuild function and supports all class.
If Not trySettingValue(moProp, oDataObject, oTB.Text, sError) Then
oTB.BackColor = System.Drawing.Color.Red
bOk = False
Else
oTB.BackColor = System.Drawing.Color.White
End If
Public Shared Function trySettingValue(ByRef oProp As
System.Reflection.PropertyInfo, _
ByRef oDataObject As Object, _
ByVal oValue As Object, _
ByRef sError As String) As Boolean
Try
oDataObject.clearerror()
Select Case oProp.PropertyType.FullName
Case "System.String"
Dim str As String
str = CType(oValue, String)
oProp.SetValue(oDataObject, str, Nothing)
Case Else
End Select
' errorhandling
If oDataObject.lastError <> "" Then
sError = sError & oDataObject.lastError
Return False
Else
Return True
End If
Catch e As System.Exception
Trace.WriteLine(e.Message)
Trace.Flush()
sError = sError & "De ingevoerde waarde voor '" & oProp.Name & "' is foutief." & vbCrLf
Return False
End Try
End Function

Multiple OR in SQL string Variable, VBA

I am using a bit a code to Adjust form length and an SQL variable to modify the names that show up in a Combobox. I want to make sure that the names in the Comboboxes above the one that is being worked on, do not show any names.
So if Combo1 has 40 names then Combo2 will only have 39 and so on.
My problem seems to be when I use multiple "OR" within the WHERE Clause of the SQL String. Can anyone advice?
Private Sub Worker3_Change()
Dim Worker4STR As String
'Shows Worker 4
Me.Worker4.RowSource = ""
Worker4STR = "SELECT T1Workers.NonUserID, T1Workers.[FirstName] & "" "" & [LastName] AS FullName FROM T1Workers WHERE T1Workers.NonUserID <>" & _
Me.Worker1.Value & " OR " & Me.Worker2.Value & " Or " & Me.Worker3.Value
Me.Worker4.RowSource = Worker4STR
Me.Worker4.AllowValueListEdits = False
Me.Worker4.ColumnCount = 2
Me.Worker4.ColumnWidths = "0, "
Me.Worker4.Visible = True
Me.Activity4.Visible = True
Me.w4a1s.Visible = True
Me.w4a1d.Visible = True
Me.w4a1o.Visible = True
Me.w4a2s.Visible = True
Me.w4a2o.Visible = True
Me.w4a2d.Visible = True
Me.w4a3s.Visible = True
Me.w4a3o.Visible = True
Me.w4a3d.Visible = True
Me.w4a4s.Visible = True
Me.w4a4o.Visible = True
Me.w4a4d.Visible = True
Me.w4a5s.Visible = True
Me.w4a5o.Visible = True
Me.w4a5d.Visible = True
Me.InsideHeight = 1440 * 4.6
End Sub
Thank you in Advance.
-Matt
Your clause:
X <> A OR B OR C
Is not valid SQL. You can use NOT IN instead:
"WHERE T1Workers.NonUserID NOT IN (" & _
Me.Worker1.Value & " , " & _
Me.Worker2.Value & " , " & _
Me.Worker3.Value & " ) "
Note that if the values are strings they at a minimum need to be enclosed in quotes. A much better solution is to use SQL parameters instead, but I do not know if that's possible with whatever Worker4 is.

Update multi table access db using vb.net

Can someone please help me with that?
My prob is that When i use dataAdapter.Update in this next construction i get a return value of zero (no line was updated but no reason or errors return).
The update sentense works when i run it using Access and other update requests works fine when i work on a single table.
Im Using:
vb.net \ visual studio 2012 \ access 2010 DB
I update the Data on my DataGridView and call the update function:
update command:
Private Function createGeneralUpdateStatement() As String
Return "UPDATE EntitysDataTbl INNER JOIN ManagersExtraDataTbl ON EntitysDataTbl.entityID = ManagersExtraDataTbl.managerID " + _
"SET EntitysDataTbl.entityUserName = [#EntitysDataTbl].entityUserName, " + _
"EntitysDataTbl.entityLastEntry = [#EntitysDataTbl].entityLastEntry, " + _
"ManagersExtraDataTbl.mPassword = [#ManagersExtraDataTbl].mPassword, " + _
"ManagersExtraDataTbl.workGroup = [#ManagersExtraDataTbl].workGroup, " + _
"ManagersExtraDataTbl.entityAccessType = [#ManagersExtraDataTbl].entityAccessType, " + _
"ManagersExtraDataTbl.mActive = [#ManagersExtraDataTbl].mActive" + _
"WHERE EntitysDataTbl.entityID= [#EntitysDataTbl].entityID"
End Function
The parameters are:
Dim parmList(2) As String
parmList(0) = "mPassword"
parmList(1) = "entityUserName"
and the update function is:
Public Function updateDB(ByVal updateCommand As String, ByVal parmList() As String, Optional ByVal insertCommand As String = "") As Boolean
Dim res As Boolean = False
SyncLock Me
Try
mainDataSet.EndInit()
mUpdateCommand = New OleDb.OleDbCommand(updateCommand, MyBase.getConnector)
For Each Str As String In parmList
If Not Str Is Nothing Then
If (Str.StartsWith("%")) Then
mUpdateCommand.Parameters.Add("#" & Str.Trim("%"), OleDb.OleDbType.Boolean, MAX_COL_LEN, Str.Trim("%"))
Else
mUpdateCommand.Parameters.Add("[#" & Str & "]", OleDb.OleDbType.VarChar, MAX_COL_LEN, Str)
End If
End If
Next
mDataAdapter.UpdateCommand = mUpdateCommand
If (Not insertCommand.Equals("")) Then
mInsertCommand = New OleDb.OleDbCommand(insertCommand, MyBase.getConnector)
For Each Str As String In parmList
If Not Str Is Nothing Then
'If Str.Equals("RuleIDNum") Then
' Continue For
'End If
If (Str.StartsWith("%")) Then
mInsertCommand.Parameters.Add("#" & Str.Trim("%"), OleDb.OleDbType.Boolean, MAX_COL_LEN, Str.Trim("%"))
Else
mInsertCommand.Parameters.Add("[#" & Str & "]", OleDb.OleDbType.VarChar, MAX_COL_LEN, Str)
End If
End If
Next
mDataAdapter.InsertCommand = mInsertCommand
End If
Dim i As Integer = mDataAdapter.Update(mainDataSet)
'ERROR - i = 0 => NO LINE WAS UPDATED!!!!
res = True
Catch ex As Exception
res = False
End Try
mDBWasUpdated = res
End SyncLock
Return res
End Function

Error: Index and length must refer to a location within the string

Im getting the following exception on vb.net. I have trouble solving this error:
Error 5: Index and length must refer to a location within the string.Parameter name: length.
here is my code :
Private Sub attemptedNumbersNew2(ByVal date_start As DateTime, ByVal date_end As DateTime, ByVal port As String, ByVal team As String)
Dim con As New ADODB.Connection
Dim recordS As New ADODB.Recordset
Dim sql As String = "some query...."
Try
con.Open(connectStringA)
recordS.Open(sql, con)
If recordS.EOF Then
Else
Do While Not recordS.EOF
Dim temp As String
If name4number.Contains(lastAttempted) Then
temp = name4number.Item(lastAttempted)
Else
temp = lastAttempted
End If
If attemptedNames.Equals("NONE") Or attemptedNames.Equals("") Then
attemptedNames = temp
ElseIf Not temp.Equals("") Then
attemptedNames = attemptedNames & " - " & temp
End If
If agentsHashOffered.Contains(temp & "_" & team) Then
agentsHashOffered.Item(temp & "_" & team) = agentsHashOffered.Item(temp & "_" & team) + 1
Else
agentsHashOffered.Add(temp & "_" & team, 1)
End If
If recordS.Fields(2).Value >= oldDate Then
If answeringNumber.Equals("NONE") Then
answeringNumber = recordS.Fields(4).Value
If answeringNumber.Substring(0, 1).Equals("9") Then
answeringNumber = answeringNumber.Substring(1, 10)
End If
callDuration = -1
End If
disconnectionTime = recordS.Fields(3).Value
Else
End If
If attempted.Equals("NONE") Then
attempted = recordS.Fields(4).Value
If attempted.Substring(0, 1).Equals("9") Then
attempted = attempted.Substring(1, 10)
End If
attemptedCount = attemptedCount + 1
lastAttempted = attempted
Else
temp = recordS.Fields(4).Value
If temp.Substring(0, 1).Equals("9") Then
temp = temp.Substring(1, 10)
End If
attempted = attempted & " - " & temp
attemptedCount = attemptedCount + 1
lastAttempted = temp
End If
recordS.MoveNext()
Loop
End If
con.Close()
con = Nothing
recordS = Nothing
Catch ex As Exception
MsgBox("Error #" & Err.Number & ": " & Err.Description)
End Try
End Sub
It was working fine the other day, but suddenly I started getting this error.
any helps are welcome
THANK YOU in Advance

Recursive Function Not Returning

I am hopeing someone can help me here with a recursive function I have that is not returning either true or false as I would have espected it to. The function loops through a Active Directory group for its members and then calls itself if it encounters any groups within the membership in order to gets its members as well. I am trying to return either true or false based on if any errors were encountered but not haveing any luck at all. It appears to just hang and never return back to the primary calling sub that starts the recursive function. Below is my code I am using:
Private Sub StartAnalysis(ByVal grp As String, ByVal grpdn As String, ByVal reqid As String)
Dim searchedGroups As New Hashtable
'prior work before calling sub
searchedGroups.Add(grp, 1)
Dim iserror As Boolean = GetGroupMembers(grpdn, searchedGroups, reqid)
If iserror = False Then
'do stuff
Else
'do stuff
End If
'cleanup
End Sub
Public Function GetGroupMembers(ByVal groupSearch As String, ByVal searchedGroups As Hashtable, ByVal requestID As String) As Boolean
Dim iserror As Boolean = False
Try
Dim lastQuery As Boolean = False
Dim endLoop As Boolean = False
Dim rangeStep As Integer = 999
Dim rangeLow As Integer = 0
Dim rangeHigh As Integer = rangeLow + rangeStep
Do
Dim range As String = "member"
If lastQuery = False Then
range = String.Format("member;range={0}-{1}", rangeLow, rangeHigh)
Else
range = String.Format("member;range={0}-*", rangeLow)
endLoop = True
End If
Dim group As SearchResult = QueryObject(groupSearch, range)
Dim groupCN As String = group.Properties("cn")(0).ToString
If group.Properties.Contains(range) Then
For Each member As Object In group.Properties(range)
Dim user As SearchResult = QueryObject(member.ToString, "member")
Dim userCN = user.Properties("cn")(0).ToString
If Not user.Properties.Contains("member") Then
Dim userMail = String.Empty
If user.Properties.Contains("mail") Then
userMail = user.Properties("mail")(0).ToString
End If
userCN = userCN.Replace("'", "''")
Dim qry As String = _
"INSERT INTO group_analysis_details (request_id, member_name, member_email, member_group) " & _
"values ('" & requestID & "', '" & userCN & "', '" & userMail & "', '" & groupCN & "')"
Dim sqlConn As SqlConnection = New SqlConnection(cs)
Dim sqlCmd As SqlCommand = New SqlCommand(qry, sqlConn)
sqlConn.Open()
sqlCmd.ExecuteNonQuery()
sqlConn.Close()
sqlCmd.Dispose()
sqlConn.Dispose()
Else
If Not searchedGroups.ContainsKey(userCN) Then
searchedGroups.Add(userCN, 1)
iserror = GetGroupMembers(user.Properties("distinguishedname")(0).ToString, searchedGroups, requestID)
If iserror = True Then Return iserror
Else
searchedGroups(userCN) += 1
End If
End If
Next
Else
lastQuery = True
End If
If lastQuery = False Then
rangeLow = rangeHigh + 1
rangeHigh = rangeLow + rangeStep
End If
Loop While endLoop = False
Return iserror
Catch ex As Exception
myEvents.WriteEntry("Error while analyzing the following group: " & groupSearch & vbCrLf & vbCrLf & _
"Details of the error are as follows: " & ex.Message, EventLogEntryType.Error)
Return True
End Try
End Function
Hopefully someone can point out where I might be making my error is this.
Thanks,
Ron
Generally if you're using a 'Do...Loop While' and manually setting the exit condition inside the loop it's very easy to get stuck in an infinite loop which is what causes the program to hang.
It looks like you're not setting endloop = True in all circumstances. Try changing it to an Exit Do and adding one to each of the various conditions you have. A bit of trial and error will be required to get it just right.
Also to make your life easier extract the database insert code into a seperate function and call it when needed.