#Func! MS Access中的iif查询出错

时间:2021-04-27 15:44:53

Below is my sql query:

下面是我的SQL查询:

IIf(remedy_src.Position Is Null,(mid(remedy_src.User,instr(1,remedy_src.User,"(")+1,instr(1,remedy_src.User,")")-2-instr(1,remedy_src.User,"(")+1)),remedy_src.Position) AS [Adjusted User]

The point is to extract string from a field. Here's an example of the value:

关键是从字段中提取字符串。以下是该值的示例:

n123456 (name lastname)

the IIf function returns what is in the brackets:

IIf函数返回括号中的内容:

name lastname

But. Sometimes the source value looks like that:

但。有时源值看起来像这样:

n123456

No brackets, and the IIf returns the ugly #Func! error which prevents the query to be refreshed in my excel file (external data connection to access db).

没有括号,IIf返回丑陋的#Func!阻止在我的Excel文件中刷新查询的错误(访问db的外部数据连接)。

I would like to handle this error somehow. Preferably to make the IIf function return raw source value if error is present.

我想以某种方式处理这个错误。优选地,如果存在错误,则使IIf函数返回原始源值。

3 个解决方案

#1


1  

You could try to catch the error:

您可以尝试捕获错误:

IIF(IsERROR(IIf(remedy_src.Position Is Null,(mid(remedy_src.User,instr(1,remedy_src.User,"(")+1,instr(1,remedy_src.User,")")-2-instr(1,remedy_src.User,"(")+1)),remedy_src.Position)),
remedy_src.user,
IIf(remedy_src.Position Is Null,(mid(remedy_src.User,instr(1,remedy_src.User,"(")+1,instr(1,remedy_src.User,")")-2-instr(1,remedy_src.User,"(")+1)),remedy_src.Position)) 
AS [Adjusted User]

or

IIF(InStr("(",remedy_src.user)=0,
remedy_src.user,
IIF(IsERROR(IIf(remedy_src.Position Is Null,(mid(remedy_src.User,instr(1,remedy_src.User,"(")+1,instr(1,remedy_src.User,")")-2-instr(1,remedy_src.User,"(")+1)),remedy_src.Position))
As [Adjusted User]

#2


0  

I think that this SQL string is getting a little too complex to easily work with. I have made a VBA function that should be able to do the same thing but with easily understandable and easier to work with.

我认为这个SQL字符串有点过于复杂而无法轻松使用。我已经制作了一个VBA功能,应该可以做同样的事情,但易于理解和更容易使用。

The Code:

Public Function ExtractString(str As String) As String

    Dim intFirstBracket As Integer
    Dim intSecondBracket As Integer
    Dim blnValidString As Boolean

    blnValidString = False

    If Nz(str, "") <> "" Then
        intFirstBracket = InStr(1, str, "(", vbTextCompare)
        If intFirstBracket > 0 Then
            intSecondBracket = InStr(1, str, ")", vbTextCompare)

            If intSecondBracket > 0 Then
                str = Mid(str, intFirstBracket + 1, intSecondBracket - (intFirstBracket + 1))
                blnValidString = True
            End If
        End If
    End If

    If blnValidString Then
        ExtractString= str
    Else
        ExtractString= "Default Value" 'Handle this how you want
    End If

End Function

So your SQL string would simply be:

所以你的SQL字符串就是:

IIf(remedy_src.Position Is Null, ExtractString(remedy_src.User) ,remedy_src.Position) AS [Adjusted User]

The function I have tested, the SQL I have not so just take that into consideration.

我测试的函数,SQL我还没有考虑到这一点。

#3


0  

Here's how I got a solution for this issue, kind of completely reqwriting the code to exclude any finction to return errors.

这是我如何得到这个问题的解决方案,完全重新编写代码以排除任何返回错误的结果。

 IIf(remedy_src.Position Is Null,replace(replace(right(remedy_src.User,len(remedy_src.User)-instr(1,remedy_src.User,' ')),'(',''),')',''),remedy_src.Position) AS [Adjusted User]

#1


1  

You could try to catch the error:

您可以尝试捕获错误:

IIF(IsERROR(IIf(remedy_src.Position Is Null,(mid(remedy_src.User,instr(1,remedy_src.User,"(")+1,instr(1,remedy_src.User,")")-2-instr(1,remedy_src.User,"(")+1)),remedy_src.Position)),
remedy_src.user,
IIf(remedy_src.Position Is Null,(mid(remedy_src.User,instr(1,remedy_src.User,"(")+1,instr(1,remedy_src.User,")")-2-instr(1,remedy_src.User,"(")+1)),remedy_src.Position)) 
AS [Adjusted User]

or

IIF(InStr("(",remedy_src.user)=0,
remedy_src.user,
IIF(IsERROR(IIf(remedy_src.Position Is Null,(mid(remedy_src.User,instr(1,remedy_src.User,"(")+1,instr(1,remedy_src.User,")")-2-instr(1,remedy_src.User,"(")+1)),remedy_src.Position))
As [Adjusted User]

#2


0  

I think that this SQL string is getting a little too complex to easily work with. I have made a VBA function that should be able to do the same thing but with easily understandable and easier to work with.

我认为这个SQL字符串有点过于复杂而无法轻松使用。我已经制作了一个VBA功能,应该可以做同样的事情,但易于理解和更容易使用。

The Code:

Public Function ExtractString(str As String) As String

    Dim intFirstBracket As Integer
    Dim intSecondBracket As Integer
    Dim blnValidString As Boolean

    blnValidString = False

    If Nz(str, "") <> "" Then
        intFirstBracket = InStr(1, str, "(", vbTextCompare)
        If intFirstBracket > 0 Then
            intSecondBracket = InStr(1, str, ")", vbTextCompare)

            If intSecondBracket > 0 Then
                str = Mid(str, intFirstBracket + 1, intSecondBracket - (intFirstBracket + 1))
                blnValidString = True
            End If
        End If
    End If

    If blnValidString Then
        ExtractString= str
    Else
        ExtractString= "Default Value" 'Handle this how you want
    End If

End Function

So your SQL string would simply be:

所以你的SQL字符串就是:

IIf(remedy_src.Position Is Null, ExtractString(remedy_src.User) ,remedy_src.Position) AS [Adjusted User]

The function I have tested, the SQL I have not so just take that into consideration.

我测试的函数,SQL我还没有考虑到这一点。

#3


0  

Here's how I got a solution for this issue, kind of completely reqwriting the code to exclude any finction to return errors.

这是我如何得到这个问题的解决方案,完全重新编写代码以排除任何返回错误的结果。

 IIf(remedy_src.Position Is Null,replace(replace(right(remedy_src.User,len(remedy_src.User)-instr(1,remedy_src.User,' ')),'(',''),')',''),remedy_src.Position) AS [Adjusted User]