改进访问模块并消除查询

时间:2019-03-19 23:36:25

标签: sql ms-access module access-vba format

我正在创建一个程序,以从tbl_a中识别在tbl_a.LevelA级别上产生异常余额的详细行,并将已识别的行插入tbl_d中。

每月一次,我将期末帐户余额导入tbl_a.LevelAtbl_a.LevelBtbl_a.AMOUNT

从那里,我已设置代码来确定当前余额是借方还是贷方,从参考表(tbl_b)更新正常余额,并确定明细行余额是否异常。

我还必须创建一个单独的选择查询,以识别余额在LevelA上是否异常。我使用此信息来识别LevelALevelB处所有异常的订单项。

我的导入,代码,查询和所需的输出如下:

导入

Import

代码

Private Sub Edit()

Dim MsgTitleImport As String

MsgTitleImport = "xxx"

Set MyDb = CurrentDb()

DoCmd.Hourglass False
DoCmd.SetWarnings False

'qry_a
MySQL = "UPDATE tbl_a " & _
        "INNER JOIN tbl_b ON tbl_a.LEVELA = tbl_b.LEVELA " & _
        "SET tbl_a.NORMAL = tbl_b.NORMAL " & _
        "WHERE tbl_a.LEVELA Is Not Null;"
MyDb.Execute MySQL

'qry_b
MySQL = "UPDATE tbl_a " & _
        "SET tbl_a.CURRENT = iif([tbl_a.AMOUNT]>=0,'POSITIVE','NEGATIVE') " & _
        "WHERE tbl_a.LEVELA Is Not Null;"
MyDb.Execute MySQL

'qry_c
MySQL = "UPDATE tbl_a " & _
        "SET tbl_a.ABNORMAL_B = iif([tbl_a.CURRENT]=[tbl_a.NORMAL],'N','Y') " & _
        "WHERE tbl_a.LEVELA Is Not Null;"
MyDb.Execute MySQL

'qry_d
MySQL = "INSERT INTO tbl_c (LEVELA, AMOUNT) " & _
        "SELECT qry_d.LEVELA, qry_d.AMOUNT " & _
        "FROM qry_d;"
MyDb.Execute MySQL

'qry_e
MySQL = "UPDATE tbl_c " & _
        "INNER JOIN tbl_b ON tbl_c.LEVELA = tbl_b.LEVELA " & _
        "SET tbl_c.NORMAL = tbl_b.NORMAL " & _
        "WHERE tbl_c.LEVELA Is Not Null;"
MyDb.Execute MySQL

'qry_f
MySQL = "UPDATE tbl_c " & _
        "SET tbl_c.CURRENT = iif([tbl_c.AMOUNT]>=0,'POSITIVE','NEGATIVE') " & _
        "WHERE tbl_c.LEVELA Is Not Null;"
MyDb.Execute MySQL

'qry_g
MySQL = "UPDATE tbl_c " & _
        "SET tbl_c.ABNORMAL_A = iif([tbl_c.CURRENT]=[tbl_c.NORMAL],'N','Y') " & _
        "WHERE tbl_c.LEVELA Is Not Null;"
MyDb.Execute MySQL

'qry_h
MySQL = "UPDATE tbl_a " & _
        "INNER JOIN tbl_c ON tbl_a.LEVELA = tbl_c.LEVELA " & _
        "SET tbl_a.ABNORMAL_A = tbl_c.ABNORMAL_A " & _
        "WHERE tbl_a.LEVELA Is Not Null;"
MyDb.Execute MySQL

'qry_i
MySQL = "INSERT INTO tbl_d (LEVELA, LEVELB, AMOUNT, CURRENT, NORMAL, ABNORMAL_A, ABNORMAL_B) " & _
        "SELECT tbl_a.LEVELA, tbl_a.LEVELB, tbl_a.AMOUNT, tbl_a.CURRENT, tbl_a.NORMAL, tbl_a.ABNORMAL_A, tbl_a.ABNORMAL_B " & _
        "FROM tbl_a " & _
        "WHERE tbl_a.ABNORMAL_A = 'Y' AND tbl_a.ABNORMAL_B = 'Y';"
MyDb.Execute MySQL

DoCmd.Hourglass False

MsgBox "xxx", vbOKOnly, MsgTitleImport

DoCmd.SetWarnings True

End Sub

查询

SELECT tbl_b.LEVELA, Sum(tbl_a.AMOUNT) AS AMOUNT
FROM tbl_a INNER JOIN tbl_b ON tbl_a.LEVELA = tbl_b.LEVELA
WHERE (((tbl_b.LEVELA) Is Not Null))
GROUP BY tbl_b.LEVELA;

输出

Output

我有几个问题:

  1. 是否可以将单独的查询(qry_d)添加到模块代码中?

  2. 此代码是否有更好的格式?这是我建立的第一个数据库,因此我不确定“正确”完成后的外观。

预先感谢您的帮助。

0 个答案:

没有答案