Results 1 to 8 of 8

Thread: [RESOLVED] VB6 LostFocus

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2004
    Location
    Northern Territory, Australia
    Posts
    61

    Resolved [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:
    1. Public Sub txtTest_LostFocus(Index As Integer)
    2.  
    3.     DoEvents
    4.    
    5.     ' Two lots of text boxes, one for 'testwhat' and one (not visible)
    6.     ' for 'IDTest', to identify the current record
    7.  
    8.     ' Trim txtTest(Index), to trim, and to get rid of spaces
    9.     ' in an otherwise empty box
    10.     Trim (txtTest(Index).Text)
    11.    
    12.     ' If no text in the box, Delete
    13.     ' If text in the box, Edit-Update
    14.     If txtTest(Index).Text = "" Then
    15.         rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
    16.         txtTestID(Index).Text = ""
    17.         rstTests.Delete
    18.     Else
    19.         rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
    20.         rstTests.Edit
    21.         rstTests!testwhat = txtTest(Index).Text
    22.         rstTests.Update
    23.     End If
    24.    
    25. End Sub

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    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.


  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2004
    Location
    Northern Territory, Australia
    Posts
    61

    Re: VB6 LostFocus

    Ganesh, pretty simple code, AddNew is in the GotFocus event.

    VB Code:
    1. Public Sub txtTest_GotFocus(Index As Integer)
    2.  
    3.     ' If no text in the box, AddNew-Update
    4.     ' If there is text in the box, move to that record, using txtTestID
    5.     If txtTest(Index).Text = "" Then
    6.         rstTests.AddNew
    7.         rstTests!ID = rstCurrent!ID
    8.         rstTests.Update
    9.         rstTests.Bookmark = rstTests.LastModified
    10.         txtTestID(Index) = rstTests!IDTest
    11.     Else
    12.         rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
    13.     End If
    14.    
    15. End Sub

    Both these events (GotFocus and LostFocus) are working well, except when the user clicks on a menu item.

    thanks, Peter

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2004
    Location
    Northern Territory, Australia
    Posts
    61

    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

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2004
    Location
    Northern Territory, Australia
    Posts
    61

    Re: VB6 LostFocus

    no ideas?

  6. #6
    Addicted Member BestS's Avatar
    Join Date
    Mar 2005
    Posts
    222

    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

  7. #7
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    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

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2004
    Location
    Northern Territory, Australia
    Posts
    61

    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:
    1. Private Sub txtTest_GotFocus(Index As Integer)
    2.  
    3.     Focussed = Index
    4.    
    5.     ' If no text in the box, AddNew-Update
    6.     ' If there is text in the box, move to that record, using txtTestID
    7.     If txtTest(Index).Text = "" Then
    8.         rstTests.AddNew
    9.         rstTests!ID = rstCurrent!ID
    10.         rstTests.Update
    11.         rstTests.Bookmark = rstTests.LastModified
    12.         txtTestID(Index) = rstTests!IDTest
    13.     Else
    14.         rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
    15.     End If
    16.    
    17. End Sub
    18.  
    19. Private Sub mnuReportMenu_Click()
    20.  
    21.     If Not Focussed = 4 Then txtTest_LostFocus (Focussed)
    22.  
    23. End Sub
    24.  
    25. Private Sub txtTest_LostFocus(Index As Integer)
    26.  
    27.     Focussed = 4
    28.    
    29.     ' Trim txtTest(Index), to trim, and to get rid of spaces
    30.     ' in an otherwise empty box
    31.     txtTest(Index).Text = Trim(txtTest(Index).Text)
    32.    
    33.     ' If no text in the box, Delete
    34.     ' If text in the box, Edit-Update
    35.     If txtTest(Index).Text = "" Then
    36.         rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
    37.         txtTestID(Index).Text = ""
    38.         rstTests.Delete
    39.     Else
    40.         rstTests.FindFirst "IDTest = " & txtTestID(Index).Text
    41.         rstTests.Edit
    42.         rstTests!testwhat = txtTest(Index).Text
    43.         rstTests.Update
    44.     End If
    45.    
    46. 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
  •  



Click Here to Expand Forum to Full Width