Solved

VB and communication between machine and user

Posted on 2002-07-15
14
181 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:rpai
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for "A" grade.
0
 
LVL 1

Author Comment

by:m_durnell
Comment Utility
No Probs
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now