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

Recursion preventing events from firing

Status
Not open for further replies.

Bugget

Programmer
Feb 18, 2005
16
US
I have a recursive function which works (ie I have stepped through it on multiple occasions and never seen it go into an infinite loop and the output is correct) but whenever I use it to validate user input, my code in the BeforeUpdate and AfterUpdate events will never be executed. I have isolated it to the recursion by commenting out the recursive call inside the function and having that call return a static value. Once this happens, all of the code in the other events is executed. I can achieve the same results by taking the user's input string and parsing it character by character so this isn't really a pressing question but its bothering me and I want to know if anyone else has seen a case where recursion has kept events from firing?

<Code>
Private Function FormatCurrencyResponse(strText As String, intCnt As Integer) As String
Dim NumCommas As Integer
Dim strCurrent As String

intCnt = intCnt + 1
Debug.Print intCnt

strCurrent = strText
strCurrent = Replace(strCurrent, ",", "")
strCurrent = Replace(strCurrent, "$", "")
While Left(strCurrent, 1) = "0"
strCurrent = Right(strCurrent, Len(strCurrent) - 1)
Wend
NumCommas = Left(Round(Len(strCurrent), 2), 1)
If NumCommas > 0 And Len(strCurrent) > 3 Then
strCurrent = FormatCurrencyResponse(Left(strCurrent, Len(strCurrent) - 3), intCnt) & "," & Right(strCurrent, 3)
End If

FormatCurrencyResponse = strCurrent

End Function
</code>
 
So here is an update on what I have discovered. I changed the code to the following:

Private Function FormatCurrencyResponse(strText As String) As String
Dim NumCommas As Integer
Dim strCurrent As String
Dim strChr As String
Dim strFormatted As String
Dim Multiple As Single
Dim i As Single

strCurrent = strText
strCurrent = Replace(strCurrent, ",", "")
strCurrent = Replace(strCurrent, "$", "")

strCurrent = strText
strCurrent = Replace(strCurrent, ",", "")
strCurrent = Replace(strCurrent, "$", "")

For i = 1 To Len(strCurrent)
'get the next char at the right
strChr = Right(strCurrent, 1)
'cut off the char we just got
strCurrent = Left(strCurrent, Len(strCurrent) - 1)

'check for multiples of four, if it is a multiple of four insert a comma
Multiple = CSng(Round(i / 4, 2))
Debug.Print Multiple
If InStr(Multiple, ".") = 0 Then
strFormatted = "," & strFormatted
End If
'append the next char
strFormatted = strChr & strFormatted
Next

FormatCurrencyResponse = strFormatted

End Function

The events still won't fire so I now am assuming that there is something I don't know about manipulating data in a bound field via code and events. Does anyone have any insight? Thanks!

- Tom
 
Did you try putting a DoEvents in there somewhere?
 
Yes I have put DoEvents all over this guy and it doesn't seem to help. I can step through the code and it never fires the afterupdate event, but it will save the changes/new input into the database. Could it simply be that the keypress/keyup events stop afterupdate from firing?
 
Silly question, but why not just use the supplied Format$ function in VB? It will be a lot faster.

Don't know why your recursion isn't working, probably because when you update your textbox string it will be repeadtedly calling afterupdate event and running your code all over the place. I'll bet you run out of stack space.

VBrit
 
Thats why I placed it in the onKeyUp event, it also looks nicer to the user to have the formatting happen as they type the input. AS you may notice I put an integer arguement on the function so I can see how many times it is executed and it seems to work properly.

The Format function has been a huge headache to this application over the years I am told. First off as this is a continuous form, it trys to format everything showing on the form that is inside the textbox of the same name. Secondly, I don't know of a way to deal with error handling if the Format guy fails and believe it or not, the users are not bright enough to A) read the directions B) know to hit escape on a bad entry since they didn't read the directions.

Anyways thank you for your input, anything helps at this point!

- Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top