Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 Focus problem after validation

Status
Not open for further replies.

Raj Gupta

Programmer
Dec 27, 2022
2
IN
Hi,
I am new to this forum so it's my first post here.
I am having a problem with a program that I wrote in VB6. I am new to programming so have lots of problems. I am sharing the first problem here.
In a form, I have created two text box one with First Name and another with Last Name, finally the submit button to save the data. The problem is that when I click the command button with textbox1 as empty, it shows the error message but doesn't focus to textbox1 and jumps forward. My sample code is given below:

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Text2.SetFocus
End If
End Sub


Private Sub Text2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Command1.SetFocus
End If
End Sub

Private Sub Command1_Click()
If Len(Trim(Text2.Text)) > 0 And Len(Trim(Text1.Text)) = 0 Then
MsgBox "Text 1 is empty", vbInformation, "Error"
If response = vbOK Then
Text1.SetFocus
Cancel = True
Else
MsgBox "Program ran fine", vbInformation, "Alert"
Unload Me
End If
End If

End Sub

Is there any way to get out of this ?
 
1) Where are you assigning a value to [tt]response[/tt]?
2) Looks like your [tt]Else[/tt] is for the wrong [tt]If[/tt]

(also the Text1.SetFocus in Command1_Click needs to be BEFORE the MsgBox otherwise it won't do what you intend)
 
Raj Gupta said:
I am new to programming

I would strongly suggest (in VB6 IDE) to go to Tools - Options... and on Editor tab, check Require Variable Declaration checkbox. What it does, it puts [tt]Option Explicit[/tt] statement in any (new) code which requires you to declare any variables you use in your code. A VERY good practice.

I would also suggest to avoid default control names. If your Text1 is for First Name, rename it to txtFirstName, Text2 rename to txtLastName, etc.

Get into a good coding habits right from the start. [thumbsup2]

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Thanks for the reply. Actually the code I wrote is too large that I can't post here so the above code is just a sample that I want to get some help so it was with default control names. Anyway I tried at my own yesterday and found the solution. Again thanks for the reply and the suggestions.
 
And the solution was what? I ask because other people might come here in the future with a similar problem, find this thread through a search, and then find no solution offered.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top