I have a saved insert query to add a record to a table. The parameters for the query come from an unbound form. 2 of the fields for the table are of the yes/no data type. The form control for these fields is a checkbox.
Here is the saved query "qryInsertLog"
PARAMETERS UserPar Text ( 255 ), ApprovedByPar Text ( 255 ), CCedByPar Text ( 255 ),
UnitIdPar Short, NotePar LongText, Z3Par Bit, Z5Par Bit, FollowupNotesPar LongText;
INSERT INTO tblLogBook ( [User], ApprovedBy, CCedBy, UnitID, Notes, Z3, Z5, FollowupNotes )
SELECT [UserPAR] AS Expr1, [ApprovedByPar] AS Expr2, [CCedByPar] AS Expr3,
[UnitIDPar] AS Expr4, [NotePar] AS Expr5, [Z3Par] AS Expr6, [Z5Par] AS Expr7,
[FollowupNotesPar] AS Expr10;
Here is my VBA code tied to a save button on my form:
Private Sub cmdSaveandNew_Click()
Dim db As DAO.Database
Dim qdf1 As DAO.QueryDef
Set db = CurrentDb
Set qdf1 = db.QueryDefs("qryInsertLog")
'put form parameters into insert query
qdf1.Parameters(0).Value = Me.cboUser.Value
qdf1.Parameters(1).Value = Me.cboApprover.Value
qdf1.Parameters(2).Value = Me.cboCCer.Value
qdf1.Parameters(3).Value = Me.cboUnit.Value
qdf1.Parameters(4).Value = Me.txtNotes.Value
qdf1.Parameters(5).Value = Me.chkZ3.Value
qdf1.Parameters(6).Value = Me.chkz5.Value
qdf1.Parameters(7).Value = Me.txtFollowup.Value
qdf1.Execute
Set qdf1 = Nothing
Call resetForm
End Sub
"resetForm" is a routine that simply returns the form controls to their default values.
When I hit my save button, sometimes the record gets added, but without getting the correct values for Me.chkZ3.Value and Me.chkZ5.Value (meaning if I had them checked, the record gets entered with those values as false). After hitting the save button a few times, I start getting the following error:
Run-time error '3000':
Reserved error (-3033); there is no message for this error.
The most interesting part about this, however, is that when I modify my query to only include 1 yes/no field, I get no errors and records are updated correctly.
What the heck is going on here?
edit: I forgot to mention that when I run the query from the Access Objects window (as opposed to VBA), it works exactly as it should.
You should probably add a bunch more error tracking and checking for Null values in your form fields before assigning them to your table. Saying that, this one fix might fix your immediate problem.
I assume your Z3 and Z5 fields are bit fields - seeing as that's how you declared the matching parameter types.
If that's so, you should probably trap for Null checkboxes and convert true(-1) to 1
As a sidenote, I would also suggesting using named parameters instead of numeric indexes in case the order of those ever get changed - and also for readability and future maintenance ease
Instead of
Use
The problem are the
LongText
parameters. I tried a similar query:with this code
and various length for the LongText parameter (2, 10, 3) .
Resulting in this crazy data (the Yes/No fields were always
yesno1 = True
andyesno2 = False
!) :So apparently with LongText parameters, you are better off using
RecordSet.AddNew
instead of a parameterized query.Parameters are limited to 255 characters anyway.
Addendum
If I run the code in a loop to find a system, I also get the
Reserved error (-3033)
.