【问题标题】:concatenate in VBA ACCESS在 VBA ACCESS 中连接
【发布时间】:2013-04-29 10:44:26
【问题描述】:

如何修复这个用 VBA 编写的“更新”SQL 查询?我想将“名称”字段更新为连接表单中的 2 个字段的值。我写了这段代码,但它不起作用,有什么建议吗?

这是我的代码:

strSQL = "UPDATE dbo.Soferi_test SET Name = '" & Me![subform_soferi].Form!numele
                                               & Me![subform_soferi].Form!prenumele & _
          "',Cipti = '" & Me![subform_soferi].Form!certificat_cipti & _
          "' WHERE IDNO = " & Me![subform_soferi].Form!IDNO

【问题讨论】:

  • “它不起作用”到底是什么意思?它会导致错误,还是只是不符合您的预期?
  • 您想要两个字段之间的任何内容吗?目前,它们直接粘在一起,没有分隔符。最后你也错过了& ";"
  • 当我点击一个按钮时他们不做任何更新
  • debug.print sql 并将输出粘贴到您的问题中

标签: ms-access vba


【解决方案1】:

这是我认为您正在努力实现的目标的基本大纲。

  • 在我看来,您的表单字段是从表单上的子表单引用的。

当您对对象进行完整引用时,您的 sql 看起来非常复杂,所以我更喜欢

  • 创建一些变量
  • 将表单对象赋值给变量和
  • 在 sql 中引用变量。

我认为它使程序更具可读性。您可以通过表单上的按钮调用以下内容:

'****Update a table based on the values on a subform****'
'Table name:    dbo.Soferi_test
'Form Name:     frmMain
'Subform Name:  subform_soferi

Sub updateTable()

'Declare your variables which will be used in the sql string

Dim strSQL As String
Dim strNumele As String
Dim strPrenumele As String
Dim strCertificatCipti As String
Dim strIDNO As String

'Assign your variables to the form\subform objects
'You should also add some validation to check that the fields are not null

strNumele = Form_frmMain.subform_soferi!numele
strPrenumele = Form_frmMain.subform_soferi!prenumele
strCertificatCipti = Form_frmMain.subform_soferi!certificat_cipti
strIDNO = Form_frmMain.subform_soferi!IDNO

'Build your sql string
strSQL = "UPDATE dbo.Soferi_test SET Soferi_test.Name = '" & strNumele & strPrenumele & "', Cipti = '" & strCertificatCipti & "' WHERE (((Soferi_test.IDNO)='" & strIDNO & "'));"

'Execute the sql
CurrentDb.Execute strSQL

'You will want to put in some error handling here

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 2015-08-19
    相关资源
    最近更新 更多