简体   繁体   English

MS Access VBA:动态 SQL

[英]MS Access VBA: dynamic SQL

Inside of a function, I am using a string to run sql.在函数内部,我使用字符串来运行 sql。 I need to vary some of the string dependent on user selection.我需要根据用户选择改变一些字符串。 For brevity, I have included only one field from each table.为简洁起见,我只包含了每个表中的一个字段。 Also, these are not the real table names.此外,这些不是真正的表名。

strSQL = "DELETE * FROM tblWax"
db.Execute strSQL, dbFailOnError

strSQL = "INSERT INTO tblWax ( strPortName, lngShortSet ) " & _
         "SELECT tblAAA.strPastName, tblBBB.lngShortID " & _
         "FROM tblAAA INNER JOIN tblBBB ON tblAAA.Parameter = tblBBB.Parameter"
db.Execute strSQL, dbFailOnError

I would like to substitute tblSSS for tblBBB depending on user choice and with that same choice (within the field strChoice), tblWax needs to be tblHat.我想根据用户的选择将 tblSSS 替换为 tblBBB,并且使用相同的选择(在字段 strChoice 内),tblWax 需要是 tblHat。 That is: if strChoice = 1, then tblWax, tblAAA, tblBBB if strChoice = 2, then tblHat, tblAAA, tblSSS即:如果 strChoice = 1,则 tblWax, tblAAA, tblBBB 如果 strChoice = 2,则 tblHat, tblAAA, tblSSS

Otherwise, the rest of the string(s) is identical.否则,其余的字符串是相同的。

Simply use conditional logic like VBA's SELECT...CASE只需使用条件逻辑,如 VBA 的SELECT...CASE

Select Case strChoice

   Case 1
      strSQL = "DELETE * FROM tblWax"
      db.Execute strSQL, dbFailOnError

      strSQL = "INSERT INTO tblWax ( strPortName, lngShortSet ) " & _
               "SELECT tblAAA.strPastName, tblBBB.lngShortID " & _
               "FROM tblAAA INNER JOIN tblBBB ON tblAAA.Parameter = tblBBB.Parameter"

   Case 2
      strSQL = "DELETE * FROM tblHat"
      db.Execute strSQL, dbFailOnError

      strSQL = "INSERT INTO tblHat ( strPortName, lngShortSet ) " & _
               "SELECT tblAAA.strPastName, tblSSS.lngShortID " & _
               "FROM tblAAA INNER JOIN tblSSS ON tblAAA.Parameter = tblSSS.Parameter"

End Select

db.Execute strSQL, dbFailOnError

You can even use multiple values in one case您甚至可以在一种情况下使用多个值

Case 1, 3, 5
...
Case 2, 4, 6
...

And use an Else for the rest that do not follow other Case statements并对不遵循其他Case语句的其余部分使用Else

Case Else
...

To expand on @Smandoli's and @Gustav's answer on using string variables for the table names.扩展@Smandoli 和@Gustav 对表名使用字符串变量的回答。 This allows you to create multiple cases without the variable names getting lost in the SQL string.这允许您创建多个案例,而不会在 SQL 字符串中丢失变量名称。

Select Case strChoice

    Case 1:
        strTarget = "tblWax"
        strJoin = "tblBBB"

    Case 2:
        strTarget = "tblHat"
        strJoin = "tblSSS"

end select

strSQL = "DELETE * FROM " & strTarget
db.Execute strSQL, dbFailOnError

strSQL = "INSERT INTO " & strTarget & " ( strPortName, lngShortSet ) " & _
         "SELECT tblAAA.strPastName, " & strJoin & ".lngShortID " & _
         "FROM tblAAA INNER JOIN " & strJoin & _
         " ON tblAAA.Parameter = " & strJoin & ".Parameter"

db.Execute strSQL, dbFailOnError

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

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