[RESOLVED] messagebox always pop up
I have problem here.. I want to query. When the textbox is null then I got the messagebox twice. Why?
Code:
Private Sub Text2_Change()
On Error GoTo error
databasepetatable 'connect record table peta menggunakan modul syskod
tablesys.Close
tablesys.Open "Select * from peta where Kod = '" & Text2.Text & "'", consys, adOpenDynamic, adLockOptimistic
If Not tablesys.BOF And Not tablesys.EOF Then
Text10.Text = tablesys.Fields("Peta").Value
Else
MsgBox ("Tiada Rekod dijumpai")
Text2.Text = ""
Text10.Text = ""
End If
error:
End Sub
Re: messagebox always pop up
Because you are setting the textbox value again.
1. Text2 = something
2. It isn't in your database and you get msgbox
3. Now you change Text2="" and the Text2_Change event fires again and msgbox happens again.
Re: messagebox always pop up
Quote:
Originally Posted by LaVolpe
Because you are setting the textbox value again.
1. Text2 = something
2. It isn't in your database and you get msgbox
3. Now you change Text2="" and the Text2_Change event fires again and msgbox happens again.
When Text2="" this not in the database. So how to avoid the message box again
Re: messagebox always pop up
Many times when I want to change a control value but not have it execute the code, I'll set its tag to something and check for that value when the event fires. Something like this:
Code:
Private Sub Text2_Change()
If Text2.Tag = "ignore" Then Exit Sub ' < added IF statement
On Error GoTo error
databasepetatable 'connect record table peta menggunakan modul syskod
tablesys.Close
tablesys.Open "Select * from peta where Kod = '" & Text2.Text & "'", consys, adOpenDynamic, adLockOptimistic
If Not tablesys.BOF And Not tablesys.EOF Then
Text10.Text = tablesys.Fields("Peta").Value
Else
MsgBox ("Tiada Rekod dijumpai")
Text2.Tag = "ignore" ' < added Tag value
Text2.Text = ""
Text2.Tag = vbNullString ' < added Tag value
Text10.Text = ""
End If
error:
End Sub