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

FOR EACH, NEXT not working. 2

Status
Not open for further replies.

faxpay

Programmer
Nov 30, 2004
119
US
Here is the code:

Public Sub TotalNetPay() 'This proc is called from proc PlaceDecimal.
'It is called from two different places within proc PlaceDecimal.

Dim StorNetMTD As Double
Dim StorNetQTD As Double
Dim StorNetYTD As Double

Dim GrossWageMTD As Double
Dim GrossWageQTD As Double
Dim GrossWageYTD As Double

Dim F4Textbox As TextBox

GrossWageMTD = txtWAMTMTD
GrossWageQTD = txtWAMTQTD
GrossWageYTD = txtWAMTYTD

For Each F4Textbox In frmF4AddEmpEarn



Select Case Left(F4Textbox.Name, 7)
Case "txtDate", "txtNetP", "txtWAMT"
'Do Nothing
Case Else
Select Case Right(F4Textbox.Name, 3)
Case "MTD"
StorNetMTD = StorNetMTD + CDbl(F4Textbox.Text)
Case "QTD"
StorNetQTD = StorNetQTD + CDbl(F4Textbox.Text)
Case "YTD"
StorNetYTD = StorNetYTD + CDbl(F4Textbox.Text)
End Select
End Select

Next

txtNetPayMTD.Text = GrossWageMTD - StorNetMTD
txtNetPayQTD.Text = GrossWageQTD - StorNetQTD
txtNetPayYTD.Text = GrossWageYTD - StorNetYTD

StorNetMTD = 0
StorNetQTD = 0
StorNetYTD = 0

End Sub

I am trying to loop thru all the txt boxes on a form and look at text for each txt box.

The FOR EACH loop gets hung up at the same place each time. The loop stops at a certain txt box and I get "Runtime Err 13", "Type mismatch" The text content is a number. It would appear that that is the only place it is getting hung up at. It stops at the "NEXT" statement.

I'm stuck. Any help is appreciated.

faxpay,
Tom

 
What is frmF4AddEmpEarn? I'm guessing from the prefix that it is a Form. If so, are you sure it only has Textboxes on it, and no other controls?
 

Try:
Code:
Dim cntr As Control

For Each cntr In frmF4AddEmpEarn.Controls
  If typeOf cntr Is TextBox Then
    [green]'... do your magic here[/green]

  End If

Have fun.

---- Andy
 
strongm,

FrmF4AddEmpEarn is a form. There are other controls on the form but they are labels. Does that matter?

faxpay,
Tom
 

Yes, it does matter.

Text Boxes have Text property, labels have Caption property, so you need to detect which control you deal with to state the right property.

Your statement from OP: "The text content is a number" is wrong, the text content is a text. It is NOT 1234, it IS "1234"

May I suggest:
Dim [red]dbl[/red]StorNetMTD As Double
Dim [red]dbl[/red]StorNetQTD As Double
Dim [red]dbl[/red]StorNetYTD As Double

as well as

[red]dbl[/red]GrossWageMTD = [red]Val([/red]txtWAMTMTD[red].Text)[/red]
[red]dbl[/red]GrossWageQTD = [red]Val([/red]txtWAMTQTD[red].Text)[/red]
[red]dbl[/red]GrossWageYTD = [red]Val([/red]txtWAMTYTD[red].Text)[/red]

Did my original suggestion work?


Have fun.

---- Andy
 
Yes, it does. You are enumerating all the controls on the form, but you are trying to assign them to a variable of type Textbox. Anything that isn't a textbox will cause the Type Mismatch error.

Andrzejek's post show one way of dealing with this.

 
Andy,

Your suggested code works like a charm. Also named my variables per your suggestion for good programming manners.

I have been away from programming for a few years and am just getting back into it. Thank You for the help.

faxpay,
Tom
 
strongm,

I used the suggested code from Andrzejek. Works great.

Thank You for explaining why my FOR EACH loop was not working. It always helps to understand why and not just get the fix. Thanks again.

faxpay,
Tom

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top