Coded button not working properly

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!
 
D

Dirk Goldgar

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.
 
P

Pamela

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)
 
D

Dirk Goldgar

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top