How do a assign a GUID to a DAO parameter in VBA

醉酒当歌 提交于 2019-12-12 20:31:23

问题


I have a piece of code that boils down to

dim stmt   as dao.queryDef
dim parId  as dao.parameter

set stmt = currentDB().createQueryDef("", _
    "parameters id guid, ...; insert into tab (id, ... ) values ([id], ...)")

I created the table with

 create table tab (
    id guid,
    ...
 )

and

alter table tab add constraint tab_pk primary key (id)

Later, I want to assign a GUID to parId:

parId.value = GuidFromString("{936DA01F-9ABD-4D9D-80C7-02AF85C822A8}")

This assignment causes a Run-time error 3421: Data type conversion error.

How is it possible to assign a GUID to a dao.parameter variable?

Updated example

This is an updated example that uses a longbinary parameter that is not working in my environment: it causes a Runtime Error 3001 (Invalid Argument) when stmt.execute is executed.

option explicit

sub main() ' {

    dim db as dao.database
    set db = application.currentDB

  ' db.execute("drop table tab")

    createTable  db
    insertValues db

end sub ' }

sub createTable(db as dao.database) ' {

    db.execute(   _
   "create table tab ( " & _
   "  id     guid,     " & _
   "  txt    char(60)  " & _
   ")")

   db.execute("alter table tab add constraint tab_pk primary key (id)")


end sub ' }


sub insertValues(db as dao.database) ' {

     dim stmt as dao.queryDef

     set stmt = db.createQueryDef("",   _
       "parameters "                  & _
       "  id     longbinary,  "       & _
       "  txt    char(60);    "       & _
       "insert into tab values ([id], [txt]) ")


     dim parId   as dao.parameter
     dim parTxt  as dao.parameter

     set parId  = stmt.parameters("id" )
     set parTxt = stmt.parameters("txt")

     parId.value  =  GuidFromString("{936DA01F-9ABD-4D9D-80C7-02AF85C822A8}")
     parTxt.value = "Hello world."

     stmt.execute ' Access throws Runtime Error 3001 (Invalid argument)

end sub ' }

回答1:


The GUID type is a special type that's not valid as a parameter.

Instead, use LONGBINARY to pass the GUID as binary data to the query:

set stmt = currentDB().createQueryDef("", _
    "parameters id LONGBINARY, ...; insert into tab (id, ... ) values ([id], ...)")

In response to the new MCVE:

The main error there is the VARCHAR declaration. The following modifications work:

Sub insertValues(db As dao.Database) ' {
     Dim stmt As dao.QueryDef
     Set stmt = db.CreateQueryDef("", _
       "parameters " & _
       "  idparam   LONGBINARY ;" & _
       "insert into tab(ID)  values ([idparam]) ")
     Dim parId   As dao.Parameter
     Dim parTxt  As dao.Parameter

     Set parId = stmt.Parameters("idparam")
     'Set parTxt = stmt.Parameters("txtparam")
     stmt.Parameters!idparam.Value = GUIDFromString("{936DA01F-9ABD-4D9D-80C7-02AF85C822A8}")
     'stmt.Parameters!txtparam.Value = "Hello world."
     stmt.Execute 'no errors
End Sub ' }

And

Sub insertValues(db As dao.Database) ' {
     Dim stmt As dao.QueryDef
     Set stmt = db.CreateQueryDef("", _
       "parameters " & _
       "  idparam   BINARY, txtparam CHAR(60) ;" & _
       "insert into tab  values ([idparam], txtparam) ")
     Dim parId   As dao.Parameter
     Dim parTxt  As dao.Parameter

     Set parId = stmt.Parameters("idparam")
     Set parTxt = stmt.Parameters("txtparam")
     stmt.Parameters!idparam.Value = GUIDFromString("{936DA01F-9ABD-4D9D-80C7-02AF85C822A8}")
     stmt.Parameters!txtparam.Value = "Hello world."
     stmt.Execute 'no errors
End Sub ' }


来源:https://stackoverflow.com/questions/53960334/how-do-a-assign-a-guid-to-a-dao-parameter-in-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!