Coded button not working properly

  • Thread starter Thread starter Pamela
  • Start date Start date
P

Pamela

I have a command button on frmVehicle that is supposed to open frmInspection
and find (if it's been entered) the related record based on the VehicleID
fields in both tables which have a one-to-many relationship. I used the
wizard to create the button but it isn't linking correctly - the VehicleID
field in frmInspection stays at 0. Any help or insight with this would be
greatly appreciated! Here is the code that the wizard created for the button:
Private Sub OpenInsp_Click()
On Error GoTo Err_OpenInsp_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmInspection"
stLinkCriteria = "[VehicleID]=" & Me![VehicleID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_OpenInsp_Click:
Exit Sub
Err_OpenInsp_Click:
MsgBox Err.Description
Resume Exit_OpenInsp_Click
End Sub
Thanks again!
 
Pamela said:
I have a command button on frmVehicle that is supposed to open
frmInspection
and find (if it's been entered) the related record based on the VehicleID
fields in both tables which have a one-to-many relationship. I used the
wizard to create the button but it isn't linking correctly - the VehicleID
field in frmInspection stays at 0. Any help or insight with this would be
greatly appreciated! Here is the code that the wizard created for the
button:
Private Sub OpenInsp_Click()
On Error GoTo Err_OpenInsp_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmInspection"
stLinkCriteria = "[VehicleID]=" & Me![VehicleID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_OpenInsp_Click:
Exit Sub
Err_OpenInsp_Click:
MsgBox Err.Description
Resume Exit_OpenInsp_Click
End Sub
Thanks again!


What type of field is VehicleID? If it's text, then you need:

stLinkCriteria = "[VehicleID]='" & Me![VehicleID] & "'"

If that isn't the problem, what is the RecordSource property of
frmInspection? Is it a query? If so, please post the SQL of that query.
 
VehicleID, an auto number field, is the pk for tblVehicle which is the record
source for frmVehicle. Then similarly tblInspection is the record source for
frmInspection with PK InspectionID, an auto number field. VehicleID is also
then in tblInspection as a number field and serves to relate the two
together. Hope this additional info helps!! Thanks so much!

Dirk Goldgar said:
Pamela said:
I have a command button on frmVehicle that is supposed to open
frmInspection
and find (if it's been entered) the related record based on the VehicleID
fields in both tables which have a one-to-many relationship. I used the
wizard to create the button but it isn't linking correctly - the VehicleID
field in frmInspection stays at 0. Any help or insight with this would be
greatly appreciated! Here is the code that the wizard created for the
button:
Private Sub OpenInsp_Click()
On Error GoTo Err_OpenInsp_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "frmInspection"
stLinkCriteria = "[VehicleID]=" & Me![VehicleID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_OpenInsp_Click:
Exit Sub
Err_OpenInsp_Click:
MsgBox Err.Description
Resume Exit_OpenInsp_Click
End Sub
Thanks again!


What type of field is VehicleID? If it's text, then you need:

stLinkCriteria = "[VehicleID]='" & Me![VehicleID] & "'"

If that isn't the problem, what is the RecordSource property of
frmInspection? Is it a query? If so, please post the SQL of that query.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Pamela said:
VehicleID, an auto number field, is the pk for tblVehicle which is the
record
source for frmVehicle. Then similarly tblInspection is the record source
for
frmInspection with PK InspectionID, an auto number field. VehicleID is
also
then in tblInspection as a number field and serves to relate the two
together.

That sounds fine, but let me suggest that you remove the Default Value of 0
for the VehicleID field in tblInspection. There will be no case where you
want that field to default to 0.

What is the recordsource of the form, frmInspection? If it's a stored
query, please post the SQL of that query.
 
Back
Top