Control Box Rules

P

PL

Hi

For Acess 2003. In designing Forms:

1) How do I ensure only number is entered into a control box?

2) For a Bound Control, I wish to set the Default Value as 2, but in Form
View when entering a new record, the Default Value remains at 0. How do I
correct this?

Thank you
 
J

Jeanette Cunningham

Hi PL,
1) if the text box is bound to a number field, access will show an error
when a non-number is entered - although it will allow some things that can
be converted to a number. If you want only integers between 0 and 9, there
are examples of functions that check this.
Here's an example.
On the BeforeUpdate event of the textbox, put -->
If Not IsNumeric = True Then
Cancel = True
End If


The code below goes in to a standard module-->

Public Function IsNumericCheck(ByVal strTestChar As String) As Boolean
On Error GoTo Err_Handler
pstrProc = "IsNumericCheck"

Dim intI As Integer 'loop counter
Dim lngLen As Integer 'string length
Dim intTestChar As Integer
Dim strMsg As String


Const conFirstDigit = 48
Const conLastDigit = 57

lngLen = Len(strTestChar)

For intI = 1 To lngLen
intTestChar = Asc(UCase(strTestChar))

If (intTestChar >= conFirstDigit And intTestChar <= conLastDigit)
Then
IsNumericCheck = True
Else
IsNumericCheck = False
strMsg = "Only numbers 0 to 9 are allowed. " _
& vbCrLf & vbCrLf _
& "Correct entry or press Esc to cancel."
MsgBox prompt:=strMsg, buttons:=vbInformation, TITLE:=pstrT

Exit Function
End If
Next intI

Exit_Handler:
Exit Function
Err_Handler:
MsgBox Err.Number & " " & Err.Description
Resume Exit_Handler
End Function

2)
You can set the default value using the control's property dialog, on the
data tab, look for default value and put 2 there.
Also open the table that the bound control has for its control source and
see if that field has a default value of 0 - access often puts a 0 for the
default value for a number field in Access 2003. Delete the default value
from the table and save the table.


Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia
 
L

Linq Adams via AccessMonster.com

Or you can simply not allow non-numeric characters to be entered:

Private Sub YourTextBox_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode

Case 48 To 57
'Numerical characters are allowed

Case vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft, vbKeyTab
'Allow these keys to be used

Case vbKeyNumpad0, vbKeyNumpad1, vbKeyNumpad2, vbKeyNumpad3, vbKeyNumpad4,
vbKeyNumpad5, vbKeyNumpad6, vbKeyNumpad7, vbKeyNumpad8, vbKeyNumpad9
'Allow input from Numbers Keypad

Case Else
'Don't allow any other keys to be used
KeyCode = 0
End Select

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top