简体   繁体   English

VBA中的运行时错误'3075'

[英]Runtime error '3075' in VBA

I know there are plenty of posts with the same title, but I couldn't find anything similar to my problem. 我知道有很多标题相同的帖子,但是找不到与我的问题类似的东西。

I'm trying to update a record with SQL in VBA. 我正在尝试在VBA中使用SQL更新记录。 In particular, my code goes to an excel file, extracts a value, asks the user for the ID of the record that needs to be updated, and then should procced to do the update. 特别是,我的代码进入了excel文件,提取了一个值,要求用户提供需要更新的记录的ID,然后应该进行更新。 The thing is i tried running the same code without the user input, and works just fine, so the problem must be with the interpretation of the input. 事情是我尝试在没有用户输入的情况下运行相同的代码,并且工作得很好,所以问题肯定出在输入的解释上。 I checked that everything is a string but i don't know how to solve the problem. 我检查了一切都是字符串,但我不知道如何解决该问题。

Here is the code I am using: 这是我正在使用的代码:

Dim query2 as string
Dim myID as string
myID = InputBox("Insert ID:")
query2 = "UPDATE [Info test] " & "SET RESULT = " & var & "WHERE ID =" & myID
DoCmd.RunSQL (query2)

In this case, var is a string, and is the value i fetch from the excel file. 在这种情况下,var是一个字符串,并且是我从excel文件中获取的值。 From various tests, i think the problem is with the last & myID, like there is an apostrophe before the value stored by myID, but i don't know what to do about it, or if this is really the problem. 从各种测试来看,我认为问题出在last和myID上,就像myID存储的值前面有撇号一样,但是我不知道该怎么办,或者这是否确实是问题所在。

Thanks to everybody who can help 谢谢大家的帮助

Insert Debug.Print query2 before DoCmd, run the code and check output in immediate window. 在DoCmd之前插入Debug.Print query2 ,运行代码并在立即窗口中检查输出。 This SQl should work in query builder. 该SQl应该在查询生成器中工作。 I believe you need to enclose var in single quotes and add a space before WHERE 我相信您需要将var括在单引号中,并在WHERE之前添加一个空格

query2 = "UPDATE [Info test] SET RESULT = '" & Replace(var, "'", "''") & "' WHERE ID =" & myID

I added Replace in order to avoid errors if var contains single quote. 我添加了Replace,以避免在var包含单引号的情况下出错。

如果var是字符串,则需要使用单引号并在WHERE之前添加空格,如下所示

query2 = "UPDATE [Info test] " & "SET RESULT = '" & var & "' WHERE ID =" & myID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM