Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with OldValue

Andi Plotsky
Guest
 
Posts: n/a
#1: Nov 12 '05

I am running some code to validate the entry made into a form called
NumberOfSamples. If validation fails, then I want to revert back to the old
value. I keep getting an error msg that says:

"Run-time Error 2115: The macro or function set to the Before Update or
Validation Rule property for this field is preventing Access from saving the
data in the field." Debug points to the oldvalue statement. What might be
the problem? See code below.....

Thanks for your help!
Andi Plotsky


Private Sub NumberOfSamples_BeforeUpdate(Cancel As Integer)
Dim rsA As DAO.Recordset
Dim strQuery As String
Dim strSQL As String
Dim numrec As Integer
Dim varTestname As String

Set frmcurrent = Me.Form
varTestname = SelectTestsLast(frmcurrent)
Debug.Print "vartestname= "; varTestname

'Select 1 of each SampleID to count the current number of samples:
strQuery = "qryOpenSamples"
strSQL = "SELECT SampleID,[Testname],BatchID, SubjectName,HistNo,[Sample
Type],RefrigFreezerNo,ShelfNo, DateOfSample,TimeOfSample FROM qryblue WHERE
BatchID=" & Me.BatchID & " AND Testname=" & "'" & varTestname & "'" & "
order by SampleID"
ChangeQueryDef strQuery, strSQL

Set rsA = CurrentDb.OpenRecordset("qryOpenSamples", dbOpenDynaset)

rsA.MoveFirst
rsA.MoveLast
numrec = rsA.RecordCount 'the number of Samples in the batch previously
Debug.Print "numrec= "; numrec

If NumberOfSamples < numrec Then
MsgBox "Oops! The new Number of Samples is LESS than the current
Number of Samples. If you want to DELETE reccords, start at the Main Menu."
NumberOfSamples.Value = NumberOfSamples.OldValue
Exit Sub
End If

If NumberOfSamples = numrec Then
MsgBox "You have not changed the Number of Samples"
NumberOfSamples.Value = NumberOfSamples.OldValue
Exit Sub
End If

rsA.Close: Set rsA = Nothing

End Sub


--
Andi Plotsky
IRIS, LLC



Salad
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Problem with OldValue


Andi Plotsky wrote:
[color=blue]
> I am running some code to validate the entry made into a form called
> NumberOfSamples. If validation fails, then I want to revert back to the old
> value. I keep getting an error msg that says:
>
> "Run-time Error 2115: The macro or function set to the Before Update or
> Validation Rule property for this field is preventing Access from saving the
> data in the field." Debug points to the oldvalue statement. What might be
> the problem? See code below.....
>
> Thanks for your help!
> Andi Plotsky
>
>
> Private Sub NumberOfSamples_BeforeUpdate(Cancel As Integer)
> Dim rsA As DAO.Recordset
> Dim strQuery As String
> Dim strSQL As String
> Dim numrec As Integer
> Dim varTestname As String
>
> Set frmcurrent = Me.Form
> varTestname = SelectTestsLast(frmcurrent)
> Debug.Print "vartestname= "; varTestname
>
> 'Select 1 of each SampleID to count the current number of samples:
> strQuery = "qryOpenSamples"
> strSQL = "SELECT SampleID,[Testname],BatchID, SubjectName,HistNo,[Sample
> Type],RefrigFreezerNo,ShelfNo, DateOfSample,TimeOfSample FROM qryblue WHERE
> BatchID=" & Me.BatchID & " AND Testname=" & "'" & varTestname & "'" & "
> order by SampleID"
> ChangeQueryDef strQuery, strSQL
>
> Set rsA = CurrentDb.OpenRecordset("qryOpenSamples", dbOpenDynaset)
>
> rsA.MoveFirst
> rsA.MoveLast
> numrec = rsA.RecordCount 'the number of Samples in the batch previously
> Debug.Print "numrec= "; numrec
>
> If NumberOfSamples < numrec Then
> MsgBox "Oops! The new Number of Samples is LESS than the current
> Number of Samples. If you want to DELETE reccords, start at the Main Menu."
> NumberOfSamples.Value = NumberOfSamples.OldValue
> Exit Sub
> End If
>
> If NumberOfSamples = numrec Then
> MsgBox "You have not changed the Number of Samples"
> NumberOfSamples.Value = NumberOfSamples.OldValue
> Exit Sub
> End If
>
> rsA.Close: Set rsA = Nothing
>
> End Sub
>
>[/color]
See if this works. Apply where necessary

If NumberOfSamples = numrec Then
MsgBox "You have not changed the Number of Samples"
me.NumberOfSamples.Undo
Cancel = True
End If

Andi Plotsky
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Problem with OldValue


It worked - thanks!!


"Salad" <oil@vinegar.com> wrote in message
news:S_Tjc.16392$e4.2983@newsread2.news.pas.earthl ink.net...[color=blue]
> Andi Plotsky wrote:
>[color=green]
> > I am running some code to validate the entry made into a form called
> > NumberOfSamples. If validation fails, then I want to revert back to the[/color][/color]
old[color=blue][color=green]
> > value. I keep getting an error msg that says:
> >
> > "Run-time Error 2115: The macro or function set to the Before Update or
> > Validation Rule property for this field is preventing Access from saving[/color][/color]
the[color=blue][color=green]
> > data in the field." Debug points to the oldvalue statement. What might[/color][/color]
be[color=blue][color=green]
> > the problem? See code below.....
> >
> > Thanks for your help!
> > Andi Plotsky
> >
> >
> > Private Sub NumberOfSamples_BeforeUpdate(Cancel As Integer)
> > Dim rsA As DAO.Recordset
> > Dim strQuery As String
> > Dim strSQL As String
> > Dim numrec As Integer
> > Dim varTestname As String
> >
> > Set frmcurrent = Me.Form
> > varTestname = SelectTestsLast(frmcurrent)
> > Debug.Print "vartestname= "; varTestname
> >
> > 'Select 1 of each SampleID to count the current number of samples:
> > strQuery = "qryOpenSamples"
> > strSQL = "SELECT SampleID,[Testname],BatchID,[/color][/color]
SubjectName,HistNo,[Sample[color=blue][color=green]
> > Type],RefrigFreezerNo,ShelfNo, DateOfSample,TimeOfSample FROM qryblue[/color][/color]
WHERE[color=blue][color=green]
> > BatchID=" & Me.BatchID & " AND Testname=" & "'" & varTestname & "'" & "
> > order by SampleID"
> > ChangeQueryDef strQuery, strSQL
> >
> > Set rsA = CurrentDb.OpenRecordset("qryOpenSamples", dbOpenDynaset)
> >
> > rsA.MoveFirst
> > rsA.MoveLast
> > numrec = rsA.RecordCount 'the number of Samples in the batch[/color][/color]
previously[color=blue][color=green]
> > Debug.Print "numrec= "; numrec
> >
> > If NumberOfSamples < numrec Then
> > MsgBox "Oops! The new Number of Samples is LESS than the[/color][/color]
current[color=blue][color=green]
> > Number of Samples. If you want to DELETE reccords, start at the Main[/color][/color]
Menu."[color=blue][color=green]
> > NumberOfSamples.Value = NumberOfSamples.OldValue
> > Exit Sub
> > End If
> >
> > If NumberOfSamples = numrec Then
> > MsgBox "You have not changed the Number of Samples"
> > NumberOfSamples.Value = NumberOfSamples.OldValue
> > Exit Sub
> > End If
> >
> > rsA.Close: Set rsA = Nothing
> >
> > End Sub
> >
> >[/color]
> See if this works. Apply where necessary
>
> If NumberOfSamples = numrec Then
> MsgBox "You have not changed the Number of Samples"
> me.NumberOfSamples.Undo
> Cancel = True
> End If
>[/color]


Closed Thread


Similar Microsoft Access / VBA bytes