Run-time error '2115' (1 Viewer)

Radioactiveduck

New member
Local time
Today, 08:47
Joined
Sep 9, 2009
Messages
9
I have a form with the following fields and data types:

Date: text in "mmm dd, yyyy" format
JAF: checkbox
EFS: checkbox
txt_ChAttn: textbox, generic format
Note: textbox, generic format

Basically, I've written code that does an AfterUpdate whenever JAF or EFS are checked or unchecked. It writes "John Doe" or "Jane Doe" into the txt_ChAttn textbox depending on which one is checked. I get the following vba error whenever it runs, however:

Run-time error '2115':

the macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Microsoft Office Access from saving the data in the field.

Now, I checked the results, and it actually is updating the txt_ChAttn textbox correctly, but it still throws this error every time you check or uncheck the checkboxes. Here is the code for the updates:


Private Sub EFS_AfterUpdate()

If EFS = -1 Then
If JAF = -1 Then
Me.txt_ChAttn.SetFocus
Me.txt_ChAttn.Text = "John Doe, Jane Doe"
Me.Refresh
Else
Me.txt_ChAttn.SetFocus
Me.txt_ChAttn.Text = "John Doe"
Me.Refresh
End If
Else
If JAF = -1 Then
Me.txt_ChAttn.SetFocus
Me.txt_ChAttn.Text = "Jane Doe"
Me.Refresh
Else
Me.txt_ChAttn.SetFocus
Me.txt_ChAttn.Text = ""
Me.Refresh
End If
End If

End Sub

Private Sub JAF_AfterUpdate()

If EFS = -1 Then
If JAF = -1 Then
Me.txt_ChAttn.SetFocus
Me.txt_ChAttn.Text = "John Doe, Jane Doe"
Me.Refresh
Else
Me.txt_ChAttn.SetFocus
Me.txt_ChAttn.Text = "John Doe"
Me.Refresh
End If
Else
If JAF = -1 Then
Me.txt_ChAttn.SetFocus
Me.txt_ChAttn.Text = "Jane Doe"
Me.Refresh
Else
Me.txt_ChAttn.SetFocus
Me.txt_ChAttn.Text = ""
Me.Refresh
End If
End If

End Sub


Depending on which conditions are met, it always highlights the
"Me.txt_ChAttn.Text =" line, regardless of which combination of checked/unchecked the textboxes fall under. Any ideas on what's causing this error, and how I can get rid of it? As I said, it is updating the textbox correctly.

EDIT: I can't seem to force it to post the code with the correct text spacing, so it may look a bit sloppy.
 
Last edited:

boblarson

Smeghead
Local time
Today, 08:47
Joined
Jan 12, 2001
Messages
32,059
1. Get rid of the .Text part on all of it. You don't use the .Text syntax in VBA unless you absolutely have to. The default of controls is the .Value property and so you can just use

Me.txt_ChAttn = "Jane Doe"

as an example.


2. You do not need the Me.Refresh after everything.

So,
Code:
Private Sub EFS_AfterUpdate()

If EFS = -1 Then
   If JAF = -1 Then
      Me.txt_ChAttn = "John Doe, Jane Doe"
   Else
      Me.txt_ChAttn = "John Doe"
   End If
Else
   IF JAF = -1 Then
        Me.txt_ChAttn = "Jane Doe"
   Else
       Me.txt_ChAttn = ""
   End If
End If
 

Atomic Shrimp

Humanoid lifeform
Local time
Today, 16:47
Joined
Jun 16, 2000
Messages
1,954
Nice username :)

Maybe it's just me, but I think the repetition in those condition tests indicates they shouldn't be nested. I'd do it like this:

Code:
If EFS = -1 Then Me.txt_ChAttn = "John Doe"
If JAF = -1 Then
  If Me.txt_ChAttn <>"" then Me.txt_ChAttn = Me.txt_ChAttn & ", "
  Me.txt_ChAttn = Me.txt_ChAttn & "Jane Doe"
End If

As far as I can tell, the above will yield the same result in all cases.
 

Users who are viewing this thread

Top Bottom