Link to home
Start Free TrialLog in
Avatar of m_durnell
m_durnellFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB and communication between machine and user

Hi

A while ago I wrote a program in Pascal, the program did the following:

It asked certain questions which the user would answer and then it would throw them back such as:

Hello, What is your name?

(reply) Peter

How old are you?

(reply) 24

I am older than you Peter

Where are you living?

(reply) Swansea

Swansea is a nice place do you like it there?

etc etc


In pascal it was easy, How can I gpo about this within VB6? and also set it up so that if a user asked a question the program could respond?

Any help appreciated

Regards

Mark

Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Private Sub Form_Load()
Dim s As String, myage As Integer
myage = 30
s = InputBox$("Hello, What is your name?", "Name")
s = InputBox$("How old are you? " & s, "hello")
If myage > s Then
    MsgBox "I am older than you"
Else
    MsgBox "I am younger than you"
End If
en sub
and so on...
Avatar of rpai
rpai

Are the questions asked based on a criteria? I mean to ask are there a standard list of questions that pop-up when any user logs in? Or are there different sets of questions for a limited set of users? How does this conversation end?
Are there some sort of validations that you wish to have in place?
Avatar of m_durnell

ASKER

hi,

What I really want to do is have two text boxes on a form so that for instance, when the program is run the text box (txtBox1)

says Hello I am Mark, Please enter your name then press the enter key.

The user enters a response in txt box2 for instance Kevin and presses the enter key

txtbox1 then says Hello Kevin how are you today?

The user answers the prompt and presses the enter key

And so on and on with different responses assigned I would guess to different variables such as age, name, sex, live, nationality etc etc etc and the program would then "ask" and "comment" on the responses etc. that I would enter into the program at source.

If I could get this far I would then like to develop it further with case statements etc, but that would come later.  I found it easy to do in Turbo Pascal I would like to be able to do this now in VB6.

Regards

Mark



The code mentioned below might be of help for a start.(The code assumes that you are hardcoding the questions into your code). You need to do validation at each point before proceeding with the next question.

------------------------Start Code-------------------------
Option Explicit
Dim iClick As Integer


Private Sub Command1_Click()
iClick = iClick + 1
Select Case iClick
    Case 1
        If ValidateName(Text2.Text) Then
            Text1.Text = "Please enter your age"
        Else
            Text1.Text = "Please enter your true name"
            iClick = iClick - 1
        End If
    Case 2
        If IsNumeric(Text2.Text) Then
            Text1.Text = "Please enter you phone number"
        Else
            Text1.Text = "Please enter a valid age"
            iClick = iClick - 1
        End If
End Select
End Sub


Private Sub Form_Load()
Text1.Text = "My name is Mark. Please enter your name"
Text2.Text = ""
iClick = 0
End Sub

Private Function ValidateName(s As String) As Boolean
ValidateName = True
If s = "" Then
    ValidateName = False
ElseIf Len(s) < 2 Then
    ValidateName = False
'-- Check if there are any numeric value in the name. Then not a Valid Name. And so on...
End If

End Function
------------------End Code---------------------------------
Hi

I tried Richies code and it worked fine with a msgbox however I want it to display within a text box(s)
called

txt1.text and txt2.text

Also I want the program to "react" when the enter key is pressed.

Regards

Mark
replace msgbox for text2.text in the code.
to emulate enter key:

Private Sub text1_KeyPress(KeyAscii As Integer)

On Local Error Resume Next

'Enter key...
If KeyAscii = vbKeyReturn Then
    KeyAscii = 0
    SendKeys "{TAB}", False
End If

End Sub
For Richie,

The first part of your coding worked fine apart from that I wanted it to work from a form with 2 text boxes on called txt1.text and txt2.text.

I tried your code above and a msgbox still ran.  What I want to do is for the program to "ask or state" something in txt1.text and then from the users input in txt2.text to assign it to a variable - the screen should be like:

(computer) What is your name?

(User)     Mark

(computer assigns Mark to the variable name)

(Computer) Hello Mark I am pleased to meet you. How old   are you?

(User)     37

(computer assigns 37 to the variable age)

(Computer) So you are 37, Well Mark Where do you live?

(user)     Swansea

(computer assigns Swansea to the variable home)

etc etc

Regards

Mark
But, do you want program would be smart enought to figure out what is typed?
Whit this approach, better usea db to store questions otherwise, we have to use one variable for ecah answer from the user! (it could be to infinity!)
Hi,

I do want to use one variable for each answer yes,

age
sex
live
nationality

etc, etc

I want to get a "feel" for assigning variables and the like.  In the future I DO want to learn how to use a DB for the questions - But that is a little way off for me for now!

Regards

Mark
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

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
Great! It worked like I hoped that it would.  

Thanks for that I will eventually get around to doing the program with a database (A while away yet though!)

Once again Thanks a lot!
Thanks for "A" grade.
No Probs
Take one piece of advice:
The code posted is hard-coded which is ugly by itself. Programs should not works like this. We have to avoid any hard-coded code whenever possible.
Cheers