9milla
Programmer
- Nov 28, 2007
- 12
I've been using the following section of code using access adp in vba with sql 2005 as the current connection. Am wanting to post the values to the current db based on certain events within the application.
Private Sub BtnUpdate1_Click()
'On Error GoTo Err_BtnUpdate1_Click
Dim aradb As Connection
Dim comrst As ADODB.Recordset
Dim rst As ADODB.Recordset
Dim DbRst As ADODB.Recordset
Dim SqlRst As ADODB.Recordset
Dim strDbRst As String
Dim StrYear As String
Dim StrMonth As String
Dim StrDate As String
Dim strCurDatePart As String
Dim strSQL As String
Dim strSQLrst As String
Dim strRstDate As String
Set aradb = CurrentProject.Connection
Set comrst = New ADODB.Recordset
Set rst = New ADODB.Recordset
Set DbRst = New ADODB.Recordset
Set SqlRst = New ADODB.Recordset
If IsNull(Me![Comment]) Then
MsgBox ("Please enter a valid Comment!")
Me![Comment].SetFocus
GoTo Exit_BtnUpdate1_Click
End If
If Me![Comment] = "PTPH" Or Me![Comment] = "PTPC" Or Me![Comment] = "PTPW" Then
If IsNull(Me![PTP DATE]) Then
MsgBox ("PTP Date must have a valid date in It. Please Correct!")
Me![PTPDate].SetFocus
GoTo Exit_BtnUpdate1_Click
End If
If IsNull(Me![PTP AMOUNT]) Or Me![PTP AMOUNT] < 1 Then
Me!ptpamt.SetFocus
MsgBox ("PTP Amount must have a value. Please Correct! ")
GoTo Exit_BtnUpdate1_Click
End If
End If
If (Comment = "ADM") Or (Comment = "HLC") Or (Comment = "PTPH") Or (Comment = "PTPW") Or (Comment = "QRY") Or (Comment = "RTP") Or (Comment = "PTPC") Or (Comment = "PTPH-NS") Or (Comment = "PTPW-NS") Or (Comment = "PTPC-NS") Or (Comment = "PTP-SUP") Then
Me.txtLetterCharge = "12.60"
Else: Me.txtLetterCharge = "0.00"
End If
'
'¯¯¯¯¯»» Checks if Date is valid and within the same ««¯¯¯¯¯'
'_____»» month and adds exp charges to new debtors ««_____'
strSQL = "SELECT ClaimNo, MAX(Date) AS Date, MAX(Comment) AS comment FROM dbo.[Comments Table] WHERE (Comment IN (N'qry', N'ptph', N'rtp', N'adm', N'ptpw', N'ptpc', N'ptph-ns', N'ptpw-ns', N'ptpc-ns', N'ptp-sup')) GROUP BY ClaimNo HAVING (ClaimNo = " & Me![ClaimNo] & ")"
rst.Open strSQL, aradb, adOpenDynamic, adLockOptimistic
If (rst.RecordCount <= 0) Then
strSQLrst = "Select * from [Payments Table] where 1 = 2"
SqlRst.Open strSQLrst, aradb, adOpenDynamic, adLockOptimistic
SqlRst.AddNew
SqlRst!ClaimNo = Me.ClaimNo
SqlRst!ClientCode = Me.ClientCode
SqlRst!FirstAccNo = Me.FirstAccNo
SqlRst!PayRefDesc = "EXPSUB " & Me.Comment
SqlRst!Value = Me.txtLetterCharge
SqlRst!Date = Format(Now(), "dd/mm/yyyy")
SqlRst!TransactionDate = Format(Now(), "dd/mm/yyyy")
SqlRst.Update
SqlRst.Close
strDbRst = "SELECT * FROM dbo.Debtors WHERE (ClaimNo = " & Me![ClaimNo] & ")"
DbRst.Open strDbRst, aradb, adOpenDynamic, adLockOptimistic
DbRst.AddNew
DbRst![Expenses] = Me.[Expenses]
DbRst![Total Expenses] = Me.[Total Expenses] + (Me.[Expenses] * 0.14)
DbRst.Update
DbRst.Close
'Me.[Total Expenses] = "(12.60 * 0.14)"
Else:
strCurDatePart = Right((Format(Now(), "dd MM yyyy")), 7)
If IsDate(rst!Date) Then
strRstDate = Format(rst!Date, "MM yyyy")
If (strCurDatePart = strRstDate) Then
MsgBox "The debtor has already been charged", vbInformation + vbExclamation + vbOKOnly
Me.txtLetterCharge = ""
ElseIf (strCurDatePart <> strRstDate) Then
strSQLrst = "Select * from [Payments Table] where 1 = 2"
SqlRst.Open strSQLrst, aradb, adOpenDynamic, adLockOptimistic
SqlRst.AddNew
SqlRst!ClaimNo = Me.ClaimNo
SqlRst!ClientCode = Me.ClientCode
SqlRst!FirstAccNo = Me.FirstAccNo
SqlRst!PayRefDesc = "EXPSUB " & Me.Comment
SqlRst!Value = Me.txtLetterCharge
SqlRst!Date = Format(Now(), "dd/mm/yyyy")
SqlRst!TransactionDate = Format(Now(), "dd/mm/yyyy")
SqlRst.Update
SqlRst.Close
End If
Else:
MsgBox "TEST"
End If
End If
'Me.[Total Expenses] = Me.[Total Expenses] + ([Expenses] + [Expenses] * 0.14)
comrst.Open "Select * from [Comments Table] where 1 = 2", aradb, adOpenDynamic, adLockOptimistic
'_________________________
'Update Comments Table
'-------------------------
comrst.AddNew
comrst![ClaimNo] = Me![ClaimNo]
comrst![Date] = Date
comrst![RefNo] = Me![CollectorNo]
comrst![Comment] = Me![Comment]
comrst![Remark1] = Me![Remarks]
comrst![PTPDate] = Me![PTPDate]
comrst![PTPAmount] = Me![ptpamt]
comrst.Update
If IsNull(Me![ReviewDate]) = False Then
If IsNull(Me![ARAPTPCount]) Then
Me![ARAPTPCount] = 1
Else
Me![ARAPTPCount] = Me![ARAPTPCount] + 1
End If
End If
If Me![PTPDate] >= Date Then
Me![ReviewDate] = Me![PTPDate] + 2
End If
Me!DateWorked = Now
DoCmd.Close
Exit_BtnUpdate1_Click:
Exit Sub
Err_BtnUpdate1_Click:
MsgBox Err.Description
Resume Exit_BtnUpdate1_Click
End Sub
This results in an error whereby erorr trapping cannot determine whether the recordset [Total Expenses] is being calculated. Has this someting to do with the fact that there is a space in the original table of the header colunm (Total Expenses)? If so, how would I overcome this problem. This was set up by a previous Administrator and the colunm contains millions of records. Changing the name is also not an option as other applications use this field as well as stored procedures and various queries.
Any help would be appreciated.
Thanks
Private Sub BtnUpdate1_Click()
'On Error GoTo Err_BtnUpdate1_Click
Dim aradb As Connection
Dim comrst As ADODB.Recordset
Dim rst As ADODB.Recordset
Dim DbRst As ADODB.Recordset
Dim SqlRst As ADODB.Recordset
Dim strDbRst As String
Dim StrYear As String
Dim StrMonth As String
Dim StrDate As String
Dim strCurDatePart As String
Dim strSQL As String
Dim strSQLrst As String
Dim strRstDate As String
Set aradb = CurrentProject.Connection
Set comrst = New ADODB.Recordset
Set rst = New ADODB.Recordset
Set DbRst = New ADODB.Recordset
Set SqlRst = New ADODB.Recordset
If IsNull(Me![Comment]) Then
MsgBox ("Please enter a valid Comment!")
Me![Comment].SetFocus
GoTo Exit_BtnUpdate1_Click
End If
If Me![Comment] = "PTPH" Or Me![Comment] = "PTPC" Or Me![Comment] = "PTPW" Then
If IsNull(Me![PTP DATE]) Then
MsgBox ("PTP Date must have a valid date in It. Please Correct!")
Me![PTPDate].SetFocus
GoTo Exit_BtnUpdate1_Click
End If
If IsNull(Me![PTP AMOUNT]) Or Me![PTP AMOUNT] < 1 Then
Me!ptpamt.SetFocus
MsgBox ("PTP Amount must have a value. Please Correct! ")
GoTo Exit_BtnUpdate1_Click
End If
End If
If (Comment = "ADM") Or (Comment = "HLC") Or (Comment = "PTPH") Or (Comment = "PTPW") Or (Comment = "QRY") Or (Comment = "RTP") Or (Comment = "PTPC") Or (Comment = "PTPH-NS") Or (Comment = "PTPW-NS") Or (Comment = "PTPC-NS") Or (Comment = "PTP-SUP") Then
Me.txtLetterCharge = "12.60"
Else: Me.txtLetterCharge = "0.00"
End If
'
'¯¯¯¯¯»» Checks if Date is valid and within the same ««¯¯¯¯¯'
'_____»» month and adds exp charges to new debtors ««_____'
strSQL = "SELECT ClaimNo, MAX(Date) AS Date, MAX(Comment) AS comment FROM dbo.[Comments Table] WHERE (Comment IN (N'qry', N'ptph', N'rtp', N'adm', N'ptpw', N'ptpc', N'ptph-ns', N'ptpw-ns', N'ptpc-ns', N'ptp-sup')) GROUP BY ClaimNo HAVING (ClaimNo = " & Me![ClaimNo] & ")"
rst.Open strSQL, aradb, adOpenDynamic, adLockOptimistic
If (rst.RecordCount <= 0) Then
strSQLrst = "Select * from [Payments Table] where 1 = 2"
SqlRst.Open strSQLrst, aradb, adOpenDynamic, adLockOptimistic
SqlRst.AddNew
SqlRst!ClaimNo = Me.ClaimNo
SqlRst!ClientCode = Me.ClientCode
SqlRst!FirstAccNo = Me.FirstAccNo
SqlRst!PayRefDesc = "EXPSUB " & Me.Comment
SqlRst!Value = Me.txtLetterCharge
SqlRst!Date = Format(Now(), "dd/mm/yyyy")
SqlRst!TransactionDate = Format(Now(), "dd/mm/yyyy")
SqlRst.Update
SqlRst.Close
strDbRst = "SELECT * FROM dbo.Debtors WHERE (ClaimNo = " & Me![ClaimNo] & ")"
DbRst.Open strDbRst, aradb, adOpenDynamic, adLockOptimistic
DbRst.AddNew
DbRst![Expenses] = Me.[Expenses]
DbRst![Total Expenses] = Me.[Total Expenses] + (Me.[Expenses] * 0.14)
DbRst.Update
DbRst.Close
'Me.[Total Expenses] = "(12.60 * 0.14)"
Else:
strCurDatePart = Right((Format(Now(), "dd MM yyyy")), 7)
If IsDate(rst!Date) Then
strRstDate = Format(rst!Date, "MM yyyy")
If (strCurDatePart = strRstDate) Then
MsgBox "The debtor has already been charged", vbInformation + vbExclamation + vbOKOnly
Me.txtLetterCharge = ""
ElseIf (strCurDatePart <> strRstDate) Then
strSQLrst = "Select * from [Payments Table] where 1 = 2"
SqlRst.Open strSQLrst, aradb, adOpenDynamic, adLockOptimistic
SqlRst.AddNew
SqlRst!ClaimNo = Me.ClaimNo
SqlRst!ClientCode = Me.ClientCode
SqlRst!FirstAccNo = Me.FirstAccNo
SqlRst!PayRefDesc = "EXPSUB " & Me.Comment
SqlRst!Value = Me.txtLetterCharge
SqlRst!Date = Format(Now(), "dd/mm/yyyy")
SqlRst!TransactionDate = Format(Now(), "dd/mm/yyyy")
SqlRst.Update
SqlRst.Close
End If
Else:
MsgBox "TEST"
End If
End If
'Me.[Total Expenses] = Me.[Total Expenses] + ([Expenses] + [Expenses] * 0.14)
comrst.Open "Select * from [Comments Table] where 1 = 2", aradb, adOpenDynamic, adLockOptimistic
'_________________________
'Update Comments Table
'-------------------------
comrst.AddNew
comrst![ClaimNo] = Me![ClaimNo]
comrst![Date] = Date
comrst![RefNo] = Me![CollectorNo]
comrst![Comment] = Me![Comment]
comrst![Remark1] = Me![Remarks]
comrst![PTPDate] = Me![PTPDate]
comrst![PTPAmount] = Me![ptpamt]
comrst.Update
If IsNull(Me![ReviewDate]) = False Then
If IsNull(Me![ARAPTPCount]) Then
Me![ARAPTPCount] = 1
Else
Me![ARAPTPCount] = Me![ARAPTPCount] + 1
End If
End If
If Me![PTPDate] >= Date Then
Me![ReviewDate] = Me![PTPDate] + 2
End If
Me!DateWorked = Now
DoCmd.Close
Exit_BtnUpdate1_Click:
Exit Sub
Err_BtnUpdate1_Click:
MsgBox Err.Description
Resume Exit_BtnUpdate1_Click
End Sub
This results in an error whereby erorr trapping cannot determine whether the recordset [Total Expenses] is being calculated. Has this someting to do with the fact that there is a space in the original table of the header colunm (Total Expenses)? If so, how would I overcome this problem. This was set up by a previous Administrator and the colunm contains millions of records. Changing the name is also not an option as other applications use this field as well as stored procedures and various queries.
Any help would be appreciated.
Thanks