Using + or- key to increment number or date in a text box

gem1204

Registered User.
Local time
Today, 12:33
Joined
Oct 22, 2004
Messages
54
How do you increment a numeric or date field using the plus or minus keys with a sub procedure only. I've created a procedure that increases or decreases a value in a numeric or date field by using the plus or minus keys, but I can't store the value in control without using the after update event and I can't keep the focus on the current control without using cancel in the onExit event. Can anyone figure out how to do this all in one procedure?
I have tried to attach the database to this post. If the database got posted you can open the database and look at the code and see what I'm talking about.
If the attachment didn’t make it, could someone please tell me how to attach a file. My file was only 50 KB
:confused:
 

Attachments

Here is a 2000 version
 

Attachments

increment number

Hello!

I have had the same problem. This is maybe a prehystoric solution, but it works.
I have made a form that shows the number from the last record. Then I included it as a not linked subform (name:counter) into the form I needed the increased number. Add formula to the field to display (counter+1 in Default Value) and make subform not visible.
But you must then close the form to use this for the next record.
It works and you can change the number anytime if you need to start again with number one or so.

I'd like to know a more elegant way to solve this.

Sara
 
Thanks for the Access 2000 version but when I try to open the database I get the following message:

Microsoft Access was unable to create the Database window.
Please upgrade to a new version of Microsoft Internet Explorer.

Any idea as to why I'm getting this error message??
 
No I don't have any idea what the problem is. I tried downloading the zip that was posted. I'm sorry you couldn't open it. I hope others don't have the same problem
 
floyding said:
Hello!

I have had the same problem. This is maybe a prehystoric solution, but it works.
I have made a form that shows the number from the last record. Then I included it as a not linked subform (name:counter) into the form I needed the increased number. Add formula to the field to display (counter+1 in Default Value) and make subform not visible.
But you must then close the form to use this for the next record.
It works and you can change the number anytime if you need to start again with number one or so.

I'd like to know a more elegant way to solve this.

Sara

Sarah and everyone else - thanks for your help.
There is a more elegant way to increment a number. I also posted on another site and there are some good ways to sove this. If you download my attachment It will do what you want with some modification to suit your particular needs. You can also go to utteraccess.com and look at those posts there under my username GEM1204
http://www.utteraccess.com/forums/s...=596220&page=0&view=collapsed&sb=5&o=&fpart=1
 
Hello,

This is one more piece of code that you can use to set up date.
[*] - enters current date.
This code should be saved in a module.
Code:
Public Sub SetDate(tboControl As TextBox, KeyAscii As Integer)
    Dim strCharacter As String
    
    strCharacter = Chr(KeyAscii)
    
    If strCharacter = "*" Then
       tboControl.Value = Date
       KeyAscii = 0
    ElseIf strCharacter = "-" Then
       If IsNull(tboControl.Value) Then
          tboControl.Value = Date
       Else
          tboControl.Value = tboControl.Value - 1
       End If
       KeyAscii = 0
    ElseIf strCharacter = "+" Then
       If IsNull(tboControl.Value) Then
          tboControl.Value = Date
       Else
          tboControl.Value = tboControl.Value + 1
       End If
       KeyAscii = 0
    End If
End Sub
In a textbox on a form on KeyPress event you should write the following:
Code:
Private Sub tbo_KeyPress(KeyAscii As Integer)
    SetDate Me.tbo, KeyAscii

    If KeyAscii = 0 Then Call tbo_AfterUpdate 'If this event exists
End Sub
 
Thanks Gem1204! Must say I am new here and I really don't know why I have never tryed to look for access help before ?!? :)
 
you can use the dmax function to get the highest check number, instead of going through all the pain.

highestChk = DMax("[CkNumber]", "tblRegister", "IsNumeric([CkNumber]) = -1")
 
A bit modified version

Code:
Public Sub SetTxtBoxValue(tboControl As TextBox, KeyAscii As Integer, Optional AllowNegative As Boolean = False, Optional tIsDate As Boolean = True, Optional NullAfterZero As Boolean = False)
    Dim strCharacter As String
    
    strCharacter = Chr(KeyAscii)
    
    If tIsDate Then
        
        If strCharacter = "*" Then
           tboControl.Value = Date
           KeyAscii = 0
        ElseIf strCharacter = "-" Then
           If IsNull(tboControl.Value) Then
              tboControl.Value = Date
           Else
              tboControl.Value = tboControl.Value - 1
           End If
           KeyAscii = 0
        ElseIf strCharacter = "+" Then
           If IsNull(tboControl.Value) Then
              tboControl.Value = Date
           Else
              tboControl.Value = tboControl.Value + 1
           End If
           KeyAscii = 0
        End If
        
    Else
        If AllowNegative Then
            If strCharacter = "-" Then
               If IsNull(tboControl.Value) Then
                    tboControl.Value = 0
                Else
                    tboControl.Value = tboControl.Value - 1
               End If
               KeyAscii = 0
               
            ElseIf strCharacter = "+" Then
               If IsNull(tboControl.Value) Then
               tboControl.Value = 0
               Else
                  tboControl.Value = tboControl.Value + 1
               End If
               KeyAscii = 0
            End If
        
        Else
            If strCharacter = "-" Then
               If IsNull(tboControl.Value) Then
               tboControl.Value = 0
               Else
                    If NullAfterZero Then
                        If tboControl.Value = 0 Then
                        tboControl.Value = Null
                        Else
                        tboControl.Value = tboControl.Value - 1
                        End If
                    Else
                        If tboControl.Value = 0 Then
                        Else
                        tboControl.Value = tboControl.Value - 1
                        End If
                    End If
               End If
               KeyAscii = 0
            ElseIf strCharacter = "+" Then
               If IsNull(tboControl.Value) Then
                    tboControl.Value = 0
               Else
                    tboControl.Value = tboControl.Value + 1
               End If
               KeyAscii = 0
            End If
        
        End If
    End If
End Sub

Works for both date and numeric feilds.
For numeric feilds checks to allow negative values to be entered , check if Null should come after - is pressed if the last value is zero.

hope this might help you.
 

Users who are viewing this thread

Back
Top Bottom