Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to join two fields together from same form

Status
Not open for further replies.

ScorpioMCN

Programmer
Feb 13, 2003
40
US
I apologize in advance if this has been asked before but I could not find a similar post in a search.

Here's my issue:

I have Access 2000 and a Contacts database and a Form where the user enters new contacts*. I have one field for the firstname and another for the lastname. I have a third field called FullName (please don't tell me this is redundant as I have a good reason for this)...I don't want the user to have to type the entire name a second time in this field but rather take the input from the firstname and lastname and put them together (autofill / populate) in the FullName field adding a space inbetween.

* I do not want the code to change the FullName for contacts put in previously - for example I want the flexibility to override what's in the FullName field with whatever...for example..."Joe"; "Smith"; "Joseph Smith" and I want it to keep this forever unless I decide to manually change it in the future. Only want the autopopulate to do its thing on new adds.

I'm an intermediate Access user/developer with no VBA code experience.

I would really appreciate any help with this. Thanks!

Mike
 
One quick way:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(txtFull.Value) Then
        txtFull.Value = txtFirst.Value & " " & txtLast.Value
    End If
End Sub

This is the On Exit event for a form. It assumes that the text boxes are named txtFull, txtFirst and txtLast. Adjust to fit your needs.
This will only update if the Full Name box is null. Therefore, it will update for all new records OR if you delete the value in the Full Name text box.

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
I don't understand - does the code you included go in the Before Update section or the On Exit section ??

I tried putting it in the On Exit using my field names and I get an error.
 
Greg - the code you gave me does not work - I tried it in both the On Exit and Before Update sections - and neither one populates the Full Name field ... i get no errors now but it does NOT populate...

Here's what I used:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Full_Name.Value) Then
Full_Name.Value = First_Name.Value & " " & Last_Name.Value
End If

End Sub
 
Sorry, I did mean to say the before update event of the form not the same event for the text boxes. I tested it and it worked fine for me.

Try naming your text boxes something different than the actual field names. i.e., in my example above the text box is txtFirst and the field name First is bound to that text box.

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
Thanks for clarifying Greg - I will try it and report back here. I'll let you know.
 
Greg - your code does work!! thanks - what threw me off is that I was expecting to see the Full Name field filled in on the form after I put in the first and last names - it doesn't appear until AFTER it updates the contact table...but that's ok! I went back and looked at a contact I added and the Full Name field is properly filled in.

THANKS!!!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top