Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can I Increment/Decrement a Form Text Field? (a2k (9.0.6926) SP-3 Jet 4.0 SP-7)

    I have a form with text field storing numeric values.

    Looking for vba code to increment/decrement value with + and

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can I Increment/Decrement a Form Text Field? (a2k (9.0.6926) SP-3 Jet

    Use the On Key Down event of the text box:

    Private Sub txtSomething_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyAdd
    txtSomething = Nz(txtSomething, 0) + 1
    KeyCode = 0
    Case vbKeySubtract
    txtSomething = Nz(txtSomething, 0) - 1
    KeyCode = 0
    End Select
    End Sub

  3. #3
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can I Increment/Decrement a Form Text Field? (a2k (9.0.6926) SP-3 Jet

    That worked great.

    How about another example?

    txtSomething contains a deposit number with the following value/format 2004-03005 which reads, this is the 5th deposit in March 2004

    All 10 charachers are stored.

    How would I adjust your previous solution to increment/decrement this deposit number with + and

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Can I Increment/Decrement a Form Text Field? (a2k (9.0.6926) SP-3 Jet

    Isn't that dangerous? With a deposit number like that, I'd think you would assign it automatically, without giving the user the ability to change it.

    Anyroad, you could try something like the following. It doesn't adjust the year or month, it expects that a correct entry is present.

    Private Sub txtSomething_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim strSeq As String
    Dim lngSeq As Long

    ' Get out if blank
    If IsNull(Me.txtSomething) Then
    Exit Sub
    End If

    Select Case KeyCode

    Case vbKeyAdd
    strSeq = Right$(Me.txtSomething, 3)
    lngSeq = Val(strSeq) + 1
    If lngSeq > 999 Then
    MsgBox "You have run out of deposit numbers for this month.", vbInformation
    Else
    strSeq = Format(lngSeq, "000")
    Me.txtSomething = Left(Me.txtSomething, Len(Me.txtSomething) - 3) & strSeq
    End If
    KeyCode = 0

    Case vbKeySubtract
    strSeq = Right$(Me.txtSomething, 3)
    lngSeq = Val(strSeq) - 1
    If lngSeq < 1 Then
    MsgBox "Deposit number must be positive.", vbInformation
    Else
    strSeq = Format(lngSeq, "000")
    Me.txtSomething = Left(Me.txtSomething, Len(Me.txtSomething) - 3) & strSeq
    End If
    KeyCode = 0

    End Select
    End Sub

  5. #5
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can I Increment/Decrement a Form Text Field? (a2k (9.0.6926) SP-3 Jet

    Hi Hans

    I agree with you, this is an off line app with a operator doing a datasheet form collecting information, much of the info repeats and the thought was to allow duping from pervious fields and adjusting up and down with + and - keys.

    I couldn't get the following working in your response, so I added it to each case stmt

    ' Get out if blank
    If IsNull(Me.txtSomething) Then
    Exit Sub
    End If

    For others that are interested in incrementing/decrementing fields there is demo db AutoUpDownVer2.mdb at :

    http://www.lebans.com/

    Thanks, John

    ================================================== =
    Private Sub Batch_KeyDown(KeyCode As Integer, Shift As Integer)

    Dim strSeq As String
    Dim lngSeq As Long

    Select Case KeyCode

    Case vbKeyF6

    SendKeys "^"""
    ' Cancel standard action of key
    KeyCode = 0
    Exit Sub

    Case vbKeyAdd

    If IsNull(Me.Batch) Then
    KeyCode = 0
    Exit Sub
    End If

    strSeq = Right$(Me.Batch, 3)
    lngSeq = Val(strSeq) + 1
    If lngSeq > 999 Then
    MsgBox "You have run out of deposit numbers for this month.", vbInformation
    Else
    strSeq = Format(lngSeq, "000")
    Me.Batch = Left(Me.Batch, Len(Me.Batch) - 3) & strSeq
    End If
    KeyCode = 0

    Case vbKeySubtract

    If IsNull(Me.Batch) Then
    KeyCode = 0
    Exit Sub
    End If

    strSeq = Right$(Me.Batch, 3)
    lngSeq = Val(strSeq) - 1
    If lngSeq < 1 Then
    MsgBox "Deposit number must be positive.", vbInformation
    Else
    strSeq = Format(lngSeq, "000")
    Me.Batch = Left(Me.Batch, Len(Me.Batch) - 3) & strSeq
    End If
    KeyCode = 0

    End Select

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •