[RESOLVED] Code works on buttonclick, but not automatically
Right now on my program I have it so when the user clicks on a certain item in the combobox, it moves to that item. All I am using is
VB Code:
Do Until ID.text = newValue
If newValue > oldValue then
Me.RecordSet.MoveNext
Else
Me.RecordSet.MovePrevious
End If
Loop
Now this works fine...It moves the bound items to the correct record, however, if I allow the form_current event to run directly after this, I receive the error
Originally Posted by Runtime Error '2115'
The macro or function set to the BeforeUpdate or ValidationRule for this field is preventing [Beta] from saving data in the field.
This happens on line
VB Code:
txtCourse.Text = ""
However, if I run the above loop, and stop form_current from running. Then run a commandbutton with this code
VB Code:
Thinger = False
Form_Current
Where thinger is the variable I used to stop form_current from running.
The program runs fine??
This has been stumping me for a good number of hours already, and any help would be appreciated
Re: Code works on buttonclick, but not automatically
No, I'm not writing back to the table at all. And sure, here is the code for the Form_Current, although I dont see why moving over 1 record wouldnt error, but moving over 2+ would :/
VB Code:
Private Sub Form_Current()
If Not RsLoad Then
RsLoad = True
Form_Load
End If
Dim strQuery As String
'strQuery = "SELECT * FROM WorkerInformation WHERE ID ='" & ID.Value & "'"
Re: Code works on buttonclick, but not automatically
Right the sequential count on the records seems to allow the Recordset.Move to work correctly...
What you need to do is take the combobox's field and perform a sequence count on this... so change the form's SQL to include this.. Presuming ID is the combo
SeqNr: Dcount("ID","WorkerInformation","ID<" ID)+1
If text based then
SeqNr: Dcount("ID","WorkerInformation","ID<'" ID & "'")+1
Now the killer part...
Remove all the code from the Form_Current event..
Create a Private Long Variable say lngCurrRec
Change the ID combobox to have the following RowSource, needs two columns with the second one hidden..
SELECT ID, DCount("ID","WorkerInformation","ID<'" & [ID] & "'")+1 AS SeqNr FROM WorkerInformation
On The Form Load set the lngCurrRec = 1
On The Form Activate Set the combobox to return the first instance of ID
On The ComboBox Change do the following
VB Code:
'populate the controls on the form with the seqnr set
Dim lngVal As Long
lngVal = CLng(IDCombo.Column(1))
If lngVal < CurrentNr Then
Recordset.Move -(CurrentNr - lngVal)
Else
Recordset.Move (lngVal - CurrentNr)
End If
CurrentNr = lngVal
And hopefully that should be Job done..
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
Re: Code works on buttonclick, but not automatically
Your assumption is correct.. got lazy and just copied the information from what I'd built..
Your form_current is trying to write the data from the recordset back into the form.. which could be causing the error.. if the form is based on an SQL statement then the Recordset.Move will handle that for you..
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
Re: [RESOLVED] Code works on buttonclick, but not automatically
Check your SeqNr in the RowSource of the Combo... sounds like it ain't correct.. it should return a count of the records irrespective of autonumber.. in my example Db I removed autonumber 5 to display this..
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation