B
BruceM
I have a table for Vendor information, with a related table for PhoneNumber
since each vendor may have several. The PK in tblVendor and the FK in
tblPhone are both named VendorID. They are both Number fields (autonumber
in tblVendor). The vendor information appears on a form (frmVendor) that is
bound to tblVendor. The Phone information appears in an unbound list box
(lstPhone) that gets its data from the Current event for frmVendor:
Me.lstPhone.RowSource = "SELECT Phone FROM tblPhone WHERE VendorID = " &
Me.VendorID
By the way, I have seen this sort of code with:
& ""
at the end of it (I think that's how it went). I can't see that it makes
any difference, but if it does I would like to know.
Anyhow, this works fine. The problem occurs when I try to get the phone
number into a report I open from the form. There is a command button with
the following as its Click event:
Dim strLinkCriteria As String
strLinkCriteria = "VendorID = " & Me.VendorID
Me.Dirty = False
DoCmd.OpenReport "rptVendorInfo", acPreview, , strLinkCriteria
The rptVendorInfo has this as its Open event:
DoCmd.OpenForm "frmSender", , , , , acDialog
frmSender is an unbound form that accepts the name, e-mail address, etc. of
the person generating the report (the report is a fax to the vendor). A
command button on it sets the form's Visible property to False, and
rptVendorInfo opens with the appropriate information about the sender along
with selected fields from the vendor's record. However, I can't figure out
how to add the phone number (from the related table) to the report. I tried
an unbound text box on the report with its control source set to
Forms!frmVendor!lstPhone, but that didn't work (invalid use of Null error
message). I also tried adding tblPhone to the report's record source, but
there was ambiguity about which VendorID field was intended (in the command
button's Click event that opens the report). More about that in a moment.
It also occurred to me that something like the list box row source SQL could
work for the phone number text box on the report, but I couldn't figure out
how that works. My guess is that it is the best solution, but I can't
figure out how to implement it.
I should probably add that the phone number appears in the list box in the
order the phone numbers were added (i.e. the first phone number added for a
vendor has the lowest PhoneID (PK) number. I will change that to a ranking
system if needed, so that the top-ranked number appears first in the list
box. Or maybe there will be a check box for the main number, or something
like that. For now I am satisfied with either the first number in the list
box appearing on the report, or the currently-selected phone number on the
form appearing as the phone number in the report.
Back to an earlier point, I have gotten into the habit of giving the PK and
FK fields the same name when possible, on the theory that it simplifies
things in terms of what I need to remember about which field is supposed to
relate to which, but I have been questioning that practice. I wonder if it
would be better to, in this case, name VendorID in tblPhone something like
VendorIDch (for Child). That may make it simpler to code things like the
situation I have described. I would be interested in hearing thoughts on
this topic.
since each vendor may have several. The PK in tblVendor and the FK in
tblPhone are both named VendorID. They are both Number fields (autonumber
in tblVendor). The vendor information appears on a form (frmVendor) that is
bound to tblVendor. The Phone information appears in an unbound list box
(lstPhone) that gets its data from the Current event for frmVendor:
Me.lstPhone.RowSource = "SELECT Phone FROM tblPhone WHERE VendorID = " &
Me.VendorID
By the way, I have seen this sort of code with:
& ""
at the end of it (I think that's how it went). I can't see that it makes
any difference, but if it does I would like to know.
Anyhow, this works fine. The problem occurs when I try to get the phone
number into a report I open from the form. There is a command button with
the following as its Click event:
Dim strLinkCriteria As String
strLinkCriteria = "VendorID = " & Me.VendorID
Me.Dirty = False
DoCmd.OpenReport "rptVendorInfo", acPreview, , strLinkCriteria
The rptVendorInfo has this as its Open event:
DoCmd.OpenForm "frmSender", , , , , acDialog
frmSender is an unbound form that accepts the name, e-mail address, etc. of
the person generating the report (the report is a fax to the vendor). A
command button on it sets the form's Visible property to False, and
rptVendorInfo opens with the appropriate information about the sender along
with selected fields from the vendor's record. However, I can't figure out
how to add the phone number (from the related table) to the report. I tried
an unbound text box on the report with its control source set to
Forms!frmVendor!lstPhone, but that didn't work (invalid use of Null error
message). I also tried adding tblPhone to the report's record source, but
there was ambiguity about which VendorID field was intended (in the command
button's Click event that opens the report). More about that in a moment.
It also occurred to me that something like the list box row source SQL could
work for the phone number text box on the report, but I couldn't figure out
how that works. My guess is that it is the best solution, but I can't
figure out how to implement it.
I should probably add that the phone number appears in the list box in the
order the phone numbers were added (i.e. the first phone number added for a
vendor has the lowest PhoneID (PK) number. I will change that to a ranking
system if needed, so that the top-ranked number appears first in the list
box. Or maybe there will be a check box for the main number, or something
like that. For now I am satisfied with either the first number in the list
box appearing on the report, or the currently-selected phone number on the
form appearing as the phone number in the report.
Back to an earlier point, I have gotten into the habit of giving the PK and
FK fields the same name when possible, on the theory that it simplifies
things in terms of what I need to remember about which field is supposed to
relate to which, but I have been questioning that practice. I wonder if it
would be better to, in this case, name VendorID in tblPhone something like
VendorIDch (for Child). That may make it simpler to code things like the
situation I have described. I would be interested in hearing thoughts on
this topic.