Microsoft Access:将组合框数据从表单保存到表

时间:2017-05-23 12:39:43

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

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

目前,我将表格的设计视图中的那些字段的数据类型设置为“短文本”。我想知道问题是我是否需要将数据类型设置为其他类型,或者是否有其他原因导致此问题。

1 个答案:

答案 0 :(得分:0)

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