本文介绍了MS访问prepared声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在VBA中的本地表中的MS Access执行prepared语句:

 更新零件SET part_description =? WHERE PART_ID =?
 

如果这样是如何做的?

解决方案

昏暗的分贝作为DAO.DATABASEDIM QDF作为DAO.QueryDef昏暗STRSQL作为字符串设置DB = CurrentDbSTRSQL =UPDATE Month_Totals设置item_date = [which_date]&放大器; _    WHE​​RE ID = [which_id];Debug.Print STRSQL设置QDF = db.CreateQueryDef(vbNullString,STRSQL)qdf.Parameters(which_date)。值=日期()qdf.Parameters(which_id)。值= 1qdf.Execute dbFailOnError

这示例使用了一个新的,未保存的QueryDef 。如果你有一个保存的参数查询,您可以改为代位线为 CreateQueryDef 行使用它:

设置QDF = db.QueryDefs(YourQueryName)

无论哪种方式,您可以参考各个参数由他们的名字和我一样,或由它们在SQL语句中的位置......所以这将工作与上面相同:

qdf.Parameters(0).value的=日期()qdf.Parameters(1)。价值= 1

其他注意事项:

  1. 。价值参数的默认属性,因此它包括在这里并不严格要求。在另一方面,它不会伤害是明确的。
  2. 如戈德文所述,您可以使用砰符号与像 QDF参数的名称!which_id ,这比 QDF更简洁。参数(which_id)

Is it possible to execute a prepared statement in MS Access on a local table in VBA like this:

UPDATE part SET part_description=? WHERE part_id=?

If so how is it done?

解决方案
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSql As String
Set db = CurrentDb
strSql = "UPDATE Month_Totals Set item_date = [which_date]" & _
    " WHERE id = [which_id];"
Debug.Print strSql
Set qdf = db.CreateQueryDef(vbNullString, strSql)
qdf.Parameters("which_date").Value = Date()
qdf.Parameters("which_id").Value = 1
qdf.Execute dbFailOnError

That example used a new, unsaved QueryDef. If you have a saved parameter query, you can use it instead by substituting this line for the CreateQueryDef line:

Set qdf = db.QueryDefs("YourQueryName")

Either way, you can then refer to individual parameters by their names as I did, or by their positions in the SQL statement ... so this will work same as above:

qdf.Parameters(0).Value = Date()
qdf.Parameters(1).Value = 1

Additional notes:

  1. .Value is the default property for a Parameter, so including it here is not strictly required. On the other hand, it doesn't hurt to be explicit.
  2. As Gord noted below, you can use "Bang notation" with the parameter's name like qdf!which_id, which is more concise than qdf.Parameters("which_id")

这篇关于MS访问prepared声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 12:04