Blank Last Name Field to Display as Company

admsvcs

Registered User.
Local time
Today, 07:20
Joined
Jul 12, 2011
Messages
14
MS Access 2010

I have a report in which some records are companies where as others are individuals (First Name, Last Name). For a record in a report where there is no Last Name, I would like the Company to display.

Please also tell me where the code is to be inserted, for example, the Properties Sheet>>Data tab>>Control Source...if that is where it goes. Thank you for your help!
 
I would create a seperate query...

Code:
SELECT tblCustomerProfile.cpCustomerID, IIf([LastName]= "" Or IsNull([LastName]), [cpCompanyName], [cpLastName] & ", " & [cpFirstName]) AS Client
FROM tblCustomerProfile
ORDER BY tblCustomerProfile.cpCustomerID;

Then use that query in the query that is the RecordSource of the Report to get the names to show. Of course, change the names to match your tables and fields.
 
Appreciate your reply, Gina, but I have no idea what to do with the code. I created a query using the query wizard including all of the fields from the underlying table. Beyond that, I do not know what to do. Where does the code go? Thank you, Norm
 
In design view of your report, right click on the textbox that you want the person's name/company to appear, click 'Properties' on the menu, click on the 'Data' tab of the dialog that pops up and put this in the Control Source field:

=IIf(isnull([LastName]), [CompanyName], [FirstName] & " " & [LastName])

Change all the data between brackets [] to the names of the corresponding fields in your underlying data source.
 
If you are still unable to get the line in place you can post the SQL of your query here I can retype it and you can copy paste it back.
 
Thank you, plog! I have attache two screenshots for you. I think I have entered the code correctly, but I am getting an error of "#type!" in the report. I have been unable to nail down the difficulty. I am only guessing here, but maybe the error is indicating a mismatch in data types, but I don't see how that could be. Your thoughts, please. Norm
 

Attachments

  • IfNull.jpg
    IfNull.jpg
    102.7 KB · Views: 145
  • Type Error.JPG
    Type Error.JPG
    83.9 KB · Views: 150
Sorry to contradict Plog, but I would NOT put it on the report directly. Create it in the query like Gina said. But to make it easier, open the query in design view and then in the QBE Grid type in this for the part that says FIELD

Client:IIf(Len([Last Name] & "") = 0, Nz([Company],""), [Last Name] & ", " & [First Name])


And the reason why I say do it on the query instead of in the report's text box is that it is faster and more efficient and you do want to avoid things (if it is possible, which in this case it is) that slow down reporting.


EDIT: Added screenshot to be more helpful

attachment.php
 

Attachments

  • admsvcs01.png
    admsvcs01.png
    5.6 KB · Views: 350
Last edited:
Bob, your suggestion work perfectly. Thank you! Now I have to spend a little more time with some unfamiliar nomenclature, such as Len and NZ. The learning curve is straight up!!!! Hopefully, I will be able to help others in due time.

I wish I was up in your neck of the woods. I am from Bellingham, WA, but moved to TX, near San Antonio, about six years ago. We normally return to the PNW for the summers, but this year we are stuck in 100 plus heat. Ugh! I will happily send some sunshine your way if you will send some rain...lots of it!!! Stay well and thank you again! Norm:)
 
And just an FYI for you.

Len is the function to determine length. So, if you have a field which is null, you can't check its length directly you will get nothing. But you can append the empty string which will then allow you to check the length (it will show up as zero if either null or empty) and that shortens up things. Some people use the NZ function (Null to Zero) which will let you substitute values for nulls. So you could use

IIf(Nz([Last Name], "") = "", [company], [Last Name] & ", " & [First Name])

And that is okay too but from some conversations with some pretty knowledeable people, it is more efficient to check the length than to use the NZ to substitute and then do the comparison with the empty string.

So hopefully that helps.
 

Users who are viewing this thread

Back
Top Bottom