Conditional increasing ID in ms access formular

40 Views Asked by At

i have a ms access formular where there are given several Information. For "Status" Combobox there are several options like "1","2","3","4". If "4" is selected in "cbx_Status" then I want to add in Textbox "txt_ID_Order" an automatically increased ID and give an Order Time in textbox "txt_OrderTime". That's why I wrote this and works well:

 Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Me.cbx_Status.Value = "4" Then
            Me.txt_OrderTime = Now()
            Me.txt_ID_Order = DMax("[ID_Order]", "tblX") + 1
    Else: 
          Me.txt_ID_Order=""
          Me.txt_OrderTime = ""
        End If
    End Sub

However, if Status "4" is for some reason changed and again selected , I want to keep that old ID. But right know wenn i do that, it's still increasing ID. How can I fix it? Thanks

1

There are 1 best solutions below

0
Gustav On

Check for a value:

Private Sub Form_BeforeUpdate(Cancel As Integer)

    If Me!cbx_Status.Value = "4" Then
        If IsNull(Me!txt_OrderTime.Value) Then
            Me!txt_OrderTime.Value = Now()
            Me!txt_ID_Order.Value = DMax("[ID_Order]", "tblX") + 1
        End If
    Else
        Me!txt_ID_Order.Value = Null
        Me!txt_OrderTime.Value = Null
    End If

End Sub

Not sure about the logic though; if you select anything else than 4, the textboxes will be cleared.