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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i'm having problems with lostfocus

Status
Not open for further replies.

njpaco

Technical User
Nov 27, 2001
5
US
I'm having a problem with the lost focus. I have 3 textboxes. I just want to scan in information. The last textbox will save my information and cycle me back to the first textbox. My problem is that once I scan something invalid it will give me a error. The second time I scan the information in, it does a search and retrieves information. I don't know why. Also my criteria doesn't work correctly. My criteria is suppose to check the length of the inputted information. It works only sometimes. Can someone help me with this. Or have another way of doing this. I have encluded code below
thanks

Option Compare Database
Option Explicit

Private Sub Form_Load()
Me.Form.Textbox1 = Null
Me.Form.Textbox2 = Null
Me.Form.Textbox3 = Null
Me.Textbox1.SetFocus
End Sub

Private Sub Textbox1_LostFocus()

If Not IsNull(Textbox1) Then
If Len([Textbox1]) = 9 Then
Me.Textbox2.SetFocus
Else
MsgBox "This is an invalid Textbox1, please reenter textbox1", 48, "Error"
Me.Form.Textbox1 = Null
Me.Textbox1.SetFocus

End If
End If

End Sub

Private Sub Textbox2_LostFocus()
If Not IsNull(Textbox2) Then
If Len([Textbox2]) >= 12 Then
Me.Textbox3.SetFocus
Else
MsgBox "This is an invalid Serial Number, please reenter Serial Number", 48, "Error"
Me.Form.Textbox2 = Null
Me.Textbox2.SetFocus

End If
End If

End Sub

Private Sub Textbox3_LostFocus()
Dim dbsa As Database, rst As Variant, num As Variant

If Not IsNull(Textbox3) Then
If Len([Textbox3]) = 9 Then

Else
MsgBox "This is an invalid Serial Number, please reenter Serial Number", 48, "Error"
Me.Form.Textbox3 = Null
Me.Textbox3.SetFocus

End If
End If

If Not IsNull(Me.Form.Textbox1) And Not IsNull(Me.Form.Textbox2) And Not IsNull(Me.Form.Textbox3) Then

Set dbsa = CurrentDb
Set rst = dbsa.OpenRecordset("current_table")
rst.AddNew
rst![Textbox1] = Me.Form.Textbox1
rst![Textbox2] = Me.Form.Textbox2
rst![Textbox3] = Me.Form.Textbox3
rst.Update
rst.Close
Else
MsgBox "There is no Data to save", 48, " Error"
Me.Textbox1.SetFocus
End If

Me.Form.Textbox1 = Null
Me.Form.Textbox2 = Null
Me.Form.Textbox3 = Null

Me.Textbox1.SetFocus

End Sub

Private Sub Save_Click()

End Sub
 
I personally would create one routine to check the information to check the values on the button save click. Something like this:
Code:
Private Function DoCheck() As Boolean
    If IsNull(Me.textbox1) Or Me.textbox1 = &quot;&quot; Or Len(Textbox1)<>9 Then
       MsgBox &quot;blah blah&quot;, vbExclamation, &quot;some title&quot;
       Me.txtbox1.SetFocus
       Exit Function
    ElseIf ... Then
       MsgBox &quot;blah blah&quot;, vbExclamation, &quot;some title&quot;
       Me.textbox2.SetFocus
       Exit Function
    Else If ....
    Else
        DoCheck = True
    End If
End Function

Private Sub Save_Click()
    If DoCheck then
        'Your save routine goes here
        'some message saying success or ...
    Else
        'optional error message here
    End If
End Sub

Hope this helps,
Rewdee

 
REwdee,
I need the save function be automatic. I can't lose too much time on stopping and check and pressing. This is going to be used in high pace area. So if u have any other idea it would be appreciated
 
Still need a button. If not you run the risk of writing to the database with only partially correct data. Say for example, textbox 1 is correct but not textbox2. What's going to happen.

If you still want something after the users enters info into a textbox, use the after update event of the textbox. Something like how you had it previously but I would be very careful.

Hope this helps,
Rewdee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top