【问题标题】:Value is not updating when sending to Access发送到 Access 时值未更新
【发布时间】:2018-04-09 16:31:19
【问题描述】:

AmeriFood(i), SpanFood(i), IndiFood(i) 的值在导出到我的 Access 表时没有更新。相反,它在整个循环中每次都连续输入值的第一个实例。

    InsertIntoString = "INSERT INTO [Data] ([American], [Spanish], [Indian]) "
''''ValuesString = "VALUES ([" & AmerFood(i) & "], [" & SpanFood(i) & "], [" & IndiFood(i) & "])"  ' I couldnt get this to work beauce there are spaces in the string.
ValuesString = "VALUES (?,?,?)"
strSql = InsertIntoString & ValuesString

With cmd
    Set .ActiveConnection = cn
    .CommandText = strSql
    .Parameters.Append .CreateParameter(, adVariant, adParamInput, , AmerFood(i))
    .Parameters.Append .CreateParameter(, adVariant, adParamInput, , SpanFood(i))
    .Parameters.Append .CreateParameter(, adVariant, adParamInput, , IndiFood(i))
    .Execute
End With

知道如何让值AmeriFood(i), SpanFood(i), IndiFood(i) 每次在 VBA 代码中循环时更新吗?我单步执行了代码,每个值都更新为正确的文本/值,但它永远不会导出为更新的值,只是给出的第一个值。

我原本打算使用注释掉的行

ValuesString = "VALUES ([" & AmerFood(i) & "], [" & SpanFood(i) & "], [" & IndiFood(i) & "])"

但由于变量字符串中有空格而出错。

我做这件事的方式对我没有影响,我只需要一些工作。

【问题讨论】:

    标签: vba excel ms-access export


    【解决方案1】:

    您需要共享完整的代码,但据我所知,您正在重复向同一命令添加参数。

    为每次迭代使用一个新命令:

    Set cmd = New ADODB.Command
    With cmd
        Set .ActiveConnection = cn
        .CommandText = strSql
        .Parameters.Append .CreateParameter(, adVariant, adParamInput, , AmerFood(i))
        .Parameters.Append .CreateParameter(, adVariant, adParamInput, , SpanFood(i))
        .Parameters.Append .CreateParameter(, adVariant, adParamInput, , IndiFood(i))
        .Execute
    End With
    

    请注意,adVariant 不受支持,请参阅docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-29
      • 2020-08-21
      • 2020-02-28
      • 1970-01-01
      • 2017-04-20
      • 2020-12-05
      • 2021-09-28
      • 1970-01-01
      相关资源
      最近更新 更多