Access 2010 - VBA - 使用表单上的文本框编辑基础表数据

时间:2015-03-10 14:34:19

标签: vba ms-access-2010

我使用了太多的布尔指标,我确信它非常低效/愚蠢......

目前在Access数据库中,我有许多用于编辑基础记录的表单。这些表单上的文本框未绑定到基础表。如果数据可由用户编辑,我不希望将表单或其任何控件直接绑定到基础表(用户的人为错误较少)。相反,我为每个包含可编辑信息的控件都有一个布尔值。

用户进入“编辑模式”,更改信息(布尔现在等于true),单击“保存更改”,查看更改并接受,然后运行相关查询以反映这些更改。我喜欢这种事件顺序,然而,我正在用40个左右的可编辑控件创建越来越复杂的形式,因此有40个布尔变量。

有人想到更好的选择吗?我可以使用控件(主要是文本框)的属性吗?

CODE:     Private Sub CommentsText_AfterUpdate()

    If Nz(Me.CommentsText) = "" Then
        CommentsEdit = False
    Else
        CommentsEdit = True
    End If
End Sub

    'Within the 'save changes' method
    If CommentsEdit Then
        CommentsEdit = False
        sql = "Update [General-CN] Set [Comments] = '" & Left(Me.CommentsText, 250) & "' Where [ID ( G )] = " & Me.[GeneralPK] & ";"
        DoCmd.RunSQL (sql)
    End If

1 个答案:

答案 0 :(得分:0)

通常,数据输入过程中的数据验证通常是根据我的经验,在开发人员跪下之前或之后。

从您的问题中,我可以感觉到核心是保持数据库的完整性。不幸的是,没有办法绝对保存您的数据库。如果您允许用户访问它,您只能使用一些提示:

1 - 如果可能的话,使用带有已定义条目的组合框,而不是使用文本框,其中用户可以自由地对任何内容进行数字处理(例如,想一个收集有关以不同方式编写的同一问题的数据的专家系统!!!)

2 - 在将数据写入数据库之前检查数据的完整性和一致性(例如类型)

3 - 当数据只是布尔值(标志)时,您可以使用开关,单选按钮或复选框。

从数据输入的角度来看,这些提示有助于开发更友好,更快捷的用户界面。

在此之后,我可以为您提供另一种验证数据的方法。 如果要在保存之前向用户显示数据,可以创建使用无界文本框输入数据的掩码。 然后,当他单击“保存”按钮时,您可以显示第二个表单,在附加模式下打开,在该表单中显示带有与数据库绑定的文本框的数据。

如果他接受了数据,您可以保存,否则您可以取消保留数据库的数据条目。我在这里发布一些代码行,其中一个示例来自我的应用程序。这是我管理联系人的部分

表单允许输入联系人数据

'------------------------------------------------------
' Temp variables to store entries before saving /cancelling
'------------------------------------------------------
Dim bolValidationErr As Boolean

Dim m_vntLastName As Variant
Dim m_vntFirstName As Variant
Dim m_vntFC As Variant
Dim m_vntVATNum As Variant
Dim m_vntAddress As Variant
Dim m_vntCity As Variant
Dim m_vntZIP As Variant
Dim m_vntCountry As Variant
Dim m_vntPhone As Variant
Dim m_vntFAX As Variant
Dim m_vntEMail As Variant
Dim m_vntNote As Variant
Dim m_vntContactType As Variant

'------------------------------------------------------
' Suppress error "Impossible to save the record...
'------------------------------------------------------
Private Sub Form_Error(DataErr As Integer, Response As Integer)
    If DataErr = 2169 Then
        Response = True
    End If
End Sub

'------------------------------------------------------
' W/o customer last name, cancel saving
'------------------------------------------------------
Private Sub Form_Beforeupdate(Cancel As Integer)
On Error GoTo Err_Form_BeforeUpdate
    Dim strType As String
    Dim bolNewContact As Boolean
    Dim intErrFC As Integer
    Dim intErrVATNum As Integer
    Dim intErrFullName As Integer
    Dim strErrMsg As String
    Dim intAnswer As Integer

    bolValidationErr = False

    '------------------------------------------------------
    ' If LastName is missing, cancel data saving
    ' Cancel = True cause the raise of the error 
    ' "You can't save record at this time..."
    ' SetWarnings doesn't work. It's needed to intercept with Form_Error event
    '------------------------------------------------------
    If HasNoValue(Me.txtLastName) Then
        strErrMsg = "Put here your error msg"
        intAnswer = MsgBox(strErrMsg, vbOKOnly + vbExclamation, "Entry Error")
        Cancel = True                   ' Cancel db update
        Call BackupCurrentData          ' Store data input until now          
        bolValidationErr = True         ' Unvalid data
        Exit Sub
    End If

Exit_Form_BeforeUpdate:
    Exit Sub

Err_Form_BeforeUpdate:
    GoTo Exit_Form_BeforeUpdate

End Sub

'------------------------------------------------------
' Store the content of textboxes for restoring them in case
' of cancelling
'------------------------------------------------------
' If the record is new, if the BeforeUpdate event is cancelled, 
' NULL values are restored (in fact there were no data!!!)
'------------------------------------------------------
Private Sub BackupCurrentData()
    m_vntLastName = Me.txtLastName
    m_vntFirstName = Me.txtFirstName
    m_vntFC = Me.txtFC
    m_vntVATNum = Me.txtVATNum
    m_vntAddress = Me.txtAddress
    m_vntCity = Me.txtCity
    m_vntZIP = Me.txtZIP
    m_vntCountry = Me.txtCountry
    m_vntPhone = Me.txtTelNum
    m_vntFAX = Me.txtFax
    m_vntEMail = Me.txtEmail
    m_vntNote = Me.txtNotes
    m_vntContactType = Me.cmbContactType

End Sub

'------------------------------------------------------
' Restore contents of textboxes before cancelling operation
'------------------------------------------------------
Private Sub RestoreCurrentData()
    Me.txtLastName = m_vntLastName
    Me.txtFirstName = m_vntFirstName
    Me.txtFC = m_vntFC
    Me.txtVATNum = m_vntVATNum
    Me.txtAddress = m_vntAddress
    Me.txtCity = m_vntCity
    Me.txtZIP = m_vntZIP
    Me.txtCountry = m_vntCountry
    Me.txtTelNum = m_vntPhone
    Me.txtFax = m_vntFAX
    Me.txtEmail = m_vntEMail
    Me.txtNotes = m_vntNote
    Me.cmbContactType = m_vntContactType
End Sub

我认为此代码可以根据您的需求进行调整。 变量名称足够自我描述 无论如何,如果您需要进一步的帮助,请随时与我联系。

再见,

奇才: - )