【问题标题】:Trying to use VBA to write query in Access尝试使用 VBA 在 Access 中编写查询
【发布时间】:2015-10-05 02:15:00
【问题描述】:

我的 Access VBA 中出现与以下语法不匹配的类型。我正在尝试通过查看是否有任何记录具有查看“Certs”表中的字符串值的日期来更新名为“Billing”的表,例如“2012-07-01”对应于我的表单的 billYear 文本框,例如2012 和我的 billMonth 文本框,例如07. 有没有更好的方法来编写 VBA 或看到错误 - 非常感谢:

Dim sRecert As String
Dim byear As Integer
Dim bmonth As Integer

byear = Me.billYear
bmonth = Me.billMonth

sRecert = "Update Billing set recertifying = -1 where (select     certificationExpireDate from certs where Left((certificationExpireDate),4) =  " & byear
    & " and Mid((certificationExpireDate),6,2) =  " & bmonth & ")"

DoCmd.RunSQL sRecert

我可能没有很好地解释它。我创建了一个从我的表单调用的真实查询: DoCmd.OpenQuery "updateRecert"
我在下面设置了我的 SQL 作为我正在使用的真实日期的测试。它在 SQL Server 中(ODBC 链接) 我的 dbo_certs 表和我的 dbo_billing 表只共享一个可连接字段 peopleID:

UPDATE dbo_Billing AS a INNER JOIN dbo_certs AS b ON a.peopleid = b.peopleid 
SET a.recertifying = -1
WHERE b.certificationExpireDate = '2015-08-31 00:00:00.000';

上面给出了数据不匹配错误。 我的底线是我的表单上有两个文本框,可以将数据最好地传递到我的 VBA 代码中:

  • billMonth 在这种情况下是 8 因为它是一个整数所以是 一个问题
  • billYear 是 2015 年

所以如果 dbo_cert 的字段“certificationExpireDate”是“2015-08-31 00:00:00.000”,我需要将我的 dbo_billing 表的“重新认证”字段更新为 -1,但前提是可以从表单中获取。

【问题讨论】:

    标签: vba ms-access


    【解决方案1】:

    有没有更好的方法来编写 VBA 或查看错误?

    是的。您需要错误处理

    我认为问题不在代码中,我认为是在 SQL 中。

    要对代码进行故障排除,请将其包装在一个好的错误处理程序中。

    Public Sub MyRoutine()
    
        On Error GoTo EH
    
        'put your code here
    
        GoTo FINISH
    
    EH:
    
        With Err
            MsgBox "Error" & vbTab & .Number & vbCrLf _
                & "Source" & vbTab & .Source & vbCrLf & vbCrLf _
                & .Description
        End With
    
        'for use during debugging
        Debug.Assert 0
        GoTo FINISH
        Resume
    
    FINISH:
    
        'any cleanup code here
    
    End Sub
    

    当 msgbox 显示错误时,记下 Source。这应该可以帮助您确定错误的来源。

    在调试期间使用的'后面的行很有帮助。以下是它们的使用方法:

    • 执行将在 Debug.Assert 0 行停止
    • 将黄色箭头(确定接下来要运行的行)拖到 Resume 行
    • 按键盘上的 {F8}(或使用菜单 Debug > Step Into)

    这将转到发生错误的行。在你的情况下,它可能是你代码的最后一行。

    【讨论】:

    • 你试过这个吗?
    【解决方案2】:

    SQL 中的错误...但是!!您确定certificationExpireDate字符串 并且一直 等于yyyy-mm-dd 模式吗?像您一样与“不确定”键建立关系是很危险的。我认为这不是一个好的数据库设计。

    但是,毕竟,对于你的情况:

    VBA:

    sRecert = "UPDATE Billing a inner join certs b " & _
              "on format(a.imaginary_date_field, """yyyy-mm-dd""") = b.certificationExpireDate " & _
              "set a.recertifying = -1 " & _
              "where CInt(Left((b.certificationExpireDate),4)) =  " & byear & " and CInt(Mid((b.certificationExpireDate),6,2)) =  " & bmonth
    

    查询定义:

    PARAMETERS Forms!your_form!byear Short, Forms!your_form!bmonth Short;
    UPDATE Billing a inner join certs b
        on format(a.imaginary_date_field, "yyyy-mm-dd") = b.certificationExpireDate
        set a.recertifying = -1
        where CInt(Left((b.certificationExpireDate),4)) = Forms!your_form!byear and CInt(Mid((b.certificationExpireDate),6,2)) = Forms!your_form!bmonth
    

    更新

    不匹配错误

    您可能会出错,因为您有日期/时间字段,而不是字符串。 MS Access 查询中的日期使用# 符号写入。 WHERE b.certificationExpireDate = #2015-08-31 00:00:00.000#;

    在你的情况下:

    PARAMETERS Forms!your_form!byear Short, Forms!your_form!bmonth Short;
    UPDATE dbo_Billing AS a INNER JOIN dbo_certs AS b ON a.peopleid = b.peopleid 
    SET a.recertifying = -1
    WHERE year(b.certificationExpireDate) = Forms!your_form!byear and Month(b.certificationExpireDate) = Forms!your_form!bmonth;
    

    更多信息请关注this链接

    【讨论】:

    • 我可能没有解释清楚。我创建了一个从我的表单调用的真实查询: DoCmd.OpenQuery "updateRecert" UPDATE dbo_Billing AS a INNER JOIN dbo_certs AS b ON a.peopleid = b.peopleid SET a.recertifying = -1 WHERE b.certificationExpireDate = '2015-08- 31 00:00:00.000';
    • @XiXiangQua,什么类型的认证ExpireDate?字符串还是日期/时间?
    • 它以日期时间的形式存储在 SQL Server 中,例如 '2008-12-31 00:00:00.000'
    • 为什么这会导致数据类型不匹配?: UPDATE dbo_Billing AS a INNER JOIN dbo_certs AS b ON a.peopleid = b.peopleid SET a.recertifying = -1 WHERE (((b.certificationExpireDate)= '2015-08-31 00:00:00.000'));
    • 我能够创建一个成功标记所需记录的独立查询: UPDATE dbo_Billing AS a INNER JOIN dbo_certs AS b ON a.peopleid = b.peopleid SET a.recertifying = -1 WHERE year( b.certificationExpireDate) = Forms!billing!billyear 和月份(b.certificationExpireDate) = Forms!billing!billmonth;谢谢大家!!
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多