How to limit the value of the textbox in vb 6.0 - vb6

I am doing a marks updating database system. I need to limit each of my textbox to be in the value of less than 100, when its over than 100 or when its not number, a message box will pop up and the data won't be save until the user change the mistake. How can I do it?

I agree with Hiren Pandya, but I thought I would add my own take as well.
Be aware that converting a string to a numerical value is not trivial, but the Val, CInt, CDBl etc. functions in VB6 can all give you behavior that close to what you want. (some of those links are for VB.Net, but can still be valuable). You want to make sure you are thinking about digit grouping, positive/negative, decimal separators, etc. when you are validating user input on your own. Most of the time, the built-in functions are good enough.
Private Sub Text1_Change()
On Error GoTo Err_Handler
Dim text As String
text = Text1.text
If IsNumeric(text) = True Then
'If you only want integers...
Dim value As Integer
value = Val(text)
If value <= 100 And value > 0 Then
'The value is good so whatever stuff you need to do
'And then leave the procedure
Exit Sub
End If
End If
'Let everything else fall through here...
Err_Handler:
MsgBox "Invalid input."
'Other stuff to prevent saving
End Sub

In the properties of the text box, set MaxLength to 2.
If you want a message, in the text box Change event, you could do...
If Len(txtBox.Text)>2 then msgbox...
then add your message in the messagebox.
I could go into more detail if you need it. Some thing like below...
Private Sub Text1_Change()
If Len(Text1) > 6 Then
Text1 = " "
MsgBox "Not more than six"
Text1.SetFocus
End If
End Sub

Related

Accept a name in Text Box, Print it when someone click on Print Button

I have a TextBox in VB and a Command Button. I want to print the string upon clicking on command button.
I am using the following code, please tell what I am doing wrong:-
Dim name As String
name = Val(Text1.Text)
MsgBox ("Welcome" & Str(name))
When I input a string in Textbox and click on command button, result is:
Welcome 0
Leave out the val() around your Text1.Text, val() returns the numbers up to the first symbol it can't recognize as a number used in a String. See the documentation. I guess you used a 0 in your string in the TextField or no number at all, both would return 0.
Additionally, it is unnecessary to cast your String name to a String since it is already a String so you can also leave out the Str().
The val function returns the numeric representation of its argument, otherwise it returns "0". It's a bit hard these days to find the official VB6 documentation, but you may want to check: https://en.wikibooks.org/wiki/Visual_Basic/VB6_Command_Reference#Val
So, in your example, if you enter any number in the Text1 textbox control, you will see it in your message box. If you enter any text, you will get "Welcome 0", as you do now. Therefore, you have to remove the val function from your code, like:
Dim name As String
name = Text1.Text
MsgBox ("Welcome " & name)
maybe even simplifying it to:
MsgBox("Welcome " & Text1.Text)
So you declared a string varaible namewhich you want to fill with the text from the Text1box. So you need to spare the val(...)part.
Second, as namealready represents a string, leave out the strin the message box:
name = Text1.Text
MsgBox ("Welcome " & name)

Excel Data Validation (with or without vba)

I have a column of cells eg "column C" whereby I would like to input a data validation.
I would like to restrict users to key in either "Done", "NA" or "any dates eg 01/10/2016, 31/03/2013 etc"
How do I create the above data validation? I am alright with using either Excel's "Data validation" function or VBA.
Thanks
Pretty simple bit of code here you could make to do whatever, highlight the cell or something, this just sets it back to blank. I would also desensitize them to caps and add some other options like "n/a"
Private sub worksheet_change(byval target as range)
If Target.Value <> "Done" And Target.Value <> "NA" And Not IsDate(Target.Value) Then
MsgBox ("incorrect entry, enter only blah or blah...")
Target.Value = ""
End If
End Sub

Only allow letters in textbox for MS Access

I have a text box in a form in MS Access. I just want users to enter Letters and spaces. No special characters.
I have implemented the following on KeyPress Event. I am wondering is there any better way to implement the same.
Private Sub txtName_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii <> 32 Then 'Not Backspace (important for error correction) and not a Space
If (KeyAscii < 65 Or KeyAscii > 90) And (KeyAscii < 97 Or KeyAscii > 122) Then 'Allowing lower and upper case
Beep 'Let the user know they hit an illegal key
KeyAscii = 0 'Don't let the keystroke through
End If
End If
End Sub
Consider using the BeforeUpdate event of the textbox which is the trigger usually used to validate entry as you have the facility to cancel the update:
Private Sub txtWordsOnly_BeforeUpdate(Cancel As Integer)
If Me.txtWordsOnly Like "*[0-9]*" Or Me.txtWordsOnly Like "*[##$%*^&?()<>/\'""!]*" Then
MsgBox "Invalid entry. Please select only words," _
& " NOT numbers or special characters.", vbInformation
Cancel = -1 ' OR Cancel = True
Me.txtWordsOnly.Undo
End If
End Sub
Use a validation rule.
Allen Browne has a highly useful list of Validation rules here:
http://allenbrowne.com/ValidationRule.html
Letters and spaces only
Is Null Or Not Like "*[!a-z OR "" ""]*"
Punctuation and digits rejected.

VB 6.0 - Formating Textbox "dd/MM/yy"

I've made a question previously about this but I didn't express really what I wanted. I have a textbox that requires the user to write a Date in it. Then he can press a button and generate a report based on the entries on some other textboxes. It is really important that the date in the generated report is in this format "dd/MM/yy".
I use this code :
Dim a as String
a = Format(Textbox1.text, "dd/MM/yy")
Everything is working fine except when the user types a date in this format "dd.MM.yy" e.g 15.05.15 . When this happens, the date in the generated report is always "30/12/99". Can some1 help me understand why this is happening ?
EDIT: Regarding the Duplicate Question. In the previous question I asked how I can extract characters from a textbox and form a date in the format I want. This question is about a specific problem that happens when a specific type of format is used in the textbox (dd.MM.yy) that prints a specific date always (30/12/99).
30/12/1899 is the Date equivalent of 0, this is what you get when you try to convert a String which is not in a correct date format to a Date.
To verify this, type any old rubbish into your text box and you will get 30/12/99 as output.
Regarding using a DateTimePicker control, see my answer to your other question here
you can chose which keys you allow in the textbox.
for example as follows:
'1 form with:
' 1 textbox : name=Text1
Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
' Caption = CStr(KeyAscii)
KeyAscii = DateOnly(KeyAscii)
End Sub
Private Function DateOnly(intKey As Integer) As Integer
Dim intResult As Integer
intResult = intKey
Select Case intKey
Case vbKeyBack 'allow backspace
Case vbKey0 To vbKey9 'allow numbers
Case 45 'allow -
' Case 46 'change . into /
' intResult = 47
Case 47 'allow /
Case Else 'dont allow anything else
intResult = 0
End Select
DateOnly = intResult
End Function
this just limits which keys the user can enter, you will still have to pay attention to other invalid inputs
[EDIT]
I added Case 45 to the code above.
In the select case you specify what happens to each key input:
you allow the key unaltered by specifying nothing, as i did with the / (ascii value 47)
you can change the key to another key, as i did with the . (ascii value 46)
you can reject the key input by setting it to 0, as i did with all other keys (case else)
You can find out which ascii value a specific key has by uncommenting the first line in Text1_KeyPress so the ascii value will show in the form caption
The IsDate Function is also useful. This will check if the date entered is valid, and return True if it is.
IsDate ("21/05/2015") 'returns 'True'
IsDate ("21.05.2015") 'returns 'False'
You could use the Replace function to change "." to "/"
mydate = Replace("21.05.2015", ".", "/") ' gives "21/05/2015"

How to save the text box values into an array?

I need to enter values in text box and when ever i press enter i have to save the text box values into an array.How can i achieve this functionality?
I would use a separate counter variable to redefine the size of the array, like this:
Option Explicit
Dim myArr() As String '~~~ dynamic array
Dim lngCnt As Long '~~~ a counter variable that keep track of the index
' inital stage..
Private Sub Form_Load()
lngCnt = 0
End Sub
' on KeyPress
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then '~~~ if Enter Key is pressed..
ReDim Preserve myArr(lngCnt) '~~~ reclare the array with the new size, while preserving any elements it may contain
myArr(lngCnt) = Text1.Text '~~~ store the line
Text1.Text = "" '~~~ empty the textbox, so that you could type the next line
lngCnt = lngCnt + 1 '~~~ increment the counter, which we would use as size during the next keypress
End If
End Sub
' to display the elements
Private Sub Command1_Click()
Dim i As Long
For i = LBound(myArr) To UBound(myArr) '~~~ loop through the elements(from Lowerbound to Upperbound)..
Debug.Print myArr(i) '~~~ ..and display the item.
Next
End Sub
If you just want a new entry to be added each time they press the button, then use something like:
Redim Preserve YourArray(LBound(YourArray) To UBound(YourArray) + 1)
YourArray(UBound(YourArray)) = TextBox.Text
Note that this can get very slow and inefficient when the array contains large numbers of items as it's reallocating memory each time.
A better method would involve expanding the size of the array in chunks, while keeping track of the last valid entry.
Just give same name to all the text Boxes
The short answer is to use split to break the string up (you will need to tell the user what char to use to split on).
Long answer don't change the user interface to a repeater and have them use an input per value start here http://blogs.microsoft.co.il/blogs/basil/archive/2008/08/20/javascript-repeater-control-datarepeater-using-jquery-presenter-1-0-8-uicontrols-library.aspx
Else you will be debugging for ever.

Resources