Run module and local code (1 Viewer)

happyaslarry

New member
Local time
Today, 18:19
Joined
Mar 3, 2007
Messages
20
Newbie to programming needs help...

I need to run a module and a class module at the same time with the afterupdate event of a text box. The module handles the entry of percentages in the text box to make them easier and the code I wish to run updates the display of other text boxes on the same form.

Module;

Public Function MakePercent(txt As TextBox)
On Error GoTo Err_Handler
'Purpose: Divide the value by 100 if no percent sign found.
'Usage: Set the After Update property of a text box named TxtDiscount to:
' =MakePercent([txtDiscount])

If Not IsNull(txt) Then
If InStr(txt.Text, "%") = 0 Then
txt = txt / 100
End If
End If

Exit_Handler:
Exit Function

Err_Handler:
If Err.Number <> 2185 Then 'No Text property unless control has focus.
msgbox "Error " & Err.Number & " - " & Err.Description
End If
Resume Exit_Handler
End Function


This is the local code I wish to run;

Private Sub txtDiscount_AfterUpdate()
[txtGross] = [txtNet] * (1 + [cboVAT])
[txtNetDiscounted] = [txtNet] * (1 - [txtDiscount])
[txtGrossDiscounted] = [txtGross] * (1 - [txtDiscount])
[txtNetMarkup] = [txtNetDiscounted] * (1 + [txtMarkup])
[txtGrossMarkup] = [txtGrossDiscounted] * (1 + [txtMarkup])
'Me.Refresh

End Sub

I assume I can run the module from within the VBA code for the class module but I cannot figure out how.

Any help would be greatly appreciated.
 

Bat17

Registered User.
Local time
Today, 18:19
Joined
Sep 24, 2004
Messages
1,687
you have it back to front :)
you call the function from the code, but I would just combine the two in this case
Code:
Private Sub txtDiscount_AfterUpdate()
If Not IsNull(Me.txtDiscount) Then
    If InStr(Me.txtDiscount, "%") = 0 Then
        Me.txtDiscount = Me.txtDiscount / 100
    End If
End If
[txtGross] = [txtNet] * (1 + [cboVAT])
[txtNetDiscounted] = [txtNet] * (1 - [txtDiscount])
[txtGrossDiscounted] = [txtGross] * (1 - [txtDiscount])
[txtNetMarkup] = [txtNetDiscounted] * (1 + [txtMarkup])
[txtGrossMarkup] = [txtGrossDiscounted] * (1 + [txtMarkup])
'Me.Refresh

End Sub

but if you are relying on the user to use th % key to show what whether it is a percentage entered or not I think I would want to add some more error trapping to make sure that you do not give away 100 time what you should!

Some simple number ranges should do it.

HTH

Peter
 

happyaslarry

New member
Local time
Today, 18:19
Joined
Mar 3, 2007
Messages
20
Thanks Peter, I copied the module from a code resource but as you say, I'd might as well put it in the class module.

Just the job, cheers:)
 

Users who are viewing this thread

Top Bottom