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!

SETFOCUS

Status
Not open for further replies.

vijip2001

Programmer
Oct 14, 2003
61
0
0
US
Hi All,

I need my cusor to focus on a particular column, if there is any error. I did write setfocus on that column. But the cursor would still go to the next column. Below is my code. Please tell me what is the mistake i have done here.

txtVEHICLE_ID = Trim$(txtVEHICLE_ID)

Set db = DBEngine.Workspaces(0).Databases(0)
Set rs = db.OpenRecordset("tblPermtrucks", DB_OPEN_DYNASET)
criteria = "VEHICLE_ID = '" & txtVEHICLE_ID & "'"
rs.FindFirst criteria
If rs.NoMatch Then
Exit Sub
Else
MsgBox "This vehicleid matches with another permanent truck id. Please use a different vehicleid"
txtVEHICLE_ID.SetFocus
db.Close
Exit Sub
End If

Thanks for any help,
Viji
 
Where is this code at(what event)? If you have this inserted before the event fires that takes it to the next field, then it will still go. If you copy all of that code and put it into the Validate eventhandler it will work. You need to get rid of the .SetFocus and Exit Sub. Then at the end add Cancel = True like this

Private Sub txtVEHICLE_ID_Validate(Cancel as Boolean)
txtVEHICLE_ID = Trim$(txtVEHICLE_ID)

Set db = DBEngine.Workspaces(0).Databases(0)
Set rs = db.OpenRecordset("tblPermtrucks", DB_OPEN_DYNASET)
criteria = "VEHICLE_ID = '" & txtVEHICLE_ID & "'"
rs.FindFirst criteria
If rs.NoMatch Then

Else
MsgBox "This vehicleid matches with another permanent truck id. Please use a different vehicleid"

db.Close
Cancel = True
End If
End Sub
 
I tried that and my cursor still goes to the next column if i tab out. Please tell me what is the mistake i have done. This is the code:

Private Sub txtVEHICLE_ID_AfterUpdate()

Dim criteria As String
Dim db As Database
Dim rs As Recordset
Dim value As Boolean

On Error GoTo txtVehicle_IDUpdateErr

txtVEHICLE_ID = Trim$(txtVEHICLE_ID)
txtVEHICLE_ID_Validate (value)
MsgBox "value =" & value

txtVehicle_IDUpdateErr:
GetError (Err)
Exit Sub

End Sub


Private Sub txtVEHICLE_ID_Validate(Cancel As Boolean)

Dim criteria As String
Dim db As Database
Dim rs As Recordset

txtVEHICLE_ID = Trim$(txtVEHICLE_ID)

Set db = DBEngine.Workspaces(0).Databases(0)
Set rs = db.OpenRecordset("tblPermtrucks", DB_OPEN_DYNASET)
criteria = "VEHICLE_ID = '" & txtVEHICLE_ID & "'"
rs.FindFirst criteria
If rs.NoMatch Then

Else
MsgBox "This vehicleid matches with another permanent truck id. Please use a different vehicleid"

db.Close
Cancel = True
End If
End Sub

Thanks for your help.
Viji
 
OK...you mentioned a "column". I assume you meant to say next textbox. If this isn't the case, then that's the problem. The control is holding the focus.

As far as the validate, it is called automatically when you attempt to leave a control. In order for it to fire, though, you must have the "CausesValidation" property set to True as well asthe NEXT control's CausesValidaton property set to true. The default for a new text box is true, so it's possible that someone changed it.

Also, you have an AfterUpdate method above. That's not a standard method for textboxes. I'd recommend you move the trim statement down to your Validate procedure and just get rid of the AfterUpdate method.

Let me know if that works.
 
This is what i see for the column i am talking about :

Name: txtVEHICLE_ID
Control_source: VEHICLE_ID

I have the column from the access database as the control source. I need my cursor to stay right there if i find it in the database.The AfterUpdate method is for that column.
Sorry if i have confused you.
If i take off the AfterUpdate method, the validate is not called.I don't see a "CausesValidation" property for that column. Suggestions please.

Viji
 
From the sounds of it, you're referring to an Access form and not a Visual Basic form. Is this the case?
 
OK...that makes a HUGE difference :) I haven't done much Access programming lately. You'd probably be better off going to the Access forums and ask the question there. If you browse under the Programming theres several Access related forums. There are a lot of little differences that would make the answers here cause problems.
 
macleod is right, this is the wrong forum. However, may I suggest using the BeforeUpdate event? - I think that's what you should be looking at when validating Access forms.

To try it out, make yourself a new form, place two textboxes on it (so you can test the "field-jump" behaviour). In this case our validated field is called Text0. Put the following code in a module.

Code:
Private Sub Text0_BeforeUpdate(Cancel As Integer)
    If Text0.Text <> "123" Then
        MsgBox "Not gonna do it!"
        Cancel = True 
    End If
End Sub

You can now only leave the first textbox by entering 123. You should be able to change this for your own validation criteria.

Cancel = True Stops the entire "update" chain-of-events. the cursor will NOT leave or update the field.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top