1

我对 MS Access 很陌生,这个问题让我很困惑。我想我错过了明显的东西,我只是无法确定它是什么。提前感谢您提供的任何帮助。

目前我有一张桌子:tblAll包含以下字段:

  • ID(PK)
  • PackageNo
  • PackageDescription
  • Activity
  • ProductivityRateLow
  • ProductivityRateHigh
  • Unit
  • TimePeriod
  • Notes
  • Source
  • LeadTimeLow
  • LeadTimeHigh

我的表单包含组合框:

  • cboPackageNumber
  • cboActivity

和文本框:

  • txtRateHigh
  • txtRateLow
  • txtUnit
  • txtTimePeriod
  • txtSource
  • txtNotes
  • txtMax
  • txtMin

我添加了三个按钮:

  • cmdSave
  • cmdAdd
  • cmdClose

用户首先从 中选择包编号cboPackageNumber,然后从cboActivity与该包关联的活动列表中选择活动。选择活动后,其余文本框将填充该活动的数据。

这一切都非常适合从表中查找记录。但是,我希望用户能够将表单中记录的更改保存回tblAll. 我还希望能够添加新记录。目前,如果表单上的文本框字段中的任何信息发生更改,则不会将其保存回表中。

我从另一个来源复制的代码cmdSave,这似乎是我试图通过保存功能实现的。

很抱歉再次在这里转储我的所有代码,我希望这能让我更容易理解我在哪里以及我哪里出错了。

Option Compare Database
Private Saved As Boolean
Option Explicit

Private Sub cboActivity_AfterUpdate()
    Me.txtRateHigh = [cboActivity].Column(5)
    Me.txtRateLow = [cboActivity].Column(4)
    Me.txtUnit = [cboActivity].Column(6)
    Me.txtTimePeriod = [cboActivity].Column(7)
    Me.txtSource = [cboActivity].Column(9)
    Me.txtNotes = [cboActivity].Column(8)
    Me.txtMax = [cboActivity].Column(11)
    Me.txtMin = [cboActivity].Column(10)
End Sub

Private Sub cboPackageNumber_AfterUpdate()
    Dim strSql As String
    strSql = "SELECT o.ID, o.PackageNo, o.PackageDescription, o.Activity, o.ProductivityRateLow, o.ProductivityRateHigh, o.Unit, o.TimePeriod, o.Notes, o.Source, o.LeadTimeLow,  o.LeadTimeHigh " & vbCrLf & _
    "FROM tblALL AS o" & vbCrLf & _
    "WHERE o.PackageNo = '" & cboPackageNumber.Value & "'" & vbCrLf & _
    "ORDER BY o.PackageNo;"
    Debug.Print strSql
    cboActivity.RowSource = strSql

    If cboPackageNumber > 0 Then
        cboActivity.Visible = True
        Me.cboActivity.Requery
    Else
        cboActivity.Enabled = False
    End If
End Sub

Private Sub cmdSave_Click()
   Saved = True
   DoCmd.RunCommand (acCmdSaveRecord)
   Me.cmdSave.Enabled = False
   Saved = False
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim Response As Integer
    If Saved = False Then
        Response = MsgBox("Do you want to save the changes on this record?", vbYesNo, "Save Changes?")
        If Response = vbNo Then
           Me.Undo
        End If
        Me.cmdSave.Enabled = False
    End If
End Sub

Private Sub cmdAdd_Click()

    On Error GoTo cmdAdd_Click_Err

    On Error Resume Next
    DoCmd.GoToRecord , "", acNewRec
    If (MacroError <> 0) Then
        Beep
        MsgBox MacroError.Description, vbOKOnly, ""
    End If

cmdAdd_Click_Exit:
    Exit Sub

cmdAdd_Click_Err:
    MsgBox Error$
    Resume cmdAdd_Click_Exit
End Sub

Private Sub Form_Dirty(Cancel As Integer)
    Me.cmdSave.Enabled = True
End Sub
4

0 回答 0