Use combined name on form

B

BrookieOU

I have a query where I use a formula to combine the first/last name and
display it as a Contact Name (the table is set-up to store first and last
name separately). I would like to have a form where I enter project billing
that allows the user to choose the appropriate employee from the Contact Name
field in the query. The problem I'm having is it keeps telling me I can't
edit a formula. Does anyone know how to do this? I want to use the combined
Contact Name field so that I don't have to type in first and last name
separately.

Thanks,
Brooke
 
B

BruceM

Are you trying to select a name in the project billing form so that you can
populate a field, or look up records that include that employee's name, or
what exactly? What is the "it" that tells you you can't edit a formula, and
under what circumstances do you receive the message? Is there a Contact
table? If so, does it include ContactName ID field (an unchanging number or
text to identify the Contact)?
 
A

Arvin Meyer [MVP]

The only way you can do that it to use an unbound textbox and parse it into
the name fields. That method doesn't work well in continuous forms or
datasheets because only the current record is actually on the form, the
other rows are merely "pictures" of the data, and will reflect the active
unbound textbox value. Databases with poor or no relational capabilities,
like the Outlook's Contact list save the name and Parse it into separate
fields. You can do that as well in Access, but it is poor relational design,
and frowned upon.
 
B

BrookieOU

OK, sorry I didn't give enough info. I have a tblEmployees that stores each
employee with first/last name separate. I have tblProjectBilling that has
BillingID, EmployeeID (from the employees table), Invoice #, Invoice Amount,
Invoice Hours, Pay Period.

I would like the form to be a Project Billing entry form where a user can
select the combined first/last name of the employee and it will populate the
tblProjectBilling with the correct EmployeeID. Currently I have a
qryProjectBillingExtended that has all of the fields from tblProjectBilling
and the First Name and Last Name fields from tblEmployees. Then I use a
formula to combine the first and last name into a Contact Name. I want users
to be able to select the full name to populate, instead of having to type (or
select) a first name and a last name.

The "it" is a pop-up error that occurs after I try to choose the combined
name from the combo box on the form I did (which doesn't work). No, there is
not a contact table. I used the formula to create the contact name and
associated it with the EmployeeID from tblProjectBilling.
 
B

BrookieOU

So what do you recommend? That I have the user choose the first name from a
drop-down and then the last-name from another drop-down? Or should I create
a Contact Name field in the tblEmployees and just manually enter that when we
enter the employee?

BTW, thanks for all your help Arvin. You've been helping me a lot lately!

Brooke
 
B

BruceM

OK, there's an Employee table. You mentioned Contacts in the original
posting, so I assumed it was a Contact table.

Create a combo box bound to EmployeeID in tblProjectBilling.

Create a query from tblEmployee with EmployeeID and a combined field:

FullName: [FirstName] & " " & [LastName]

You can sort by the FullName field, or you can add the LastName field before
FullName (left to right in the query design grid) and sort on that, then
FullName.

Either build the query in the combo box Row Source, or save the query and
use it as the combo box Row Source. Set the combo box Bound Column to 1,
the Column Widths to something like 0";1.5", and the Column Count to 2. If
you are including the LastName field, Column Count is 3 and column widths
0";1.5",0".

Perhaps you already did something like this, but you were not specific about
the formula you used to combine the names, so this can be the starting
point.

As a general suggestion for newsgroup postings, be specific. Rather than
saying you used a formula, which could be any number of things, tell us
specifically what you tried.
 
B

BrookieOU

THANK YOU!!! I got it done!

Brooke


KenSheridan via AccessMonster.com said:
Brooke:

Here's the setup for an analogous situation, in this case for selecting an
existing salesperson or entering a new salesperson via a combo box on a form
bound to another table (in my case Customers, in your case the ProjectBilling
table) which includes a SalesPersonID numeric foreign key column:

1. the combo box is set up as follows:

ControlSource: SalesPersonID

RowSource: SELECT SalesPersonID,
FirstName & " " & LastName AS Fullname
FROM SalesPersons
ORDER BY LastName, FirstName;

BoundColum: 1
ColumnCount: 2
ColumnWidths: 0cm;8cm

If your units of measurement are imperial rather than metric Access will
automatically convert the last one. The important thing is that the first
dimension is zero to hide the first column and that the second is at least as
wide as the combo box.

2. To enable the user to enter a new sales person by typing the name in the
format 'Ken Sheridan' into the combo box the following code goes in the combo
box's NotInList event procedure:

Const conMESSAGE = "Salesperson's name must be entered " & _
"in format 'FirstName <space> LastName'"

Dim args As String
Dim strFirstName As String
Dim strLastName As String
Dim intSpacePos As Integer
Dim ctrl As Control
Dim strMessage As String

Set ctrl = Me.ActiveControl
strMessage = "Add " & NewData & " to list?"

intSpacePos = InStr(NewData, " ")

If intSpacePos = 0 Then
MsgBox conMESSAGE, vbExclamation, "Invalid Operation"
Response = acDataErrContinue
Else
strFirstName = Left(NewData, intSpacePos - 1)
strLastName = Mid(NewData, intSpacePos + 1)

' add named arguments
AddArg args, "argFirstName", strFirstName
AddArg args, "argLastName", strLastName


If MsgBox(strMessage, vbYesNo + vbQuestion) = vbYes Then
DoCmd.OpenForm "frmSalesPersons", _
DataMode:=acFormAdd, _
WindowMode:=acDialog, _
OpenArgs:=args
' ensure frmSalesPersons closed
DoCmd.Close acForm, "frmSalesPersons"
' ensure salesperson has been added
If Not IsNull(DLookup("SalesPersonID", "SalesPersons", _
"FirstName = """ & strFirstName & """ And " & _
"LastName = """ & strLastName & """")) Then
Response = acDataErrAdded
Else
strMessage = NewData & " was not added to Salespersons table.
"
MsgBox strMessage, vbInformation, "Warning"
Response = acDataErrContinue
ctrl.Undo
End If
Else
Response = acDataErrContinue
ctrl.Undo
End If
End If

3. The frmSalesPersons form, which is bound to the SalesPersons table, has
the following code in its Open event procedure:

Dim args As String

If Not IsNull(Me.OpenArgs) Then
args = Me.OpenArgs
' get named named arguments
Me.FirstName.DefaultValue = """" & Arg(args, "argFirstName") & """"
Me.LastName.DefaultValue = """" & Arg(args, "argLastName") & """"
End If

' move form to new record
DoCmd.GoToRecord acForm, Me.Name, acNewRec

4. The following module, named basArgs, which was developed some years ago
by Stuart McCall and myself and whose functions are called by the above
procedures, goes is added to the database:

''''module starts''''
Private Const OFFSET As Long = 127
Private Const ASSIGNOP As String = "=="

Function Arg(buffer, idx) As Variant

If IsNumeric(idx) Then
I& = InStr(1, buffer, Chr(idx + OFFSET - 1))
token$ = Chr(idx + OFFSET)
I& = InStr(I&, buffer, ASSIGNOP) + 2
Else
I& = InStr(1, buffer, idx) + Len(idx) + 2
token$ = Chr(Asc(Mid(buffer, InStr(1, buffer, idx) - 1, 1)) + 1)
End If
Arg = Mid(buffer, I&, InStr(I&, buffer, token$) - I&)

End Function

Function Argname(buffer, idx) As String

I& = InStr(1, buffer, Chr(idx + OFFSET - 1))
token$ = Chr(idx + OFFSET)
Argname = Mid(buffer, I& + 1, InStr(I&, buffer, ASSIGNOP) - (I& + 1))

End Function

Function ArgCount(buffer) As Long

ArgCount = Asc(Right(Chr(OFFSET) & buffer, 1)) - OFFSET

End Function

Sub AddArg(buffer, Argname, argval)

If Len(buffer & "") = 0 Then buffer = Chr(OFFSET)
If IsNumeric(Argname) Then Argname = ArgCount(buffer) + 1
buffer = buffer & Argname & ASSIGNOP & argval & Chr(Asc(Right(buffer, 1))
+ 1)

End Sub

Sub AddArgList(buffer, ParamArray Tokens())

For I& = 0 To UBound(Tokens)
AddArg buffer, I& + 1, Tokens(I&)
Next

End Sub
''''module ends''''

NB: you don't need to include a control bound to the computed 'contact name'
column in the form. The name shows in that format in the combo box, but its
value, by virtue of the hidden first column, is the unique numeric ID value
for that person.

Ken Sheridan
Stafford, England
 

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