运行时错误 13 类型不匹配 VBA Access 2007

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9155367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:09:17  来源:igfitidea点击:

RunTime Error 13 Type Mismatch VBA Access 2007

ms-accessvbaruntime-errortype-mismatch

提问by Eyowzitgoin

I'm sure this is probably something simple so I apologize ahead of time. This is actually my first time ever writing VBA and I've come to a problem which I simply can't get my head around how to fix. I've searched and tried multiple things but still the same problem despite anything I try.

我确定这可能很简单,所以我提前道歉。这实际上是我第一次编写 VBA,我遇到了一个问题,我根本无法解决如何解决的问题。我已经搜索并尝试了多种方法,但尽管我尝试了任何方法,但仍然存在相同的问题。

I'm simply opening a new recordset, populating it with values from another table using SQL, retrieving the values from the recordset, then setting the caption on some labels on my form. My code:

我只是打开一个新记录集,使用 SQL 用另一个表中的值填充它,从记录集中检索值,然后在我的表单上的一些标签上设置标题。我的代码:

Dim oconStudents As ADODB.Connection
Dim orsStudents As ADODB.Recordset
Dim strStudentsSQL As String

Set oconStudents = CurrentProject.AccessConnection
Set orsStudents = New ADODB.Recordset
Set strGetCustIDSQL = _
    "SELECT [CustomerID] FROM [Students Classes] WHERE [Class ID] = " & _
    Me.txtClassID.Value & ";"

orsStudents.Open strGetCustIDSQL, oconStudents, adOpenForwardOnly, _
    adLockReadOnly, adCmdTable

With orsStudents
   If Not orsStudents.EOF Then
       lblStudent1.Caption = orsStudents.Fields.Item(0)
       orsStudents.MoveNext
   End If
End With

I run this and I get a runtime error 13 with this line:

我运行这个,我得到一个运行时错误 13 与这一行:

Set strGetCustIDSQL = _
    "SELECT [CustomerID] FROM [Students Classes] WHERE [Class ID] = " & _
    Me.txtClassID.Value & ";"

I can't for the life of me figure out why. I run the SQL in a query and it returns my values for me properly. I've changed the adCmdTable to adCmdText and tried adding "#" symbols as well as a host of other characters around the Me.txtClassID.Value section of the SQL string. Hovering over the SQL string, I can see the right "Class ID =" value (in this case "2") but hovering over the SQL string shows up "=empty".

我终其一生都无法弄清楚原因。我在查询中运行 SQL,它正确地为我返回我的值。我已将 adCmdTable 更改为 adCmdText 并尝试在 SQL 字符串的 Me.txtClassID.Value 部分周围添加“#”符号以及许多其他字符。将鼠标悬停在 SQL 字符串上,我可以看到正确的“Class ID =”值(在本例中为“2”),但将鼠标悬停在 SQL 字符串上会显示“=empty”。

What simple thing am I neglecting to notice? Thanks in advance for your help!

我忽略了注意什么简单的事情?在此先感谢您的帮助!

回答by Pynner

The Setpart of the assignment for the string is not correct. You only use Setfor objects (for example ADODB objects). Primitive data types (string, integer, etc.) use simple assignment.

Set分配的字符串的一部分是不正确的。您仅Set用于对象(例如 ADODB 对象)。原始数据类型(字符串、整数等)使用简单赋值。

i.e.

IE

strGetCustIDSQL = _
    "SELECT [CustomerID] FROM [Students Classes] WHERE [Class ID] = " & _
    Me.txtClassID.Value & ";"

Also I don't think the ";" is nessecary when executing SQL from ADODB.

我也不认为“;” 从 ADODB 执行 SQL 时是必要的。