2017-10-13 169 views
1

我在vba访问中使用下面的代码来更新表的一列,但它不起作用。请帮帮我。在vba中使用SQL代码access

此致敬礼。

Dim sqlupdate As String 
sqlupdate = "UPDATE Assay" _ 
& "SET Assay.assay_repeat = " & 0 & "" _ 
& "WHERE (((Assay.[assay_repeat])= " & 1 & "));" 

DoCmd.RunSQL sqlupdate 
+0

欢迎堆栈溢出!请阅读[*为什么“有人可以帮助我?”不是一个真正的问题?*](https://meta.stackoverflow.com/q/284236/1188513) - 然后再次阅读您的文章并[编辑]它,以便它包含一个明确的问题。 –

回答

2

您有一个额外的双引号和失踪一对夫妇的空间 - 尝试这样的:

Dim sqlupdate As String 
sqlupdate = "UPDATE Assay" _ 
& " SET Assay.assay_repeat = " & 0 & " _ 
& " WHERE (((Assay.[assay_repeat])= " & 1 & "));" 
+0

这是正确的,谢谢。 – Nasim

+0

您不能在字符串文字中继续使用VBA语句,因此行'&“SET Assay.assay_repeat =”&0&“_'的末尾会导致语法错误 - 最后一个'&”'shouldn'不要在那里。 – YowE3K

1

你只是错过了空间字符在表名的结束,之前在那里。

Dim sqlupdate As String 
sqlupdate = "UPDATE Assay " _ 
& "SET Assay.assay_repeat = " & 0 & " " _ 
& "WHERE (((Assay.[assay_repeat])= " & 1 & "));" 
0

这是将SQL字符串转换为VBA代码的好方法。

创建表格

表单只需要两个文本框和一个命令按钮。 SQL语句可能很长,因此您将文本框放在选项卡控件的不同页面上。

Create a new form (in design view.) 
Add a tab control. 
In the first page of the tab control, add a unbound text box. 
Set its Name property to txtSql. 
Increase its Height and Width so you can see many long lines at once. 
In the second page of the tab control, add another unbound text box. 
Name it txtVBA, and increase its height and width. 
Above the tab control, add a command button. 
Name it cmdSql2Vba. 
Set its On Click property to [Event Procedure]. 
Click the Build button (...) beside this property. 
When Access opens the code window, set up the code like this: 

Private Sub cmdSql2Vba_Click() 
    Dim strSql As String 
    'Purpose: Convert a SQL statement into a string to paste into VBA code. 
    Const strcLineEnd = " "" & vbCrLf & _" & vbCrLf & """" 

    If IsNull(Me.txtSQL) Then 
     Beep 
    Else 
     strSql = Me.txtSQL 
     strSql = Replace(strSql, """", """""") 'Double up any quotes. 
     strSql = Replace(strSql, vbCrLf, strcLineEnd) 
     strSql = "strSql = """ & strSql & """" 
     Me.txtVBA = strSql 
     Me.txtVBA.SetFocus 
     RunCommand acCmdCopy 
    End If 
End Sub 

http://allenbrowne.com/ser-71.html