Search code examples
databasevbaformsms-accessdata-entry

MS Access Insert Multi-line Textbox Values into Table


I have a Long Text column in my table and a multi-line text field in my form. My text field is set to open a FileDialog and insert the full paths, each on a new line, to the files selected. The insert is not working when it is multi-line, it is blank. I tested with single line and it is working. Here is my VB code:

My Insert:

Private Sub btn_test1_Click()
  mSaved = True
  Dim strSQL        As String
  Dim db            As DAO.Database
  Dim rs            As DAO.Recordset
  Dim ctl           As Control
  Dim varItem       As Variant
  Dim i             As Integer

  On Error GoTo ErrorHandler

  Set db = CurrentDb()
  Set rs = db.OpenRecordset("TrainingLog", dbOpenDynaset, dbAppendOnly)

  'make sure a selection has been made
  If Me.ListBox_Emp.ItemsSelected.Count = 0 Then
    MsgBox "Must select at least 1 employee"
    mSaved = False
    Exit Sub
  ElseIf Me.ddl_Topic.Value = 0 Then
    MsgBox "Must select a topic"
    mSaved = False
    Exit Sub
  ElseIf Me.ddl_Type.Value = 0 Then
    MsgBox "Must select a category"
    mSaved = False
    Exit Sub
  ElseIf Me.ddl_Source.Value = 0 Then
    MsgBox "Must select a source"
    mSaved = False
    Exit Sub
  ElseIf Me.ddl_mediaType.Value = 0 Then
    MsgBox "Must select a media type"
    mSaved = False
    Exit Sub
  ElseIf Me.ddl_Cert.Value = 0 Then
    MsgBox "Must select either certificate, sign-in sheet, or both"
    mSaved = False
    Exit Sub
  End If

'add selected value(s) to table
  Set ctl = Me.ListBox_Emp
  For Each varItem In ctl.ItemsSelected
    rs.AddNew
    rs!Employee = ctl.ItemData(varItem)
    rs!Topic = Me.ddl_Topic.Value
    rs!TopicOther = Me.txt_TopicOther.Value
    rs!TitleOfTraining = Me.txt_Title.Value
    rs!Category = Me.ddl_Type.Value
    rs!CategoryOther = Me.txt_typeOther.Value
    rs!MediaType = Me.ddl_mediaType.Value
    rs!Source = Me.ddl_Source.Value
    rs!SourceOther = Me.txt_sourceOther.Value
    rs!DateCompleted = Me.DateCompleted.Value
    rs!CertSignSheet = Me.ddl_Cert.Value
    rs!MakeUp = Me.ChkBox_MakeUp.Value
    rs!DateOriginal = Me.dt_DateOriginal.Value
    rs!Mandatory = Me.ChkBox_Mandatory.Value
    rs!DateDue = Me.dt_DueDate.Value
    rs!DocumentLinks = Me.txt_DocumentLinks.Value
    rs!Notes = Me.txt_Notes.Value
    rs.Update
  Next varItem

  MsgBox ("Changes Saved!")
  mSaved = False
  If MsgBox("Do You Want to Log Another Training?", vbYesNo + vbQuestion) = vbYes Then
    For i = 0 To ListBox_Emp.ListCount
        If ListBox_Emp.Selected(i) = True Then
            ListBox_Emp.Selected(i) = False
        End If
    Next i
    ddl_Topic.Value = ""
    txt_TopicOther.Value = ""
    txt_Title.Value = ""
    ddl_Type.Value = ""
    txt_typeOther.Value = ""
    ddl_mediaType.Value = ""
    ddl_Source.Value = ""
    txt_sourceOther.Value = ""
    DateCompleted.Value = ""
    ddl_Cert.Value = ""
    ChkBox_MakeUp.Value = ""
    dt_DateOriginal.Value = ""
    ChkBox_Mandatory.Value = ""
    dt_DueDate.Value = ""
    txt_DocumentLinks.Value = ""
    txt_Notes.Value = ""
    Exit Sub
Else
    DoCmd.Close acForm, "AddTrainingLog_noSub"
End If
ExitHandler:
  Set rs = Nothing
  Set db = Nothing
Exit Sub

ErrorHandler:
Select Case Err
  Case Else
    MsgBox Err.Description
    DoCmd.Hourglass False
    Resume ExitHandler
  End Select
End Sub

My FileDialog:

Private Sub txt_DocumentLinks_Click()
    Dim dlgOpen         As FileDialog
    Dim varFileName     As String
    Dim sFolder         As String
    Dim vrtSelectedItem As Variant
    Dim myArray()       As Variant
    Dim myString        As String
    Dim x               As Long

        txt_DocumentLinks.Text = ""

        Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
        With dlgOpen
            .AllowMultiSelect = True
            ''Start in
            .InitialFileName = "Y:\Data Dept"
            .Show
            For Each vrtSelectedItem In .SelectedItems
                'Using a msgbox to test getting the path names, this is working great!
                'MsgBox (vrtSelectedItem)
                txt_DocumentLinks.Text = txt_DocumentLinks.Text '& vbCrLf & vrtSelectedItem

                'sText = Replace(sText, vbLf & vbCr, "")
            Next vrtSelectedItem
        End With 
End Sub

Solution

  • Concatenate with vbCrLf and vrtSelectedItem

    txt_DocumentLinks.Text = txt_DocumentLinks.Text & vbCrLf & vrtSelectedItem

    However, recommend saving each link to a record in a related table. In which case, instead of concatenating a single string in the loop, run an INSERT action SQL in each iteration. Or open a DAO recordset of that table outside the loop and execute AddNew and Update actions within loop, like you do for parent record. Real trick will be first committing parent record and grabbing new unique ID generated to use as foreign key in dependent table.