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

Form change 2

Status
Not open for further replies.

kimtp

Programmer
Jun 15, 2002
310
0
0
US
I am trying to determine if any textbox has changed in a form and here is the code so far:

Private Sub FormChange()
Dim sMsg As String
Dim sTxt As TextBox
sMsg = "Data has been changed. Save the new data?"
For Each sTxt In Form1
If sTxt = True Then
'do the save stuff here
End If
Next sTxt
End Sub

Though when it gets to for each part I get a type mismatch msg.

Any help is appreciated.

Kim
 
Hi Kim,

When you use the For Each ... Next statement, you'll iterate through each of the objects in the collection you specify... In this case, you'll get each of the objects on Form1.

Because you're only interested in TextBox objects, you should use a typeof expression to select only those objects as follows...

Code:
Private Sub FormChange()
    Dim sMsg As String
    Dim obj As Object
    sMsg = "Data has been changed. Save the new data?"

    '' Iterate through all objects on Form1

    For Each obj In Form1

        '' Now decide if this object
        '' is actually a TextBox

        If TypeOf Obj Is TextBox Then

            If obj.DataChanged Then

                '' do the save stuff here

            End If

        End If

    Next obj

End Sub

Hope it helps,
Stephen
 
Don't forget to reset the DataChanged to False after you've actioned it each time:

Dim sTxt As Control
For Each sTxt In Me.Controls
If TypeOf sTxt Is TextBox Then
If sTxt.DataChanged = True Then
MsgBox "data changed in " & sTxt.Name
sTxt.DataChanged = False
End If
End If
Next sTxt


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Stars to both, thanx. This has evolved into a function since I want to pass a boolean to another form before I take any action. In one textbox, txtYear, a year is called from the database and placed into that box. Since this would create a True, in the object_change I placed bFormChange = False. However, when I go to the other form, bFormChange is True using StevieK's rendition and False when using John's. Here is the code so far:

Public Sub FormChange(ByRef bFormChange As Boolean)
Dim sTxt As Control
For Each sTxt In Me.Controls
If TypeOf sTxt Is TextBox Then
If sTxt.DataChanged = True Then
MsgBox "data changed in " & sTxt.Name
sTxt.DataChanged = False
End If
End If
Next sTxt

Each text box has this:

Private Sub txtCity_Change()
Call FormChange(bFormChange)
End Sub

Thanks.

Kim

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top