2

I'm validating my datasheet programmatically because it's not possible to do it the "normal" way (linked tables). The "normal" way to tell the user they entered a badness is to punish them with MsgBox popups - I want to display a message in a label on my form (this works) and change the background color of the offending cell. I can only change cell colors through Conditional Formatting (this works), so I set up some rules to highlight cells with leading spaces.

So now the last piece of the puzzle is to change the value of offending cells with a leading space so it gets highlighted via Conditional Formatting. But that part isn't working. When I change the value of the cell, nothing happens. I feel as if my datasheet needs to be told to update itself with my new values.

Here's what I have so far:

Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim isValid As Boolean
    isValid = myDataSheetRow_IsValid()
    ' if the page is valid, don't cancel the update
    Cancel = Not isValid
End Sub

Private Function myDataSheetRow_IsValid() As Boolean
    ' validate our data as it cannot be validated in the GUI due to the way linked tables work
    myDataSheetRow_IsValid = True
    Dim myErrMsg As String
    myErrMsg = ""

    'evaluate my column called ABC
    If (IsNull(Me.ABC)) Then
        myErrMsg = myErrMsg + "ABC cannot be blank" + vbNewLine

        '---------------------
        'PROBLEM START
        '---------------------
        'Set the cell value to what it was plus a leading space
        'Because I have set up conditional formatting on the datasheet
        'So that leading spaces highlights the cell for attention

        Me.ABC= " " + Me.ABC 'trigger conditional formatting ??
        '---------------------
        'PROBLEM END
        '---------------------
    End If
    ' ... more validating ...
    ' done validating
    Call Me.Parent.UpdateErrorMsgArea(myErrMsg)
    If (Len(myErrMsg) > 1) Then
        myDataSheetRow_IsValid= False
    End If
End Function

How do I set the value of a datasheet cell programmatically?

I've tried it this way and it's not working - nothing is changed on the datasheet, it's as if the datasheet isn't updating itself to display my new programmatically changed value.

4

1 回答 1

1

Me.ABC代码在为 Null时运行以下语句...

Me.ABC= " " + Me.ABC

我的印象是您希望该语句在 中存储一个空格Me.ABC,但它实际上存储的是 Null。

原因是向某事物添加 Null 会给您带来 Null。某个东西是数字还是文本字符串都没有关系。添加Null 仍然会给你 Null。

但是,如果您(使用&运算符) Null 与文本字符串连接,则会返回文本字符串。

Me.ABC= " " & Me.ABC

但是由于您Me.ABC当时知道是 Null,因此串联并没有任何好处。只分配空格字符...

Me.ABC = " "
于 2015-06-10T19:31:27.933 回答