MS Access 将多行文本框值插入 Table
MS Access Insert Multi-line Textbox Values into Table
我的 table 中有一个长文本列,我的表单中有一个多行文本字段。
我的文本字段设置为打开一个 FileDialog 并将完整路径插入到所选文件中,每个路径都在一个新行中。多行时插入不起作用,它是空白的。我用单线测试,它正在工作。这是我的 VB 代码:
我的插入:
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
我的文件对话框:
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
连接 vbCrLf 和 vrtSelectedItem
txt_DocumentLinks.Text = txt_DocumentLinks.Text & vbCrLf & vrtSelectedItem
但是,建议将每个 link 保存到相关 table 中的一条记录中。在这种情况下,不是在循环中连接单个字符串,而是在每次迭代中 运行 一个 INSERT 操作 SQL。或者在循环外打开 table 的 DAO 记录集并在循环内执行 AddNew 和 Update 操作,就像您对父记录所做的那样。真正的技巧是首先提交父记录并获取生成的新唯一 ID 以用作依赖 table.
的外键
我的 table 中有一个长文本列,我的表单中有一个多行文本字段。 我的文本字段设置为打开一个 FileDialog 并将完整路径插入到所选文件中,每个路径都在一个新行中。多行时插入不起作用,它是空白的。我用单线测试,它正在工作。这是我的 VB 代码:
我的插入:
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
我的文件对话框:
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
连接 vbCrLf 和 vrtSelectedItem
txt_DocumentLinks.Text = txt_DocumentLinks.Text & vbCrLf & vrtSelectedItem
但是,建议将每个 link 保存到相关 table 中的一条记录中。在这种情况下,不是在循环中连接单个字符串,而是在每次迭代中 运行 一个 INSERT 操作 SQL。或者在循环外打开 table 的 DAO 记录集并在循环内执行 AddNew 和 Update 操作,就像您对父记录所做的那样。真正的技巧是首先提交父记录并获取生成的新唯一 ID 以用作依赖 table.
的外键