【问题标题】:Microsoft Access: Saving combo box data from form to tableMicrosoft Access:将组合框数据从表单保存到表
【发布时间】:2017-10-23 10:12:00
【问题描述】:

我有一个表格,用于存储用户在表单中输入的数据。除了从组合框中选择的那些字段外,在表单中输入的所有数据都会正确保存在表中。我已检查以确保表单设计视图中的所有这些组合框选择都绑定到基础数据中的关联表字段。

目前,我在表的设计视图中将这些字段的数据类型设置为“短文本”。我想知道问题是我需要将数据类型设置为不同的类型,还是有其他原因导致了这个问题。

【问题讨论】:

  • 请向我们展示组合框的数据源和行源属性,以及您的表单要保存到的表的结构。尽量使问题最小化、完整和可验证 (stackoverflow.com/help/mcve)
  • 我会尝试将数据类型更改为文本,看看是否可行。只要字段绑定到控件,除了不兼容的数据类型之外,没有其他原因可以使某些字段起作用而某些字段不起作用。
  • Text和Short Text其实是同一种数据类型(Long Text和Memo一样)。由于组合框列实际上是文本,因此任何组合框列值都应该保存到文本字段中,所以我认为还有其他事情发生。

标签: ms-access vba ms-access-2010 ms-access-2007


【解决方案1】:
Option Compare Database
Option Explicit
Dim stay As String
Function EnableInfo()

    Me.ADescription.Locked = False
    Me.ComboVendor.Locked = False
    Me.ComboVendor.Locked = False
    Me.onHand.Locked = False
    Me.onOrder.Locked = False
    Me.CostB.Locked = False
    Me.ListPriceB.Locked = False

End Function

Function DisableInfo()

    Me.ADescription.Locked = True
    Me.ComboVendor.Locked = True
    Me.onHand.Locked = True
    Me.onOrder.Locked = True
    Me.CostB.Locked = True
    Me.ListPriceB.Locked = True

End Function

'------------------------------------------------------------
' Function that disables/enable when command buttons when ADD and Edit are clicked!
'------------------------------------------------------------
Function AddEdit()

    Me.CmdAdd.Enabled = False
    Me.CmdEdit.Enabled = False
    Me.CmdExit.Enabled = False
    Me.CmdSave.Enabled = True
    Me.CmdCancel.Enabled = True

    Me.AllowAdditions = True
    Me.AllowDeletions = True
    Me.AllowEdits = True

    Call EnableInfo

End Function

'------------------------------------------------------------
' Function that disables/enable when command buttons when Save and Cancel are clicked!
'------------------------------------------------------------
Function SaveCancel()

    Me.CmdAdd.Enabled = True
    Me.CmdEdit.Enabled = True
    Me.CmdExit.Enabled = True
    Me.CmdSave.Enabled = False
    Me.CmdCancel.Enabled = False

    Call DisableInfo


End Function

'------------------------------------------------------------
' Function that enables navigation buttons
'------------------------------------------------------------
Function EnableNavigation()

    Me.cmdFirst.Enabled = True
    Me.cmdNext.Enabled = True
    Me.cmdPrevious.Enabled = True
    Me.cmdlast.Enabled = True

End Function


'------------------------------------------------------------
' Function that disables navigation buttons
'------------------------------------------------------------
Function DisableNavigation()

    Me.cmdFirst.Enabled = False
    Me.cmdNext.Enabled = False
    Me.cmdPrevious.Enabled = False
    Me.cmdlast.Enabled = False

End Function


'------------------------------------------------------------
' Function when the ADD button is clicked
'------------------------------------------------------------
Private Sub CmdAdd_Click()

    PartIDtext.SetFocus
    stay = PartIDtext.Value
    Me.DataEntry = True
    Call EnableInfo
    Call AddEdit
    Call DisableNavigation

End Sub

'------------------------------------------------------------
' Function when the CANCEL button is clicked
'------------------------------------------------------------
Private Sub CmdCancel_Click()

    Call SaveCancel
    Call DisableInfo
    Call EnableNavigation
    Me.Undo
    Me.DataEntry = False
    Me.RecordsetClone.FindFirst "partID = " & stay
    Me.Bookmark = Me.RecordsetClone.Bookmark



End Sub

'------------------------------------------------------------
' Function when the EDIT button is clicked
'------------------------------------------------------------
Private Sub CmdEdit_Click()

    Call AddEdit
    Call EnableInfo
    Call DisableNavigation
    PartIDtext.SetFocus
    stay = PartIDtext.Value



End Sub

'------------------------------------------------------------
' Function when the EXIT button is clicked
'------------------------------------------------------------
Private Sub CmdExit_Click()

    DoCmd.Close

End Sub

'------------------------------------------------------------
' Function when the SAVE button is clicked
'------------------------------------------------------------
Private Sub CmdSave_Click()

        ADescription = Trim(ADescription.Value)
        stay = Me.PartIDtext.Value

        If IsNull(Me.ADescription) Or Len(Me.ADescription) < 5 Then
            MsgBox "Please enter a description of at least 5 characters"
            Me.ADescription.SetFocus


        ElseIf IsNull(Me.onHand) Or (Me.onHand) < 0 Then
            MsgBox "On hand must have a value greater than 0"
            Me.onHand.SetFocus

        ElseIf IsNull(Me.ComboVendor) Then
            MsgBox "select one"
            Me.onHand.SetFocus

        ElseIf IsNull(Me.onOrder) Or (Me.onOrder) < 0 Then
            MsgBox "On order must have a value greater than 0"
            Me.onOrder.SetFocus

        ElseIf IsNull(Me.CostB) Or (Me.CostB) < 0 Then
            MsgBox "Cost must have a value greater than 0"
            Me.CostB.SetFocus

        ElseIf IsNull(Me.ListPriceB) Or (Me.ListPriceB) < (Me.CostB) Then
            MsgBox "List price must be greater than cost!"
            Me.ListPriceB.SetFocus

        Else
            Me.DataEntry = False
            Me.RecordsetClone.FindFirst "partID = " & stay
            Me.Bookmark = Me.RecordsetClone.Bookmark

            Call SaveCancel
            Call DisableInfo
            Call EnableNavigation

        End If
End Sub


'------------------------------------------------------------
' CmdNext
'------------------------------------------------------------
Private Sub CmdNext_Click()

    On Error Resume Next
    DoCmd.GoToRecord , "", acNext

End Sub


'------------------------------------------------------------
' CmdPrevious
'------------------------------------------------------------
Private Sub CmdPrevious_Click()

    On Error Resume Next
    DoCmd.GoToRecord , "", acPrevious

End Sub


'------------------------------------------------------------
' CmdFirst
'------------------------------------------------------------
Private Sub CmdFirst_Click()

    DoCmd.GoToRecord , "", acFirst

End Sub


'------------------------------------------------------------
' CmdLast
'------------------------------------------------------------
Private Sub CmdLast_Click()

    DoCmd.GoToRecord , "", acLast

End Sub

【讨论】:

    猜你喜欢
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多