|
-
Apr 1st, 2006, 01:27 AM
#1
Thread Starter
Member
[RESOLVED] VB6 LostFocus
Hi all,
I'm putting together a front-end for an Access '97 db using VB6.
I'm using the LostFocus event to save data to the db. The trouble is, when the cursor is in a text box, and the user clicks a menu item, it looks like the LostFocus event is not firing properly. Specifically, the data saving procedures are not working, because it seems the recordset is no longer available.
Is there any way to finish the LostFocus before going on to the menu command? I've tried DoEvents in the menu_click event, that doesn't seem to help.
thanks, Peter
VB Code:
Public Sub txtTest_LostFocus(Index As Integer)
DoEvents
' Two lots of text boxes, one for 'testwhat' and one (not visible)
' for 'IDTest', to identify the current record
' Trim txtTest(Index), to trim, and to get rid of spaces
' in an otherwise empty box
Trim (txtTest(Index).Text)
' If no text in the box, Delete
' If text in the box, Edit-Update
If txtTest(Index).Text = "" Then
rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
txtTestID(Index).Text = ""
rstTests.Delete
Else
rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
rstTests.Edit
rstTests!testwhat = txtTest(Index).Text
rstTests.Update
End If
End Sub
-
Apr 1st, 2006, 01:51 AM
#2
Re: VB6 LostFocus
could you post the code where you are adding records, the code in post #1 is editing/deleting
If an answer to your question has been helpful, then please, Rate it!
Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.
-
Apr 1st, 2006, 02:44 AM
#3
Thread Starter
Member
Re: VB6 LostFocus
Ganesh, pretty simple code, AddNew is in the GotFocus event.
VB Code:
Public Sub txtTest_GotFocus(Index As Integer)
' If no text in the box, AddNew-Update
' If there is text in the box, move to that record, using txtTestID
If txtTest(Index).Text = "" Then
rstTests.AddNew
rstTests!ID = rstCurrent!ID
rstTests.Update
rstTests.Bookmark = rstTests.LastModified
txtTestID(Index) = rstTests!IDTest
Else
rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
End If
End Sub
Both these events (GotFocus and LostFocus) are working well, except when the user clicks on a menu item.
thanks, Peter
-
Apr 1st, 2006, 06:42 AM
#4
Thread Starter
Member
Re: VB6 LostFocus
Looking around on the net, it looks as though clicking on a menu item does not trigger a LostFocus event in the last object that had focus.
The Validate event doesn't seem to be any use either, as the Form (or menu items) do not have a CauseValidate property - and when I tried it out, it didn't work.
Any ideas welcome, Peter
-
Apr 8th, 2006, 01:09 AM
#5
Thread Starter
Member
-
Apr 8th, 2006, 05:06 AM
#6
Addicted Member
Re: VB6 LostFocus
Are you using ADODB.Recordset?
what's that then ?
rstTests.FindFirst
rstTests.Edit
Is it VBA? If all this you wrote does not work for you, why don't we try other method. Not the LOST AND GOT Focus...For that we'll need more info about the project and where you write you code - in the database or in a VB Exe project
-
Apr 8th, 2006, 05:17 AM
#7
Addicted Member
Re: VB6 LostFocus
How about calling the sub txtTest_LostFocus() in the mnu_click event? Have a boolean variable that is set to true whenever text_change event is fired. In the lostfocus event after the databse is updated set that variable to false.
In the menu_click event , if the boolean variable is true then call the lostfocus event otherwise no.
Last edited by srisa; Apr 8th, 2006 at 05:21 AM.
Reason: adding something more
-
Apr 8th, 2006, 07:30 PM
#8
Thread Starter
Member
Re: VB6 LostFocus
BestS - I'm not using ADO, but I think I might have to bite that bullet soon I'm using DAO, without the data control. I've done it that way since VB5, because the data control is next to useless. I've never got around to learning ADO, but I hear the ADO data control is more flexible than the DAO one. It is a VB EXE project, but I've found I've been able to get around most problems with some creative coding using DAO in the past.
Srisa, I think you've done it ... a simple solution, it looks like it does the trick. Because txtTest is part of a control array, I had to reference the index with the variable. Focussed is a globally-declared variable, there are only four txtTest boxes (0 to 3):
VB Code:
Private Sub txtTest_GotFocus(Index As Integer)
Focussed = Index
' If no text in the box, AddNew-Update
' If there is text in the box, move to that record, using txtTestID
If txtTest(Index).Text = "" Then
rstTests.AddNew
rstTests!ID = rstCurrent!ID
rstTests.Update
rstTests.Bookmark = rstTests.LastModified
txtTestID(Index) = rstTests!IDTest
Else
rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
End If
End Sub
Private Sub mnuReportMenu_Click()
If Not Focussed = 4 Then txtTest_LostFocus (Focussed)
End Sub
Private Sub txtTest_LostFocus(Index As Integer)
Focussed = 4
' Trim txtTest(Index), to trim, and to get rid of spaces
' in an otherwise empty box
txtTest(Index).Text = Trim(txtTest(Index).Text)
' If no text in the box, Delete
' If text in the box, Edit-Update
If txtTest(Index).Text = "" Then
rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
txtTestID(Index).Text = ""
rstTests.Delete
Else
rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
rstTests.Edit
rstTests!testwhat = txtTest(Index).Text
rstTests.Update
End If
End Sub
and that seems to be working Thankyouthankyouthankyou. Peter
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|