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

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)

Related

padding the middle of a string

I have a Textbox field that takes in a string with a character limit of 10. I would like to implement a short hand version because there are a lot of zeros in the string that have to be entered. so an example of the string is T000028999. but id like to key in T28999 and have the zeros padded between the "T" and the "28999" and show up as the T000028999 string in the Textbox field.
Is this even possible?
I've tried searching examples on google and have only found ways to pad the beginning and end of the string.
You want to keep the first character, so you can use oldString.Chars(0) to get that.
You want the remainder of the string: oldString.Substring(1), and you can pad it to the width you require with a character of your choice with PadLeft, like this:
Dim newString = oldString.Chars(0) & oldString.Substring(1).PadLeft(9, "0"c)
It would be a good idea to check that oldString is at least 1 character long before doing that otherwise the .Chars(0) will give an error.
Alternatively you could insert a new string of the required quantity of "0"s:
Dim newString = oldString.Insert(1, New String("0"c, 10 - oldString.Length))
A good place to perform the formatting would be in the control's Validating event handler. (The TextChanged event handler would not be a good place because it would interfere with the user's typing.)
Refs:
String.Chars[Int32] Property
String.Substring Method
String.PadLeft Method
String.Insert(Int32, String) Method
String Constructors

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"

Vb6 .text property for textbox required

I am trying to convert letters to numbers.
I have a sub which ensures only numbers are put into the textbox.
My questions is will the following code work. I have a textbox(for numbers) and combobbox(for letters)
Dim sha As String
Dim stringposition As Long
Dim EngNumber As Long
sha = "abcdefghifjklmnopqrstuvwxyz"
stringposition = InStr(1, sha, Mid(1, cmbEngletter.Text, 1))
MsgBox "stringposition"
EngNumber = (txtManuNo.Text * 10) + stringposition
My only question above would be will the multiplication work with a .text. I believe it won't because it is a string. Please advise then on how to deal with a situation.
You can use CLng() to convert a string to a Long variable
CLng() will throw an error though if it doesn't like the contents of the string (for example if it contains a non-numeric character), so only use it when you are certain your string will only contain numbers
More forgiving is it to use Val() to convert a string into a numeric variable (a Double by default)
I also suggest you look into the following functions:
Asc() : returns the ASCII value of a character
Chr$() : coverts an ASCII value into a character
Left$() : returns the first characters of a string
CStr() : convert a number into a string
I think in your code you mean to show the contents of your variable stringposition instead of the word "stringposition", so you should remove the ""
I do wonder though what you are trying to accomplish with your code, but applying the above to your code gives:
Dim sha As String
Dim stringposition As Long
Dim EngNumber As Long
sha = "abcdefghifjklmnopqrstuvwxyz"
stringposition = InStr(1, sha, Left$(cmbEngletter.Text, 1))
MsgBox CStr(stringposition)
EngNumber = (Val(txtManuNo.Text) * 10) + stringposition
I used Val() because I am not certain your txtManuNo will contain only numbers
To ensure an user can only enter numbers you can use the following code:
Private Sub txtManuNo_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyBack
'allowe backspace
Case vbKey0 To vbKey9
'allow numbers
Case Else
'refuse any other input
KeyAscii = 0
End Select
End Sub
An user can still input non-numeric charcters with other methods though, like copy-paste via mouse actions, but it is a quick and easy first filter

How to limit the value of the textbox in vb 6.0

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

Searching Data from DataGrid Control using ADODC in VB6.0

I'm a student doing my final year mini project and am facing a problem related to searching data in the datagrid.
The error I'm getting is :
Run-time error : '3001'
Arguments are of wront type, are out of acceptable range, or are in conflict with one another
The code is :
Private Sub Command1_Click()
Dim item As String
Adodc1.Recordset.MoveFirst
item = Text1.Text
Adodc1.Recordset.Find "L_No = " & item
If Adodc1.Recordset.EOF Then
MsgBox "Record Set not found"
End If
End Sub
The above code is working when the data I'm searching is only number.
For example
When I search the data on the basis of L_Id which is a License ID an Integer value the searching is done and I'm getting the result.
When I search the data on the basis of L_No which is a License Number a string value which consists of both numbers and alphabets I'm getting the above error.
Do I have to parse the value is text1.text or do anything else?
When the field you're searching in is not a numeric type, you'll want to delimit it with single quotes:
Adodc1.Recordset.Find "L_No = '" & item & "'"

Resources