Page 2 of 2 FirstFirst 12
Results 16 to 22 of 22
  1. #16
    mib1019 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2020
    Posts
    54

    Decided on Child Table for Dates in Text Field

    Quote Originally Posted by kd2017 View Post
    I believe this will do what you've asked for:
    Code:
    Private Sub lstChooseDates_AfterUpdate()
        Dim i As Integer
        Dim dates As String
        
        dates = ""
        
        For i = 0 To Me.lstChooseDates.ListCount - 1
            If Me.lstChooseDates.Selected(i) Then
                dates = dates & Format(Me.lstChooseDates.ItemData(i), "MMM, mm/dd") & "; "
            End If
        Next i
        
        If dates <> "" Then
            'remove the last semicolon and space
            dates = Left(dates, Len(dates) - 2)
        End If
        
        Me.txtResult = dates
    End Sub
    BUT without knowing the details of your project it might be better to have a child table for these dates rather than to denormalize them to save it in a short text field. Good luck with your project.
    KD2017, I'm back on this topic again....

    I've got the popup form for populating dates between txtStartDate and txtEndDate working perfectly. For simplicity, I want to keep it that way. It generates the necessary piece for the document my user needs. I can see where I might want to be able to actually use the dates in reporting later.

    So my question is, how could I populate a child table that is related to the one which holds this text string?



    My child table fields would be ID, Detail_ID (the primary key of the table that holds the text field with all the dates), Air_Date (which is derived from the text field, split into the x number of dates that are in that text box).

    So I would like to see, when the record is saved with the txtAirDates field, it writes new records to the Child Table: Dates of 7/13, 7/15, 7/17 go to three records in the child table, all with the same Detail_ID and a generated ID in the primary key.

    Is this possible?

    Thanks!
    MIB1019

  2. #17
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Sure!

    The primary key should be an Autonumber datatype, access automatically handles creating a unique ID for autonumbers. You would no longer want to have the AirDates text field in the parent table, rather you would derive the string based on a query that looks up AirDates when ever you would need it. You can write a simple vba function to accomplish this lookup and "compile" your string.

  3. #18
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    See attached for an example database I've created. In the vba module 'HelperFunctions' you'll find a function I wrote that will build a string as I suggested above. This function can be called from forms, reports, and even queries. I've provided a usage example in both a query and on the 'Parent' form.

    You will need to tweak the code, field and object names, to match those in your database.

    *Note: a vba function like this works just fine in queries with a relatively small number of records, however it's not very efficient and can be quite slow if you try to run it in a query with a lot of parent records. You shouldn't have any problems running the function in forms or reports. If you need to run the function in a query I'd suggest filtering the records to as few as possible before running the vba function.
    Attached Files Attached Files

  4. #19
    mib1019 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2020
    Posts
    54
    kd2017,

    Correct, the child table primary key would be autonumber.

    I will play with this and see how it works out. Thanks so much for our help!

    MIB1019

  5. #20
    mib1019 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2020
    Posts
    54
    KD2017, I have looked at your sample. Thanks for your input there.

    The current table, tblIODetails, which is a child of table called tblIOs. The child table currenlty has the has the text box of dates. What I am wanting to do is extract the dates into separate records for the purpose of reporting. Would like to do this dynamically and to a temporary table perhaps, since the user may go in and edit the airdates textbox and so I would only want current values.

    So if I have a record in the tblIODetails table that's got these dates in it, 6/17, 6/19, 6/23, 6/25, I'd like to retrieve those dates as separate records.

    Thanks for your help. You are very patient.
    MIB1019

  6. #21
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Quote Originally Posted by mib1019 View Post
    ... since the user may go in and edit the airdates textbox and so I would only want current values...
    This is why I suggest that you get rid of the txtAirDates field all together and only "calculate" the string when needed for presentation.

    You can use the split function to break up your date string into an array, then use CDate to convert to a date datatype.
    For example:
    Code:
        Dim dateArray() As String
        Dim i As Integer
        
        dateArray = Split(AirDates, ",")
        
        For i = 0 To UBound(dateArray)
            'This loop will print each date individually
            'From here you can insert records into your child table
            'or do what ever it is you need to do.
            Debug.Print CDate(dateArray(i))
        Next i
    Where AirDates = a string like "6/17, 6/19, 6/23, 6/25". Note that the example string you've provided doesn't have a year so CDate will assume the current year. You can use the DateSerial function to specify the year if it's not available within the string.

  7. #22
    mib1019 is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2020
    Posts
    54
    Thanks so much kd2017. You're very helpful.

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 10-02-2017, 02:02 PM
  2. Replies: 2
    Last Post: 09-19-2017, 11:31 AM
  3. Creating Range of Dates in a Form
    By melb in forum Forms
    Replies: 6
    Last Post: 03-16-2015, 02:32 PM
  4. Query input dates for range of dates in two fields
    By kagoodwin13 in forum Queries
    Replies: 3
    Last Post: 02-13-2012, 04:25 PM
  5. Replies: 10
    Last Post: 11-16-2011, 10:58 AM

Tags for this Thread

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