VBA访问代码审查请求

时间:2013-12-24 18:29:42

标签: vba ms-access access-vba

我想对以下代码进行一般性代码审查。 此代码是自动决策制定过程的一部分,用于确定需要将哪些要素添加到数据库中的表中。我只想用另一双眼睛检查语法错误和一般逻辑缺陷。

Private Sub btnAddEnt_Click()
Dim strEnt As String
Dim Direct As Integer
Dim Indirect As Integer
Dim DirectNum As Currency
Dim IndirectNum As Currency
Dim db As Database
Set db = CurrentDb
Dim disRs As DAO.Recordset
Dim dayRs As DAO.Recordset
Dim Jan As Currency
Dim Feb As Currency
Dim Mar As Currency
Dim Apr As Currency
Dim May As Currency
Dim Jun As Currency
Dim Jul As Currency
Dim Aug As Currency
Dim Sep As Currency
Dim Oct As Currency
Dim Nov As Currency
Dim Dec As Currency
Dim DKClient As String
Dim DKNum As Integer
strEnt = "INSERT INTO EntList (EntityID, BusinessUnit, EntityName, Position, Location, Client, Dept, DistKey, Salary, Currency, SQ&A, BillRate, Util%, MeritDate, MeritRate) " & _
         "VALUES ('" & Me.EntityID & "', '" & Me.BusinessUnit & "', '" & Me.EntityName & "', '" & Me.Position & "', '" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '" & Me.DistKey & "', '" & Me.Salary & "', '" & Me.Currency & "', '" & Me.SG_A & "', '" & Me.BillRate & "', '" & Me.Util_ & "', '" & Me.MeritDate & "', '" & Me.Merit_ & "');"
DoCmd.RunSQL strEnt
Direct = (1 - Me.SG_A)
Indirect = Me.SG_A
If Direct > 0 Then
    DirectNum = Direct * Me.Salary / 12
End If
If Indirect > 0 Then
    IndirectNum = Indirect * Me.Salary / 12
End If
If Me.BillRate > 0 Then
    Set dayRs = db.OpenRecordset("SELECT WrkDays FROM WrkDays ORDER BY WrkMonth;")
    dayRs.MoveFirst
    Set DayNum = dayRs.Fields("WrkDays")
    While Not dayRs.EOF
        Jan = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Feb = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Mar = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Apr = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        May = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Jun = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Jul = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Aug = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Sep = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Oct = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Nov = (Me.BillRate * DayNum) * Me.Util_
        dayRs.MoveNext
        Dec = (Me.BillRate * DayNum) * Me.Util_
    Wend
End If
If Me.DistKey <> "N/A" Then
    Set disRs = db.OpenRecordset("SELECT Client, DistPer FROM DistMap WHERE DistKey = '" & Me.DistKey & "';")
    disRs.MoveFirst
    Set DKClient = disRs.Fields("Client")
    Set DKNum = disRs.Fields("DistPer")
    While Not disRs.EOF
        If Direct > 0 Then
            DoCmd.RunSQL ("INSERT INTO ForcastTrans (Location, Client, Department, Account, EntityID, Description, Currency, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
                          "VALUES ('" & Me.Location & "', DKClient, '" & Me.Dept & "', '5010 SALARIES/WAGES - ADMIN', '" & Me.EntityID & "', 'Payroll', '" & Me.Currency & "', DirectNum * DKNum, DirectNum * DKNum, DirectNum * DKNum, DirectNum * DKNum, DirectNum * DKNum, DirectNum * DKNum, DirectNum * DKNum, DirectNum * DKNum, DirectNum * DKNum, DirectNum * DKNum, DirectNum * DKNum, DirectNum * DKNum);")
        End If
        If Indirect > 0 Then
            DoCmd.RunSQL ("INSERT INTO ForcastTrans (Location, Client, Department, Account, EntityID, Description, Currency, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
                          "VALUES ('" & Me.Location & "', DKClient, '" & Me.Dept & "', '7010 SALARIES/WAGES - ADMIN', '" & Me.EntityID & "', 'Payroll', '" & Me.Currency & "', IndirectNum*DKNum, IndirectNum * DKNum, IndirectNum * DKNum, IndirectNum * DKNum, IndirectNum * DKNum, IndirectNum * DKNum, IndirectNum * DKNum, IndirectNum * DKNum, IndirectNum * DKNum, IndirectNum * DKNum, IndirectNum * DKNum, IndirectNum * DKNum);")
        End If
        If Me.BillRate > 0 Then
            DoCmd.RunSQL ("INSERT INTO ForcastTrans (Location, Client, Department, Account, EntityID, Description, Currency, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
                          "VALUES ('" & Me.Location & "', DKClient, '" & Me.Dept & "', '4900 CONSULTING FEES', '" & Me.EntityID & "', 'Revenue', '" & Me.Currency & "', Jan * DKNum, Feb * DKNum, Mar * DKNum, Apr * DKNum, May * DKNum, Jun * DKNum, Jul * DKNum, Aug * DKNum, Sep * DKNum, Oct * DKNum, Nov * DKNum, Dec * DKNum);")
        End If
        disRs.MoveNext
    Wend
Else
    If Direct > 0 Then
        DoCmd.RunSQL ("INSERT INTO ForcastTrans (Location, Client, Department, Account, EntityID, Description, Currency, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
                      "VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '5010 SALARIES/WAGES - ADMIN', '" & Me.EntityID & "', 'Payroll', '" & Me.Currency & "', DirectNum, DirectNum, DirectNum, DirectNum, DirectNum, DirectNum, DirectNum, DirectNum, DirectNum, DirectNum, DirectNum, DirectNum);")
    End If
    If Indirect > 0 Then
        DoCmd.RunSQL ("INSERT INTO ForcastTrans (Location, Client, Department, Account, EntityID, Description, Currency, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
                      "VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '7010 SALARIES/WAGES - ADMIN', '" & Me.EntityID & "', 'Payroll', '" & Me.Currency & "', IndirectNum, IndirectNum, IndirectNum, IndirectNum, IndirectNum, IndirectNum, IndirectNum, IndirectNum, IndirectNum, IndirectNum, IndirectNum, IndirectNum);")
    End If
    If Me.BillRate > 0 Then
        DoCmd.RunSQL ("INSERT INTO ForcastTrans (Location, Client, Department, Account, EntityID, Description, Currency, Month1, Month2, Month3, Month4, Month5, Month6, Month7, Month8, Month9, Month10, Month11, Month12) " & _
                      "VALUES ('" & Me.Location & "', '" & Me.Client & "', '" & Me.Dept & "', '4900 CONSULTING FEES', '" & Me.EntityID & "', 'Revenue', '" & Me.Currency & "', Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);")
    End If
End If

End Sub

我在以下代码行中收到错误    设置DKClient = disRs.Fields(“Client”) 它说需要编译错误对象但我已经声明了对象。

2 个答案:

答案 0 :(得分:1)

您收到该错误的原因是您将DKClientDKNum声明为字符串,然后尝试将DAO.Field对象分配给它们。

更改以下行:

Dim DKClient As String
Dim DKNum As String

要:

Dim DKClient As DAO.Field
Dim DKNum As DAO.Field

那就是说,你发布的代码中那些变量的目的并不完全清楚。它们似乎未被使用。

我提出的修复程序应该让您的代码进行编译,但是您可能想要询问其他的目标问题,以便找到您想去的地方。< /击>

<强>更新
我看到您正在尝试将这些变量用作通过DoCmd.RunSQL执行的SQL字符串的一部分。由于@HansUp似乎已经正确推测,你真正想要的是这些领域的价值。所以改变你的行:

Set DKClient = disRs.Fields("Client")
Set DKNum = disRs.Fields("DistPer")

要:

DKClient = disRs.Fields("Client").Value
DKNum = disRs.Fields("DistPer").Value

接下来,我强​​烈建议您使用CurrentDb.Execute "{YOUR SQL HERE}", dbFailOnError代替DoCmd.RunSQL

此外,您需要使用&连接String变量,而不是简单地将它们写入随后的代码行中的字符串中。

我今天不想让自己在这个特殊的兔子洞里再被吸走,所以我会在那里停下来,祝你好运,整理其余的。

答案 1 :(得分:1)

DKClient是一个字符串变量。

Dim DKClient As String

稍后,您尝试使用Set为其分配值。

Set DKClient = disRs.Fields("Client")

但是,Set只能与对象变量一起使用,这解释了对象所需的编译错误。使用简单的作业。 .Value是默认属性,因此不是必需的,但无论如何首先尝试这种方式......

DKClient = disRs.Fields("Client").Value