smtazulislam
Member
- Local time
- Today, 17:40
- Joined
- Mar 27, 2020
- Messages
- 808
'Get "Run-Time error" this line, not execute sql'
Code:
CurrentDb.Execute strSql
CurrentDb.Execute strSql
Private Sub cmdUpdate_Click()
If Me.Dirty Then Me.Dirty = False
Dim CrId As Integer
Dim StrEmployeeID As Long
Dim StrYear As Long
Dim StrMonth As Long
Dim StrGrossSalary As Double
Dim StrTotalAllowances As Double
Dim StrTotalAbsentdays As lonh
Dim StrTotalDeductions As Double
Dim StrTotalOvertime As Double
Dim StrCashAdvance As Double
Dim StrSQL As String
CrId = Me.CurrentRecord
StrEmployeeID = Me.txtEmployeeID
StrYear = Me.cboYear
StrMonth = Me.CboMonth
StrGrossSalary = [Forms]![frmPayrollMainForm]![SubfrmEmployeeSalaries].[Form]![txtGS]
StrTotalAllowances = [Forms]![frmPayrollMainForm]![SubfrmAllowanceData].[Form]![txtTotalAllowances]
StrTotalAbsentdays = [Forms]![frmPayrollMainForm]![SubfrmAbsentDay].[Form]![txtTotalAbsent]
StrTotalDeductions = [Forms]![frmPayrollMainForm]![SubfrmDeductionProcedure].[Form]![txtTotalDeductions]
StrTotalOvertime = [Forms]![frmPayrollMainForm]![SubfrmOverTime].[Form]![txtTotalOverTime]
StrCashAdvance = [Forms]![frmPayrollMainForm]![SubfrmCashAdvance].[Form]![txtTotalCashAdvance]
StrSQL = "UPDATE INTO tblPayroll SET PayrollYear = p0, PayrollMonth = p1, WorkedDays = p2, GrossSalary = p3, TotalAllowances = p4, TotalAbsentdays = p5, TotalDeductions = p6, TotalOvertime = p7, CashAdvance = p8 " & _
"WHERE EmployeeID = p9;"
'StrSQL = "UPDATE INTO tblPayroll SET PayrollYear = " & StrYear & ", PayrollMonth = " & StrMonth & ", WorkedDays = " & Me.txtDayOfMonth & ", GrossSalary = " & StrGrossSalary & ", TotalAllowances = " & StrTotalAllowances & ", TotalAbsentdays = " & StrTotalAbsentdays & ", TotalDeductions = " & StrTotalDeductions & ", TotalOvertime = " & StrTotalOvertime & ", CashAdvance = " & StrCashAdvance & "" & _
' "WHERE EmployeeID = " & StrEmployeeID & ";"
With CurrentDb.CreateQueryDef(vbNullString, StrSQL)
.Parameters("p0") = StrYear
.Parameters("p1") = StrMonth
.Parameters("p2") = Me.txtDayOfMonth
.Parameters("p3") = StrGrossSalary
.Parameters("p4") = StrTotalAllowances
.Parameters("p5") = StrTotalAbsentdays
.Parameters("p6") = StrTotalDeductions
.Parameters("p7") = StrTotalOvertime
.Parameters("p8") = StrCashAdvance
.Parameters("p9") = StrEmployeeID
.Execute dbFailOnError
End With
MsgBox "Enter your Data have been updated in the table ", vbInformation + vbOKOnly, "Update Info"
'Debug.Print StrSQL
'CurrentDb.Execute StrSQL, dbFailOnError
DoCmd.GoToRecord , , acGoTo, CrId
End Sub
If Me.Dirty Then Me.Dirty = False
Dim CrId As Integer
Dim StrGrossSalary As Double
Dim StrTotalAllowances As Double
Dim StrTotalAbsentdays As Double
Dim StrTotalDeductions As Double
Dim StrTotalOvertime As Double
Dim StrCashAdvance As Double
Dim StrSQL As String
CrId = Me.CurrentRecord
StrGrossSalary = [Forms]![frmPayrollMainForm]![SubfrmEmployeeSalaries].[Form]![txtGS]
StrTotalAllowances = [Forms]![frmPayrollMainForm]![SubfrmAllowanceData].[Form]![txtTotalAllowances]
StrTotalAbsentdays = [Forms]![frmPayrollMainForm]![SubfrmAbsentDay].[Form]![txtTotalAbsent]
StrTotalDeductions = [Forms]![frmPayrollMainForm]![SubfrmDeductionProcedure].[Form]![txtTotalDeductions]
StrTotalOvertime = [Forms]![frmPayrollMainForm]![SubfrmOverTime].[Form]![txtTotalOverTime]
StrCashAdvance = [Forms]![frmPayrollMainForm]![SubfrmCashAdvance].[Form]![txtTotalCashAdvance]
StrSQL = "INSERT INTO tblPayroll SET PayrollYear = p0, PayrollMonth = p1, WorkedDays = p2, GrossSalary = p3, TotalAllowances = p4, TotalAbsentdays = p5, TotalDeductions = p6, TotalOvertime = p7, CashAdvance = p8 " & _
"WHERE EmployeeID = p9;"
With CurrentDb.CreateQueryDef(vbNullString, StrSQL)
.Parameters("p0") = Me.cboYear 'Main form
.Parameters("p1") = Me.CboMonth 'Main form
.Parameters("p2") = Me.txtDayOfMonth 'Main form
.Parameters("p3") = StrGrossSalary '(Sub)form
.Parameters("p4") = StrTotalAllowances '(Sub)form
.Parameters("p5") = StrTotalAbsentdays '(Sub)form
.Parameters("p6") = StrTotalDeductions '(Sub)form
.Parameters("p7") = StrTotalOvertime '(Sub)form
.Parameters("p8") = StrCashAdvance '(Sub)form
.Parameters("p9") = Me.txtEmployeeID 'Main form
.Execute dbFailOnError
End With
MsgBox "Enter your Data have been updated in the table ", vbInformation + vbOKOnly, "Update Info"
DoCmd.GoToRecord , , acGoTo, CrId
Why I try to change previous "String Variable", because those 3 fields is from "mainform"..
Still have "Run time error 3134"
See attached current picture.
If Me.Dirty Then Me.Dirty = False
Dim CrId As Long
Dim StrGrossSalary As Variant
Dim StrTotalAllowances As Variant
Dim StrTotalAbsentdays As Variant
Dim StrTotalDeductions As Variant
Dim StrTotalOvertime As Variant
Dim StrCashAdvance As Variant
Dim StrSQL As String
Dim db As DAO.Database
Set db = Currentdb
CrId = Me.CurrentRecord
StrGrossSalary = Me![SubfrmEmployeeSalaries][txtGS]
StrTotalAllowances = Me![SubfrmAllowanceData]![txtTotalAllowances]
StrTotalAbsentdays = Me![SubfrmAbsentDay]![txtTotalAbsent]
StrTotalDeductions = Me![SubfrmDeductionProcedure]![txtTotalDeductions]
StrTotalOvertime = Me![SubfrmOverTime]![txtTotalOverTime]
StrCashAdvance = Me![SubfrmCashAdvance]![txtTotalCashAdvance]
StrSQL = "INSERT INTO tblPayroll SET PayrollYear = p0, PayrollMonth = p1, WorkedDays = p2, GrossSalary = p3, TotalAllowances = p4, TotalAbsentdays = p5, TotalDeductions = p6, TotalOvertime = p7, CashAdvance = p8 " & _
"WHERE EmployeeID = p9;"
With Db.CreateQueryDef(vbNullString, StrSQL)
.Parameters("p0") = Me.cboYear 'Main form
.Parameters("p1") = Me.CboMonth 'Main form
.Parameters("p2") = Me.txtDayOfMonth 'Main form
.Parameters("p3") = StrGrossSalary '(Sub)form
.Parameters("p4") = StrTotalAllowances '(Sub)form
.Parameters("p5") = StrTotalAbsentdays '(Sub)form
.Parameters("p6") = StrTotalDeductions '(Sub)form
.Parameters("p7") = StrTotalOvertime '(Sub)form
.Parameters("p8") = StrCashAdvance '(Sub)form
.Parameters("p9") = Me.txtEmployeeID 'Main form
.Execute dbFailOnError
End With
MsgBox "Enter your Data have been updated in the table ", vbInformation + vbOKOnly, "Update Info"
DoCmd.GoToRecord , , acGoTo, CrId
Set db = Nothing
Post # 6 confirms that the OP is trying to update one table with data that is in other tables: Not a good practice.@arnelgp That syntax is off - you don't use a SET clause in an Insert query, only an Update?
It doesn't help that the OP seems to have switched them around at various points.
Thank you so much for your reply.Why would you want to have the same data in different tables? Is the data time-based? You really need to read up on normalization.
Thank you so much for correction again,add a Database variable:
Code:If Me.Dirty Then Me.Dirty = False Dim CrId As Long Dim StrGrossSalary As Variant Dim StrTotalAllowances As Variant Dim StrTotalAbsentdays As Variant Dim StrTotalDeductions As Variant Dim StrTotalOvertime As Variant Dim StrCashAdvance As Variant Dim StrSQL As String Dim db As DAO.Database Set db = Currentdb CrId = Me.CurrentRecord StrGrossSalary = Me![SubfrmEmployeeSalaries][txtGS] StrTotalAllowances = Me![SubfrmAllowanceData]![txtTotalAllowances] StrTotalAbsentdays = Me![SubfrmAbsentDay]![txtTotalAbsent] StrTotalDeductions = Me![SubfrmDeductionProcedure]![txtTotalDeductions] StrTotalOvertime = Me![SubfrmOverTime]![txtTotalOverTime] StrCashAdvance = Me![SubfrmCashAdvance]![txtTotalCashAdvance] StrSQL = "INSERT INTO tblPayroll SET PayrollYear = p0, PayrollMonth = p1, WorkedDays = p2, GrossSalary = p3, TotalAllowances = p4, TotalAbsentdays = p5, TotalDeductions = p6, TotalOvertime = p7, CashAdvance = p8 " & _ "WHERE EmployeeID = p9;" With Db.CreateQueryDef(vbNullString, StrSQL) .Parameters("p0") = Me.cboYear 'Main form .Parameters("p1") = Me.CboMonth 'Main form .Parameters("p2") = Me.txtDayOfMonth 'Main form .Parameters("p3") = StrGrossSalary '(Sub)form .Parameters("p4") = StrTotalAllowances '(Sub)form .Parameters("p5") = StrTotalAbsentdays '(Sub)form .Parameters("p6") = StrTotalDeductions '(Sub)form .Parameters("p7") = StrTotalOvertime '(Sub)form .Parameters("p8") = StrCashAdvance '(Sub)form .Parameters("p9") = Me.txtEmployeeID 'Main form .Execute dbFailOnError End With MsgBox "Enter your Data have been updated in the table ", vbInformation + vbOKOnly, "Update Info" DoCmd.GoToRecord , , acGoTo, CrId Set db = Nothing
Thanks for your reply.All of this manoeuvring is completely unnecessary.
You have these numbers and therefore can easily create a query to sum them and display them, if any data changes your sums will always be correct - your separate table with the saved values won't be without running a load of updates to maintain it.
If you have null values you can NZ([YourSumField],0) and it will display a zero. Simple.
Well said. I have been in that boat as well and often wondered "WHY won't they just answer my question instead of telling me about how my understanding of Access is terrible?"Well.. I was quite angry on others in the past some times when I was told that my approach is not... lets say appropriate.
Thank you so much for your advice. appreciate for expense more time for me.Sorry to say that... I have been programming a lot of Payrolls and it is sometimes hard to sort that all out - but I have identified for myself some points about structuring such data that actual works in all countries I have been working. From that point of view I would strongly recommend to normalize the data correctly (which seems not to be the case in your approach). I have run several times in the last 30 years into situations, where I deleted a lot of Forms and work to start from scratch to get it right - cause my experience is that is i compromise at one point with the data structure it keeps me haunting. One of the leading questions is always: How is the actual workflow BEFORE putting it in the system - how SHOULD it be optimized and processed within a system. In my early years i made some mistakes by trying to put 1 to 1 existing workflows in a database system - this never worked out for me - and I always tell my clients to look at the workflow FIRST - then optimize it - then program it.
I usually need only 2 tables: Employees - and the payroll. In the payroll form that is loaded every month from the data in the employees table i have all functions to calculate NET Salaries (with different Taxation approaches, National Security Fonds for Health, old age etc.).
To give only one example: You primary key in the payroll is wrong. If it is the fact that you have a monthly payroll - the primary key need to be the Employee-ID and the MONTH together.
In the employee table I have ALL information for processing "normal" monthly salaries - meaning:
Taxrates, Children/Spouses etc. In this case I have a tax-allowance BEFORE Tax and a special Tax allowance that is deducted from the calculated TAX.
Because. after 2 years finished contract some worker take work benefit money AND some worker take benefit money when she/he go to final exit (Resign). So that all of the Cut Salary calculation we have to showed to worker. Becuase some of worker have letter to deduction his/her absent from years Medical Leave. So we should have this absent with MC AND absent without MC THEN absent with Dudection from Yearly Medical Leave.
Actually, I should keep the history of the employee mainly with contractSalary, Absent, OverTime, and CashAdvance etc.OK - so 2 points:
POINT1: Can you make a flow chart of the process leading to the monthly information (what is static - depending on the contract - and what has to be checked on every month? As we say "the devil is in the details..." and I need to understand when what information is accessible that leads to the payroll. Some points seem to be easy with lookup-tables (contracts: 1st 5%, 2nd 7% .. 3rd... etc.). This still would be a part of the employee table...
POINT2: You heard about the Pareto principle? It says the first 20% effort you put into something generates 80% of the result... and if you need 100% it will cost a lot of work... Meaning: sometimes its easier to go to the core issue and leave some factors outside (for the time being... like putting things on paper etc... and we discover later that there is no need to include them in programming - or: we do it and know we are heading for the the last 20% and this will cost time).
My main approach to simplify is: I have only the ACTUAL situation in the employee table. Once it changes i overwrite the old data and update it to the new situation. I don't care for the history here, cause the history is done by the payroll. You can make views on the payroll seeing for example: how many contracts did he have so far, how many years did he work, when did he get paid what... The salary slips are a report based on the payroll - and because all data is there you have no need to program a lot.
I will try to put that together in a test dba - give me some time...
By the way: Don't bother English is not my mother tongue either ( I was born in Germany...) - but live in Asia...
I understand: You have a basic salary based on a contract - based on what kind of contract you have the employee gets some % PLUS on the basic salary. Before resigning there is some kind of benefit ( we call that severance pay - here its outside the tax -so I have taxable and non-taxable parts of the salary...). I don't understand the "Letter of deduction": Does that mean the worker gets paid ALTHOUGH he was absent - and in some cases (when no letter is there) the days of absence get DEDUCTED from the gross salary?
Sorry... I still have more questions than answers..