Link to home
Start Free TrialLog in
Avatar of zyanj
zyanj

asked on

Simple Newbie VB questions

Let me know if I need to brak these up into multiple question...

I am a newbie trying to finish off a project.

Question #1
I am looking for a way to draw lines on a mschart  in VB. When I type a value into a textbox I need a horizontal line to be drawn on the Y axis. If I type in a 2 I need (0,2) to be graphed, and so on.

Question #2
How do I ensure that the data that can be inserted in a textbox are numerical values only.

Avatar of taycuong76
taycuong76

1)

2) Assuming that you have a textbox named Text1:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> asc("-") Then KeyAscii = 0
End Sub
For the 2nd question :-

If your textbox name is Text1, then you can use below code.

Private Sub Text1_Validate(Cancel As Boolean)
       
    If Not IsNumeric(Text1.Text) Then
        MsgBox "Enter a numeric value please.", vbExclamation, "Alert"
        Cancel = True
    End If
   
End Sub

This will validate the contents in Text1 textbox. It will not allow the user to move off the textbox, untill the user enters a numeric value. Everytime the user enters a non-numeric value, it will show an alert through the message box.

I hope this will do the needful.

Enjoy !!!
Avatar of zyanj

ASKER

taycuong76 ....

How do i apply this to a textbox that is apart of an array.

Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> asc("-") Then KeyAscii = 0
End Sub

Private Sub Text1_Change(Index As Integer)

CODE>>>CODE

End Sub
for your textbox that is a part of an array:

suppose if there are 3 textbox and if you want to put the condition only for one text box (which is of index 1) then:

Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
    If Index = 1 Then
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> Asc("-") Then
           KeyAscii = 0
        End If
    End If
End Sub

suppose if the condition is to be put for all textbox in the control array then there is no need for the index condition.

Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> Asc("-") Then
           KeyAscii = 0
        End If
End Sub
* You declare a n array of textbox and just add an If...then.... with Index in the Sub:

If Index = ..... Then
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 And KeyAscii <> Asc("-") Then
           KeyAscii = 0
        End If
End If

* To apply the "numeric textbox" to all of your textbox in a form, you need to use a class module and do something with "With Event" to reference to the class module.
Avatar of zyanj

ASKER

neither work
ASKER CERTIFIED SOLUTION
Avatar of taycuong76
taycuong76

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zyanj

ASKER

Sorry for the delay. Good answer. Thanks