Full Version: Saving a template
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
flyingdutchman
The following is supposed to be used to update a template in a table, but the message box displays even when it shouldn't be. Why would this be?


Private Sub cmdUpdateTemp_Click()
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "Template", CurrentProject.Connection, adOpenStatic, adLockOptimistic
rs.Filter = "[ModelNumber] = '" & cboModelNumber & "'"
templateSave (rs)
On Error GoTo endsub
rs.Save
rs.Close
Set rs = Nothing
cboModelNumber.Requery
endsub:
End Sub

Public Sub templateSave(rs)
On Error GoTo err_notintable
rs.Item("JobNumber") = txtJobNumber
rs.Item("ModelNumber") = cboModelNumber
rs.Item("ChassisSize") = cboChassisSize
Exit Sub
err_notintable:
MsgBox "Product with model number '" & cboModelNumber.Value & "' is not a template"
End Sub
MikeLyons
For testing purposes, I'd suggest adding the following right under your err_notintable label:

Debug.Print Err.Number & " - " & Err.Description


That will tell you what the actual problem is that was detected -- maybe the recordset never got opened? Or the data you are trying to store is too big for the intended field?

Some other problem?


Once you start finding the different errors, you can build specific tests in your error handler to react to them.

Mike
flyingdutchman
This did not work. The MsgBox showed but no description of what the error was.
MikeLyons
Can you post the changed code?
flyingdutchman
Private Sub cmdUpdateTemp_Click()
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
rs.Open "Template", CurrentProject.Connection, adOpenStatic, adLockOptimistic
rs.Filter = "[ModelNumber] = '" & cboModelNumber & "'"
templateSave (rs)
On Error GoTo endsub
rs.Save
rs.Close
Set rs = Nothing
cboModelNumber.Requery
endsub:
End Sub

Public Sub templateSave(rs)
On Error GoTo err_notintable
rs.Item("JobNumber") = txtJobNumber
rs.Item("ModelNumber") = cboModelNumber
rs.Item("ChassisSize") = cboChassisSize
Exit Sub
err_notintable:
Debug.Print Err.Number & " - " & Err.Description
MsgBox "Product with model number '" & cboModelNumber.Value & "' is not a template"
End Sub
MikeLyons
Hmmm... still nothing jumps out at me as obvious.

Try running the code with the option to Break on all Errors.

See if it halts at any particular place.

Mike
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.