I am using access to demonstrate the power of a database for my firm to track issues that arise during construction. I will use SQL Server as a backend eventually (before we actually start to use this) but I need to be able to show everyone what this brings to us before the IT guy will jump on board with giving me (and everyone else) permissions on the SQL Server database for this purpose.
I have a form that will add a record to one of my tables, and I planned on doing this with a QueryDef calling a parametric query like so:
Dim qdfAddNewRecord As DAO.QueryDef
Set qdfAddNewRecord = dbs.QueryDefs("InsertObservation")
qdfAddNewRecord.Parameters("P_ReportID").Value = Me.ReportID
qdfAddNewRecord.Parameters("P_Description").Value = CStr(Me.Description)
qdfAddNewRecord.Parameters("P_Division").Value = CInt(Me.cmbDivision.Column(0))
qdfAddNewRecord.Parameters("P_ObservationNum").Value = CInt(Me.ObservationNum)
qdfAddNewRecord.Execute dbFailOnError
But I get an error that I am not setting the [Table].Division field (listed as required in the table definition). When I run the query from the sql view it prompts me for the 4 parameters and then executes successfully. I was stumped, so I decided to try using a recordset to do this like so:
Set recset = dbs.OpenRecordset("Observations", dbOpenDynaset)
With recset
.AddNew
!ReportID = Me.ReportID
!Description = Me.Description
!Division = Me.cmbDivision.Column(0)
!ObservationNum = Me.ObservationNum
.Update
End With
This method works very well, so there must be something wrong with my query, or the way I set the parameters. I am at a loss having looked at this for several days, simplifying the query to only these 4 fields (there are many more in the table that I'll want to set) etc. My query is below, note that access automatically converts my Insert Into . . . Values pattern into the Select as expr pattern:
PARAMETERS P_ReportID Short, P_Description LongText, P_Division Short, P_ObservationNum Short;
INSERT INTO Observations ( ReportID, Description, Division, ObservationNum )
SELECT P_ReportID AS Expr1, P_Description AS Expr2, P_Division AS Expr3, P_ObservationNum AS Expr4;
One further detail: before setting the Division field as required the insert would work but only set the first 2 fields (ReportID and Description). In the debugger the parameters for the QueryDef are set with valid values and types that match. I have tried with and without the CInt() conversion with the same results. I have tried using index parameter calls (parameters(0).Value) and I have tried with and without the .Value all with the same results.
EDIT: I also tried using:
Dim divno as Integer
divno = CInt(Me.cmbDivision.Column(0))
And then using divno to set the qdf parameter value for Division. This resulted in the same outcome (just the first two parameters entered in the new record).
I will just use the working method for now, and when I move this to SQL Server will use a proper WPF/ MVVM front end, but I am bothered that something this simple won't work as expected. Any thoughts?