【问题标题】:Use SQL code in vba access在 vba 访问中使用 SQL 代码
【发布时间】:2018-03-25 23:52:35
【问题描述】:

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

最好的问候。

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

DoCmd.RunSQL sqlupdate

【问题讨论】:

标签: sql vba ms-access sql-update


【解决方案1】:

你有一个额外的双引号,并且缺少几个空格 - 试试这样:

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

【讨论】:

  • 您不能在字符串文字中继续执行 VBA 语句,因此行尾 & " SET Assay.assay_repeat = " & 0 & " _ 会导致语法错误 - 最后一个 & " 不应该在那里。跨度>
【解决方案2】:

您只是错过了表名末尾和位置之前的空格字符。

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

【讨论】:

    【解决方案3】:

    这是将 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

    【讨论】:

      【解决方案4】:

      我建议您使用记录集。

      Private Sub Update_My_Records(Parent As Object)
      Dim Data_Recset As Object
      Dim Parent_Reference As Object
      Set Data_Recset = Parent_Reference.Application.DBEngine.Workspaces(0).Databases(0).OpenRecordset("SELECT * FROM Assay WHERE assay_repeat = " & 0 & ";", DB_OPEN_DYNASET)
          Data_Recset.MoveLast
          Data_Recset.MoveFirst
          Data_Recset.Edit
          Data_Recset.Fields("assay_repeat") = 1
          Data_Recset.Update
          Data_Recset.Close
      Set Data_Recset = Nothing
      End Sub
      

      假设 父级引用了 Access.Application。 (我通常通过:Form.Module.Parent 对 Sub/Function 的引用)
      表或查询“Assay”已经存在。 您一次只需要更新 1 行 但如果你想在表单中使用查询:

      Private Sub Query_Definition_Update()
        Dim Obj_Qdef As Object
        Dim Query_Name As String
        Query_Name = "Q_Assay"
        Me.Form.Application.DBEngine.Workspaces(0).Databases(0).QueryDefs.Refresh
        Set Obj_Qdef = Me.Form.Application.DBEngine.Workspaces(0).Databases(0).QueryDefs(Query_Name)
        Obj_Qdef.SQL = Query_Update(1)
        Debug.Print Obj_Qdef.SQL
        Obj_Qdef.Execute
        ''When finished updating
        Obj_Qdef.Close
        Set Obj_Qdef = Nothing
       End Sub
      
      '------------------------------------------------------------'
      
       Private Function Query_Update(New_Value as Integer) As String
        Query_Update = "UPDATE Assay" & _
                 " SET Assay.assay_repeat = " & 0 & "" & _
                 " WHERE (((Assay.[assay_repeat])= " & New_Value & "));"
       End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-22
        • 1970-01-01
        • 1970-01-01
        • 2017-03-28
        • 1970-01-01
        • 2017-07-04
        • 1970-01-01
        相关资源
        最近更新 更多