• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

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

0
m_durnell
Asked:
m_durnell
  • 6
  • 6
  • 2
1 Solution
 
Richie_SimonettiIT OperationsCommented:
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...
0
 
rpaiCommented:
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?
0
 
m_durnellAuthor Commented:
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



0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
rpaiCommented:
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---------------------------------
0
 
m_durnellAuthor Commented:
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
0
 
Richie_SimonettiIT OperationsCommented:
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
0
 
m_durnellAuthor Commented:
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
0
 
Richie_SimonettiIT OperationsCommented:
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!)
0
 
m_durnellAuthor Commented:
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
0
 
Richie_SimonettiIT OperationsCommented:
Dim arrQ(1 To 3) As String
Dim user As String, age As String, city As String   ' and so on...

Function retStr(idx As Integer) As String
Dim s As String
Select Case idx
Case 1
    s = Replace(arrQ(idx), "<user>", user, , , vbTextCompare)
Case 2
    s = Replace(arrQ(idx), "<user>", user, , , vbTextCompare)
    s = Replace(s, "<age>", age, , , vbTextCompare)
Case 3
    s = Replace(arrQ(idx), "<city>", city, , , vbTextCompare)
End Select
retStr = s
End Function


Private Sub Form_Load()
arrQ(1) = "Hello <user> I am pleased to meet you. How old   are you?"
arrQ(2) = "So you are <age>. Well <user>, Where do you live?"
arrQ(3) = "<city> is a nice city to live!"
' and so on...
End Sub


Private Sub Text2_KeyPress(KeyAscii As Integer)
Static n As Integer
Dim s As String
If KeyAscii = vbKeyReturn Then
  With Text2
    n = n + 1
    Select Case n
    Case 1
        user = .Text
    Case 2
        age = .Text
    Case 3
        city = .Text
    End Select
    ' and so on...
    Text1.Text = retStr(n)
    .Text = ""
    .SetFocus
  End With
End If
End Sub

0
 
m_durnellAuthor Commented:
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!
0
 
Richie_SimonettiIT OperationsCommented:
Thanks for "A" grade.
0
 
m_durnellAuthor Commented:
No Probs
0
 
Richie_SimonettiIT OperationsCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now