Set focus of a text box control on a form

reena

Registered User.
Local time
Today, 15:38
Joined
Jul 18, 2001
Messages
17
Hi,

I have a form based on the member information, which has a individual_Id has a field and primary key for that table.
I am trying to code, so that if a ID is already present in the table, give user a message box, saying that the ID already exists, enter a new ID. And again it has to focus on the control txt_Individual ID, so that they know they have to enter the ID without moving on.

The problem with my code is it gives the error message, but focuses to the next control instead of the control I said to focus on.

I am just going nuts.

I have the code in the before update event of the
txtIndividual_ID:

My code is:

Private Sub txtIndividual_ID_AfterUpdate()

Dim db As Database
Dim rs As Recordset
Dim Ssql As String
Dim selectedid As Integer
If Not IsNull(Me.txtIndividual_ID) And Me.txtIndividual_ID <> 0 Then
selectedid = Me.txtIndividual_ID
Else

MsgBox "There is no such Individual_Id ", vbCritical

Me.txtIndividual_ID = 0
Me.txtIndividual_ID.SetFocus
Exit Sub
End If
Set db = CurrentDb
Ssql = " SELECT Individual_id FROM tblMemberInformation WHERE" _
& " Individual_id IN(" & selectedid & ")"
Set rs = db.OpenRecordset(Ssql, 8)
If Not rs.EOF Then
MsgBox "This Individual_id already exist Please enter a different ID", vbCritical

Me.txtIndividual_ID = 0

Me.txtIndividual_ID.SetFocus
Exit Sub
End If
Me.LstCommitee.Requery
rs.Close
Set rs = Nothing

End Sub


I did all the possible changes I could do, But it did'nt help me.

Thanks alot in advance

Regards
 
Hi Reena,

You could either move all the code onto the On_Exit event of the control and make use of Cancel=True to stop the focus moving away or you could create a form level variable ( eg blnIdOkay as boolean ) and toggle it once the entry is okay. Then check for it on_exit eg. If not blnIdOkay then
Cancel=True
end if

HTH

Drew
 

Users who are viewing this thread

Back
Top Bottom