MS Access 2013 calling insert queries from VBA with strange errors

后端 未结 2 1003
时光说笑
时光说笑 2020-12-12 04:30

I have a saved insert query to add a record to a table. The parameters for the query come from an unbound form. 2 of the fields for the table are of the yes/no data type. Th

2条回答
  •  感情败类
    2020-12-12 05:26

    The problem are the LongText parameters. I tried a similar query:

    PARAMETERS pText1 Text ( 255 ), pInt1 Short, pMemo1 LongText, pYesno1 Bit, pYesno2 Bit;
    INSERT INTO ForInsert ( text1, int1, Memo1, yesno1, yesno2 )
    SELECT [pText1] AS A1, [pInt1] AS A2, [pMemo1] AS A3, [pYesno1] AS A4, [pYesno2] AS A5;
    

    with this code

    Set db = CurrentDb
    Set qdf1 = db.QueryDefs("qAppForInsert")
    
    qdf1.Parameters(0).Value = "asdf"
    qdf1.Parameters(1).Value = 77
    qdf1.Parameters(2).Value = String(3, "a")
    qdf1.Parameters(3).Value = True
    qdf1.Parameters(4).Value = False
    
    qdf1.Execute
    

    and various length for the LongText parameter (2, 10, 3) .

    Resulting in this crazy data (the Yes/No fields were always yesno1 = True and yesno2 = False !) :

    +----+-------+------+------------+--------+--------+
    | ID | text1 | int1 |   Memo1    | yesno1 | yesno2 |
    +----+-------+------+------------+--------+--------+
    |  8 | asdf  |   77 | aa         | True   | False  |
    |  9 | asdf  |   77 | aaaaaaaaaa | False  | False  |
    | 10 | asdf  |   77 | aaa        | False  | True   |
    +----+-------+------+------------+--------+--------+
    

    So apparently with LongText parameters, you are better off using RecordSet.AddNew instead of a parameterized query.

    Parameters are limited to 255 characters anyway.

    Addendum

    If I run the code in a loop to find a system, I also get the Reserved error (-3033).

提交回复
热议问题