I'm not new to coding but I can say that my weak point is error handling. I have a text field in a form for an account number in this format "##-####.#" I'm trying to handle a couple of different errors.
This all occurs on the After Update event. Maybe a different event should be used?
I check the length of the text that was keyed.
I run a query to look the account number up in a table.
if it doesn't exist I want to undo what was keyed and set the focus back in that field. I can't get undo to work or get the focus back to that field?
Actual Code:
Dim db As Database
Dim rst As Recordset
Set db = CurrentDb()
DoCmd.SetWarnings (0)
If Len(txtAccount.Text) <> 9 Then
MsgBox "Please follow the Account Number Convention"
txtAccount.SetFocus
Exit Sub
End If
Select Case Right(txtAccount.Text, 1)
Case 9
Case 0
Case Else
MsgBox "The last digit in your account must be 9 or 0"
txtAccount.SetFocus
End Select
If Right(txtAccount.Text, 1) = 9 Then
DoCmd.OpenQuery "q_tbl_AccountLookup3"
Else
DoCmd.OpenQuery "q_tbl_AccountLookup2"
End If
Set rst = db.OpenRecordset("Mktbl_AccountLookup"
On Error GoTo ErrHandler:
rst.MoveFirst
MyInvID = rst![InvIDN]
MyPartID = rst![PartIDN]
MyInvName = rst![PartnershipInvestorName]
MyPartnershipName = rst![PartnershipName]
rst.Close
MyAccountNumber = txtAccount.Text
MyGlobalAccountNumber = MyAccountNumber
ErrHandler:
If Err.Number = 3021 Then
MsgBox "The Account Number You entered is invalid" & Chr(10) _
& "It doesn't exist in tbl_PartnershipMembers" & Chr(10) _
& "Correct this immediately!" & " " & Err.Number & Chr(10) _
& Err.Description
txtAccount.Undo
txtAccount.SetFocus
End If
Exit Sub
If Len(txtAccount.Text) <> 9 Then
MsgBox "Please follow the Account Number Convention"
txtAccount.SetFocus
Else
MyPrefix = Left(txtAccount, 2)
Call AutoSaveFiller
End If
txtTransactionDate.SetFocus
End Sub
This all occurs on the After Update event. Maybe a different event should be used?
I check the length of the text that was keyed.
I run a query to look the account number up in a table.
if it doesn't exist I want to undo what was keyed and set the focus back in that field. I can't get undo to work or get the focus back to that field?
Actual Code:
Dim db As Database
Dim rst As Recordset
Set db = CurrentDb()
DoCmd.SetWarnings (0)
If Len(txtAccount.Text) <> 9 Then
MsgBox "Please follow the Account Number Convention"
txtAccount.SetFocus
Exit Sub
End If
Select Case Right(txtAccount.Text, 1)
Case 9
Case 0
Case Else
MsgBox "The last digit in your account must be 9 or 0"
txtAccount.SetFocus
End Select
If Right(txtAccount.Text, 1) = 9 Then
DoCmd.OpenQuery "q_tbl_AccountLookup3"
Else
DoCmd.OpenQuery "q_tbl_AccountLookup2"
End If
Set rst = db.OpenRecordset("Mktbl_AccountLookup"
On Error GoTo ErrHandler:
rst.MoveFirst
MyInvID = rst![InvIDN]
MyPartID = rst![PartIDN]
MyInvName = rst![PartnershipInvestorName]
MyPartnershipName = rst![PartnershipName]
rst.Close
MyAccountNumber = txtAccount.Text
MyGlobalAccountNumber = MyAccountNumber
ErrHandler:
If Err.Number = 3021 Then
MsgBox "The Account Number You entered is invalid" & Chr(10) _
& "It doesn't exist in tbl_PartnershipMembers" & Chr(10) _
& "Correct this immediately!" & " " & Err.Number & Chr(10) _
& Err.Description
txtAccount.Undo
txtAccount.SetFocus
End If
Exit Sub
If Len(txtAccount.Text) <> 9 Then
MsgBox "Please follow the Account Number Convention"
txtAccount.SetFocus
Else
MyPrefix = Left(txtAccount, 2)
Call AutoSaveFiller
End If
txtTransactionDate.SetFocus
End Sub