Results 1 to 9 of 9
  1. #1
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145

    Write Code to Create Form Text Box to Display Date and Name

    Hi Forum,

    I like to write a VBA code on my text box "File Name 2" in my Form to display the Date (Date of Inspection) and Name (Inspector Name) that is selected within my Form.

    Here's the illustration. The code is my attempt to get it to work but it doesn't.

    Click image for larger version. 

Name:	3.png 
Views:	23 
Size:	12.9 KB 
ID:	29343Click image for larger version. 

Name:	4.png 
Views:	23 
Size:	34.8 KB 
ID:	29344

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    What does 'does not work' mean - error message, wrong results, nothing happens?

    Are you trying to save the concatenated value to a field in table? Why? Just do the concatenation when needed. The concatenation expression can be in query or textbox ControlSource.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    Quote Originally Posted by June7 View Post
    What does 'does not work' mean - error message, wrong results, nothing happens?

    Are you trying to save the concatenated value to a field in table? Why? Just do the concatenation when needed. The concatenation expression can be in query or textbox ControlSource.
    Nothing happens. I am trying to concatenate the values in the form so it can be saved into a table. I know I can concatenate these values in expression builder; however, I do not want to use that. I want a code that can display it in the text box and still can edit at the user's discretion.

  4. #4
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I know I can concatenate these values in expression builder;
    No, not in the expression builder, in a query.... the record source for the form. In general, it is a bad idea to save calculated values.
    In a query is would look like
    Code:
    FN2: Format([Date of Inspection],"yyyymm-dd") & "-" & [Inspector Name]
    Note: should not use spaces in object names. Instead of [Date of Inspection], how about [InspectionDate]??

    You have a query for the form record source, the "FileName2" is bound to the query calculated field. You enter the inspector name and the date and the "FileName2" control displays the calculated value. It is always up to date.



    Also note that, in the image, the label for the inspector name has an "i" in it that shouldn't be there: "Inspectior Name"...

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I think the code would have to go in the AfterUpdate events of the date and name controls, not File_Name2. And should be saving to the field not the control. Bind control to the field if you want users to be able to edit the saved concatenated value.

    But only if the field is null and values are available from both date and name controls.

    If IsNull(Me!fieldname) And IsNull(Me.[Date of Inspection]) And IsNull(Me.[Inspector Name]) Then
    Me!fieldname = Me.[Date of Inspection] & "-" & Me.[Inspector Name]
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    Quote Originally Posted by ssanfu View Post
    No, not in the expression builder, in a query.... the record source for the form. In general, it is a bad idea to save calculated values.
    In a query is would look like
    Code:
    FN2: Format([Date of Inspection],"yyyymm-dd") & "-" & [Inspector Name]
    Note: should not use spaces in object names. Instead of [Date of Inspection], how about [InspectionDate]??

    You have a query for the form record source, the "FileName2" is bound to the query calculated field. You enter the inspector name and the date and the "FileName2" control displays the calculated value. It is always up to date.



    Also note that, in the image, the label for the inspector name has an "i" in it that shouldn't be there: "Inspectior Name"...
    Okay I see. Please look below at what I'm inputting incorrect

    Click image for larger version. 

Name:	5.png 
Views:	15 
Size:	24.1 KB 
ID:	29346

  7. #7
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    If you are dead set on storing the calculation, follow June's advice.


    Maybe what I suggest is a little advanced right now.

    I think it is not necessary to store the calculation. This would mean you do not need the field "File Name2". The "File Name2" would be calculated in a query.
    It would also mean you would have to use a query as the record source for a form or report, not the table.

    Soooo,

    In the query grid, Row named FIELD, instead of "File Name2", you would put

    FileName2: Format([Date of Inspection],"yyyymm-dd") & "-" & [Inspector Name]

    Nothing in the Criteria row!

    Save the query. Run the query to see the result.
    Set the form record source to the query.
    Then bind "FN2" to the control "File Name2" on the form/report.



    If you have a field named "Inspection Type", you need to add brackets -->> [Inspection Type]
    This is why you should not use spaces in names.
    Also, do not need ".Value"

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    The problem with using a calculated Field, like

    FileName2: Format([Date of Inspection],"yyyymm-dd") & "-" & [Inspector Name]

    in a Query, is this stated requirement:

    Quote Originally Posted by raychow22 View Post

    ...I want a code that can display it in the text box and still can edit at the user's discretion...
    If the Control on the Form is based on that Field, in the Table, the user won't be able to 'edit (that Control) at the user's discretion.'

    Given all requirements, I think June7's suggestion in Post #5 is probably the way to go.

    Linq ;0)>

  9. #9
    raychow22 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2017
    Posts
    145
    Quote Originally Posted by ssanfu View Post
    If you are dead set on storing the calculation, follow June's advice.


    Maybe what I suggest is a little advanced right now.

    I think it is not necessary to store the calculation. This would mean you do not need the field "File Name2". The "File Name2" would be calculated in a query.
    It would also mean you would have to use a query as the record source for a form or report, not the table.

    Soooo,

    In the query grid, Row named FIELD, instead of "File Name2", you would put

    FileName2: Format([Date of Inspection],"yyyymm-dd") & "-" & [Inspector Name]

    Nothing in the Criteria row!

    Save the query. Run the query to see the result.
    Set the form record source to the query.
    Then bind "FN2" to the control "File Name2" on the form/report.



    If you have a field named "Inspection Type", you need to add brackets -->> [Inspection Type]
    This is why you should not use spaces in names.
    Also, do not need ".Value"
    Thanks ssanfu,

    Figured it out. Realized it's not necessary for it to be on the table. I can just use query to combine the date and name together like you mentioned. Thanks again!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 12
    Last Post: 03-26-2015, 10:42 AM
  2. Replies: 9
    Last Post: 04-18-2014, 08:51 PM
  3. Write code to see all date between two date
    By barkarlo in forum Programming
    Replies: 2
    Last Post: 02-24-2013, 10:58 AM
  4. Display value in a text box after scheduled date
    By venu_resoju in forum Forms
    Replies: 5
    Last Post: 01-06-2013, 01:13 AM
  5. Write text to text box on a form
    By DKY in forum Programming
    Replies: 0
    Last Post: 10-08-2008, 11:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums