Microsoft Access中的VBA - 运行时错误'3075' [英] Microsoft Access VBA - Run time error '3075'

查看:1079
本文介绍了Microsoft Access中的VBA - 运行时错误'3075'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到运行时错误3075。我是一个新手,在VBA! ><我能知道它是在哪里出了问题?我解决不了的......

有关,例如,如果我进入一个名为萨利到一个文本框(txtMainName),在点击搜索按钮错误弹出式。

错误:

运行时错误'3075':

语法错误(缺少操作员)在查询前pression'而[主申请人名称]像'仙'。

 公用Sub Search_Record()

昏暗stDocName作为字符串
昏暗stLinkCriteria作为字符串
昏暗的stLinkCriteria1作为字符串
昏暗的stLinkCriteria2作为字符串


    stLinkCriteria =
    stDocName =frmDisplayInfo

    如果(我txtMainName<!>中),然后
        stLinkCriteria1 =[主申请人名称]像'&放大器;我[txtMainName]放!; '
        stLinkCriteria = stLinkCriteria和放大器; 和&放大器; stLinkCriteria1
    结束如果

    如果(我txtIDNo<!>中),然后
        stLinkCriteria2 =[ID号]象与&我[txtIDNo]放!; '
        stLinkCriteria = stLinkCriteria和放大器; 和&放大器; stLinkCriteria2
    结束如果

    (*此部分突出显示*)
    DoCmd.OpenForm stDocName,,,stLinkCriteria
    DoCmd.Maximize

结束小组
 

解决方案

运行时错误3975 无效的操作符,根据在快速谷歌搜索访问VBA运行时错误3075 (你可能做你自己)。

现在的问题是,你只能分配的值 stCriteria 。如果第一个如果语句的执行, stCriteria 然后被分配的值 stCriteria和stCriteria1 ,这是无效的。

如果执行了第二个如果语句同样的事情发生。 (事实上​​,如果两者都实际运行, stCriteria 现在包含键,stCriteria1和stCriteria2 ,这更是雪上加霜。)

解决的办法是修改code只添加,如果它的需要:

  stLinkCriteria =
stDocName =frmDisplayInfo

如果(我txtMainName<!>中),然后
    stLinkCriteria =[主申请人名称]像'&放大器;我[txtMainName]放!; '
结束如果

如果(我txtIDNo<!>中),然后
    如果stLinkCriteria<>  然后
        stLinkCriteria = stLinkCriteria和放大器;  和 
    结束如果
    stLinkCriteria2 =[ID号]象与&我[txtIDNo]放!; '
    stLinkCriteria = stLinkCriteria和放大器; 和&放大器; stLinkCriteria2
结束如果
 

有关备查,顺便解决这类问题是实际检查的变量的值(在这种情况下, stCriteria )只是导致错误的前行(在这里,它是 DoCmd.OpenForm 行)执行。您可以通过设置一个断点,其中的错误发生行,运行你的应用程序,直到一个断点,然后检查变量的值,这样做。这说明你的完全的是什么变量包含,它可以揭示的问题。

I've encountered the run time error '3075'. I'm a novice in VBA! >.< Can i know where did it went wrong? I can't solve it...

For e.g, if I enter a name "Sally" into a textbox (txtMainName), upon clicking on the search button the error pops-up.

The error:

Run-time error '3075':

Syntax error(missing operator) in query expression ' And [Main Applicant Name] Like 'Sally'".

Public Sub Search_Record()

Dim stDocName As String
Dim stLinkCriteria As String
Dim stLinkCriteria1 As String
Dim stLinkCriteria2 As String


    stLinkCriteria = ""
    stDocName = "frmDisplayInfo"

    If (Me!txtMainName <> "") Then
        stLinkCriteria1 = "[Main Applicant Name] Like ' " & Me![txtMainName] & "'"
        stLinkCriteria = stLinkCriteria & " And " & stLinkCriteria1
    End If

    If (Me!txtIDNo <> "") Then
        stLinkCriteria2 = "[ID No] Like ' " & Me![txtIDNo] & "'"
        stLinkCriteria = stLinkCriteria & " And " & stLinkCriteria2
    End If

    '(*This part is highlighted*)
    DoCmd.OpenForm stDocName, , , stLinkCriteria
    DoCmd.Maximize

End Sub

解决方案

Runtime error 3975 is Invalid operator, according to a quick Google search on access vba runtime error 3075 (which you could have made yourself).

The problem is that you only assign a value of "" to stCriteria. If the first if statement executes, stCriteria is then assigned the value of stCriteria AND stCriteria1, which is invalid.

The same thing happens if the second if statement is executed. (In fact, if both are actually run, stCriteria now contains AND stCriteria1 AND stCriteria2, which is even worse.)

The solution is to modify your code to only add the AND if it's needed:

stLinkCriteria = ""
stDocName = "frmDisplayInfo"

If (Me!txtMainName <> "") Then
    stLinkCriteria = "[Main Applicant Name] Like ' " & Me![txtMainName] & "'"
End If

If (Me!txtIDNo <> "") Then
    If stLinkCriteria <> "" then
        stLinkCriteria = stLinkCriteria & " AND "
    End If
    stLinkCriteria2 = "[ID No] Like ' " & Me![txtIDNo] & "'"
    stLinkCriteria = stLinkCriteria & " And " & stLinkCriteria2
End If

For future reference, the way to solve these kind of problems is to actually examine the value of the variable (in this case stCriteria) just before the line causing the error (here, it's the DoCmd.OpenForm line) executes. You do so by setting a breakpoint on the line where the error happens, running your app until the breakpoint is hit, and then examining the value of the variable. This shows you exactly what the variable contains, which can reveal the problem.

这篇关于Microsoft Access中的VBA - 运行时错误'3075'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆