简体   繁体   中英

VBA Access Code Review Request

I would like to get a general code review on the following code. This code is part of an automated decision making process to determine which entires need to be added to a table in the database. I would just like to have another pair of eyes to check for Syntax errors and and general logic flaws.

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

I am getting an error on the following line of code Set DKClient = disRs.Fields("Client") it says Compile Error Object required But I have the object declared already.

The reason you are getting that error is because you are declaring DKClient and DKNum as strings but then trying to assign DAO.Field objects to them.

Change the following lines:

 
 
 
  
  Dim DKClient As String Dim DKNum As String
 
  

To:

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

That said, it's not entirely clear the purpose of those variables from the code you posted. They appear to be unused.

The fix I proposed should get your code to compile, but you will probably want to ask additional, targeted questions to get to where you are trying to go.

UPDATE
I see that you are attempting to use those variables as part of the SQL strings you are executing via DoCmd.RunSQL . As @HansUp seems to have correctly surmised, what you really want is the value of those fields. So change your lines from:

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

To:

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

Next, I would strongly advise that you use CurrentDb.Execute "{YOUR SQL HERE}", dbFailOnError instead of DoCmd.RunSQL .

Also, you need to concatenate your String variables using & instead of simply writing them into the string in your ensuing lines of code.

I don't want to get myself sucked any further down this particular rabbit hole today, so I will stop there and wish you luck in sorting out the rest.

DKClient is a string variable.

Dim DKClient As String

Later, you attempt to assign it a value using Set .

Set DKClient = disRs.Fields("Client")

However, Set can only be used with an object variable, which explains the object required compile error. Use a simple assignment. .Value is the default property, so not required, but try this way first anyway ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM