Setfocus

sohailcdc

Registered User.
Local time
Today, 11:36
Joined
Sep 25, 2012
Messages
55
Hello Guru's of Access

I am totally green in Access, just trying to learn during my free time
I am trying to set focus the field, but unable to do can anyone help, where I am making mistake

Case1

I want to make ensure that unbound form make ensure the record is already exists yes or no, if yes, message "Already exists" and move back to same field

When i enter the duplicate value Message popup "Customer ID already Exists", and when i press OK button (with my assumption, curser move back to same text field, following message is appears

Run-time error '2108':
You must save the field before you execute the GoToControl action, the GotoControl method, or the SetFocus method

Message/searching is working but move back to same field is not working


Here what i wrote
Private sub txtcid_BeforeUpdate(Cancel As Integer)
If DCount ("[custid]", "[customerdetails]", "[custid]='" & me.txtcid & "'") > 0 Then
MsgBox "Customer ID already exists"
txtcid.setfocus - this highlighted line when i debug
else
MsgBox "Test Unsuccessful"
End if
End Sub
 
Change your code to this:

Code:
Private sub txtcid_BeforeUpdate(Cancel As Integer)
   If DCount ("[custid]", "[customerdetails]", "[custid]='" & me.txtcid & "'") > 0 Then
      MsgBox "Customer ID already exists" 
[B][COLOR="Red"]      Cancel = True
      Me.txtcid.Undo[/COLOR][/B]
   End If
End Sub
 
Try;
Code:
Private sub txtcid_BeforeUpdate(Cancel As Integer)
If DCount ("[custid]", "[customerdetails]", "[custid]='" & me.txtcid & "'") > 0 Then
     MsgBox "Customer ID already exists"
     Cancel = True     [COLOR="DarkGreen"]'Cancels the last action and return Cursor to field.[/COLOR]
     Exit Sub     [COLOR="DarkGreen"]'Stops any further code in this Sub routine running [/COLOR]  
else
MsgBox "Test Unsuccessful"
End if
End Sub

If you want to select all the Text in the field insert the following between the Cancel statement and the Exit Sub command;
Code:
Me.txtcid.SelLength = Len(Me.txtcid)
 

Users who are viewing this thread

Back
Top Bottom