We help IT Professionals succeed at work.

VB Beginner

bylj
bylj asked
on
Medium Priority
245 Views
Last Modified: 2010-05-02
I've just started using VB. Probably a silly question....

Have a table with 5-fields, 5th being a date/time field which stores the last update date/time for every record. After bringing the field-data to txtboxes how do I check if the data in any of the txtboxes has changed?

Help....
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2014
Commented:
There are several ways.  My answer requires me to ask if the textbox controls are bound to a data control.

================================================
If the textbox controls are not bound you can try:

1. Set a boolean variable = True in the change event of a textbox.  Examine these variables to determine whether you need to update the database.  When you bring in the data from a record, set the variables = False.  If your textbox controls are a control array, create a booleam array and use the textbox Index values to access the array.

2. Store the database value in both the textbox.Text property and another variable.  Compare these values.

3. Store the database value in both the textbox.Text property and textbox.Tag property.  Compare these values.

Commented:
First, try to store all fields information for current record to variable.  For example you have 3 fields called Name, Address, IC.  Then, you declare three variable
general declaration section.
Dim name as String
Dim address as String
Dim ic as String

Then your program move to next record, then you call a procedure to do all the checking. For example

if name = txtName.text then
 'coding here..
endif

If you want to check the changes in textbox automatically, may be you can try to add in a Timer to your form.  Set its interval to 1000 (1 second).  Then, the timer will check for changes every one second.

In your timer's procedure, just call the method above.

Hope this can help you.
Good Luck ^_^

Commented:
You can also know the textbox contain was change by using 'Change' method in textbox.

CERTIFIED EXPERT
Top Expert 2014

Commented:
textbox controls do not have a change method.

textbox controls have a change event in which you may place code to execute whenever the Text property of the textbox control changes.

Commented:
Haha... sorry, i typed it wrongly.  'Change' is an event not a method. ^_^
Valliappan ANSenior Tech Consultant
CERTIFIED EXPERT

Commented:
>> I've just started using VB. Probably a silly question....

No need to worry about silly questions.

>> Have a table with 5-fields, 5th being a date/time field which stores the last update date/time for every
record. After bringing the field-data to txtboxes how do I check if the data in any of the txtboxes
has changed?

Can you explain what you need exactly? Do you want to know, if the textbox contents are changed, or the time when it was changed? I dont understand why you are specifying about a date/time field, here.

Cheers.
Wishing a Merry Christmas & Wonderful New Year.
Valliappan AN.
CERTIFIED EXPERT
Top Expert 2014

Commented:
Valliappan AN,

The application "tracks" the last changed date/time for each record.  The question leads me to beleive that the questioner needs to update this field and update the record if a change is detected.

Of course the best place to do the date/time field update is with a trigger in the database (if the database supports triggers).
Valliappan ANSenior Tech Consultant
CERTIFIED EXPERT

Commented:
aikimark,

If thats the case, I think you have listed a number of options for doing the same.

bylj,
One way is to do like this to check for changes made or not, as already suggested by aikimark. There are other ways ofcourse.

1) Whenever the text boxes are filled with data from db, if it is Modification, or reset if it is Addition, then set its Tag property to its Text, like this:

*****
Dim ctrl
For Each ctrl In Me.Controls
   'you could have coding to store other controls values also as the case may be. But listboxes etc. could be difficult, would need to use some other methods.
   If Typename(ctrl) = "TextBox" Then
          ctrl.Tag = ctrl.Text
   End If
Next
*****

2) Then before saving, just need to do this, thus you have very less coding to do your thing.

*****
Private Sub CmdOk_Click()

Dim ctrl
Dim lDataChanged As Boolean

lDataChanged = False
For Each ctrl In Me.Controls
   If Typename(ctrl) = "TextBox" Then
          If ctrl.Text <> ctrl.Tag Then
               lDataChanged = True
          End If
   End If
Next

If lDataChanged Then
     'update your date/time field
End If
End Sub
*****

My 2 cents.
Valliappan ANSenior Tech Consultant
CERTIFIED EXPERT

Commented:
BTW, had not run the code that I just now put. If you find any problems in running code pls let me know.

Commented:
byli,

Use the Change Event of the textbox control. This event    indicates the contents of a control have changed. How and when this event occurs varies with the control...

TextBox — changes the contents of the text box. Occurs when a DDE link updates data, when a user changes the text.

An example:-

Private Sub Text1_Change()

End Sub

If this event is raised then the data has changed.

Regards
Jodalvy.
CERTIFIED EXPERT
Top Expert 2014

Commented:
Jodalvy,

There is a problem with your answer, you have to know in the change event whether the change occurred because of user actions (keyboard, paste from clipboard) or from your code (setting values from the database as the user navigates through the recordset).

In order to accomplish this with the change event, you can easily do one of the following:
1. define and set a form-level variable for the textbox_change code to inspect.
Example:
Dim boolUpdatingFromDatabase As Boolean

Sub ShowFields()
  boolUpdatingFromDatabase =True
  txtField1.Text = rs!Field1name
...
  DoEvents
  boolUpdatingFromDatabase =False
End

Sub txtField1_Change()
  If boolUpdatingFromDatabase then
  Else
     'change indication/detection/action code goes here
  End If
End Sub

Note:
Without the DoEvents, you won't get desired results.

2. Use the Enabled textbox property as your flag.
Example:

Sub ShowFields()
  txtField1.Enabled = False
  txtField1.Text = rs!Field1name
...
  DoEvents
  txtField1.Enabled = True
End

Sub txtField1_Change()
  If txtField1.Enabled then
     'change indication/detection/action code goes here
  End If
End Sub
bylj---->  You have asked 57 questions at this site and only closed 39 of them, which is against our site's Guidelines.  Please click the HELP DESK link on the left regarding Member Agreement, Guidelines and the Question/Answer process.  I will be updating all your open questions with this information and monitor them for closures.  Please return as quickly as possible to complete these questions.  If special handling is needed, comment here with details.

Open today:
http://www.experts-exchange.com/jsp/qShow.jsp?ta=oracle&qid=20091636
http://www.experts-exchange.com/jsp/qShow.jsp?ta=win2k&qid=20241395
http://www.experts-exchange.com/jsp/qShow.jsp?ta=win2k&qid=20241043
http://www.experts-exchange.com/jsp/qShow.jsp?ta=dbgen&qid=20247085
http://www.experts-exchange.com/jsp/qShow.jsp?ta=msaccess&qid=20237668
http://www.experts-exchange.com/jsp/qShow.jsp?ta=oracle&qid=20088755
http://www.experts-exchange.com/jsp/qShow.jsp?ta=hardgen&qid=20228196
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20248490
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20108859
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20091635
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20088839
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20087841
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20085748
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20081510
http://www.experts-exchange.com/jsp/qShow.jsp?ta=asp&qid=20081065
http://www.experts-exchange.com/jsp/qShow.jsp?ta=msvisdev&qid=20088291
http://www.experts-exchange.com/jsp/qShow.jsp?ta=networkgen&qid=20220006
http://www.experts-exchange.com/jsp/qShow.jsp?ta=networkgen&qid=20198006

It helps you tremendously to remain active in all your open questions, provide ongoing feedback to the experts who step in to help you so that your goal can be achieved quickly, and the expert(s) awarded with points for their excellent help.

Your responsiveness to these is appreciated.

Moondancer
Community Support Moderator @ Experts Exchange
Admin notified of User neglect. Force-accepted by
Netminder
Community Support Moderator
Experts Exchange

Explore More ContentExplore courses, solutions, and other research materials related to this topic.