Solved

VB and communication between machine and user

Posted on 2002-07-15
14
184 Views
Last Modified: 2012-05-04
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
Comment
Question by:m_durnell
  • 6
  • 6
  • 2
14 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7155435
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
 
LVL 5

Expert Comment

by:rpai
ID: 7155446
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
 
LVL 1

Author Comment

by:m_durnell
ID: 7155489
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:rpai
ID: 7155631
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
 
LVL 1

Author Comment

by:m_durnell
ID: 7160671
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7160754
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
 
LVL 1

Author Comment

by:m_durnell
ID: 7160808
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7160843
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
 
LVL 1

Author Comment

by:m_durnell
ID: 7162587
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
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 100 total points
ID: 7162823
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
 
LVL 1

Author Comment

by:m_durnell
ID: 7163261
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7163340
Thanks for "A" grade.
0
 
LVL 1

Author Comment

by:m_durnell
ID: 7165216
No Probs
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7165291
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB 6.0 printer how to align 6 67
Adding to a VBA? 6 77
vb6 connector to mongodb 2 118
Using an encrypted  value to decrypt and display contents in vb6 9 55
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question