Hi. i made a form named calculator with "calculate" button for value execution from textboxes.
the textboxes have default corresponding default values in it. the first to do is press "clear" button first so that the default values disappear.
Everytime i open the form and press the "calculate" button as first action an error comes up showing "type mismatch". what code to insert so that if "calculate" be the first to be pressed a message box will appear which says "please press clear button first".
Please see attached access sample form.
here is the code inside.
thanks.
Option Compare Database Option Explicit Private Sub Calc_button_Click() Dim Label1 As Long, Label2 As Long, Label3 As Long, Label4 As Long, Label5 As Long, Label6 As Double Label1 = IIf(Me.cat1a.Value = "", 0, Me.cat1a.Value) * IIf(Me.cat1b.Value = "", 1, Me.cat1b.Value) Label2 = IIf(Me.cat2b.Value = "", 0, Me.cat2b.Value) * IIf(Me.cat2a.Value = "", 1, Me.cat2a.Value) Label3 = IIf(Me.cat3b.Value = "", 0, Me.cat3b.Value) * IIf(Me.cat3a.Value = "", 1, Me.cat3a.Value) Label4 = IIf(Me.cat4b.Value = "", 0, Me.cat4b.Value) * IIf(Me.cat4a.Value = "", 1, Me.cat4a.Value) Label5 = IIf(Me.cat5b.Value = "", 0, Me.cat5b.Value) * IIf(Me.cat5a.Value = "", 1, Me.cat5a.Value) If Me.Prod.Value <> "" Then Label6 = Me.Prod.Value Else MsgBox "Productivity / Unit Hour ??" Exit Sub End If Me.Equal.Value = (Label1 + Label2 + Label3 + Label4 + Label5) / Label6 End Sub Private Sub Clear_button_Click() Me.cat1a.Value = "" Me.cat2a.Value = "" Me.cat3a.Value = "" Me.cat4a.Value = "" Me.cat5a.Value = "" Me.cat1b.Value = "" Me.cat2b.Value = "" Me.cat3b.Value = "" Me.cat4b.Value = "" Me.cat5b.Value = "" Me.Prod.Value = "" Me.Equal.Value = "" End Sub Sub Command16_Click() DoCmd.Close End Sub
Last edited by macky1730; 10-11-2010 at 08:59 AM.
Try
Option Explicit Sub Command16_Click() On Error GoTo err_msg DoCmd.Close Exit Sub err_msg MsgBox "Please use clear button first", vbCritical, "Error" End Sub
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Try this
Option Explicit Private Sub Calc_button_Click() Dim Label1 As Long, Label2 As Long, Label3 As Long, Label4 As Long, Label5 As Long, Label6 As Double On Error GoTo err_msg Label1 = IIf(Me.cat1a.Value = "", 0, Me.cat1a.Value) * IIf(Me.cat1b.Value = "", 1, Me.cat1b.Value) Label2 = IIf(Me.cat2b.Value = "", 0, Me.cat2b.Value) * IIf(Me.cat2a.Value = "", 1, Me.cat2a.Value) Label3 = IIf(Me.cat3b.Value = "", 0, Me.cat3b.Value) * IIf(Me.cat3a.Value = "", 1, Me.cat3a.Value) Label4 = IIf(Me.cat4b.Value = "", 0, Me.cat4b.Value) * IIf(Me.cat4a.Value = "", 1, Me.cat4a.Value) Label5 = IIf(Me.cat5b.Value = "", 0, Me.cat5b.Value) * IIf(Me.cat5a.Value = "", 1, Me.cat5a.Value) If Me.Prod.Value <> "" Then Label6 = Me.Prod.Value Else MsgBox "Productivity / Unit Hour ??" Exit Sub End If Me.Equal.Value = (Label1 + Label2 + Label3 + Label4 + Label5) / Label6 Exit Sub err_msg MsgBox "Please use clear button first", vbCritical, "Error" Exit Sub err_msg MsgBox "Please use clear button first", vbCritical, "Error" End Sub
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
thanks sir! it did work but some minor edit
final code:
Reputation addedPrivate Sub Calc_button_Click() Dim Label1 As Long, Label2 As Long, Label3 As Long, Label4 As Long, Label5 As Long, Label6 As Double On Error GoTo err_msg Label1 = IIf(Me.cat1a.Value = "", 0, Me.cat1a.Value) * IIf(Me.cat1b.Value = "", 1, Me.cat1b.Value) Label2 = IIf(Me.cat2b.Value = "", 0, Me.cat2b.Value) * IIf(Me.cat2a.Value = "", 1, Me.cat2a.Value) Label3 = IIf(Me.cat3b.Value = "", 0, Me.cat3b.Value) * IIf(Me.cat3a.Value = "", 1, Me.cat3a.Value) Label4 = IIf(Me.cat4b.Value = "", 0, Me.cat4b.Value) * IIf(Me.cat4a.Value = "", 1, Me.cat4a.Value) Label5 = IIf(Me.cat5b.Value = "", 0, Me.cat5b.Value) * IIf(Me.cat5a.Value = "", 1, Me.cat5a.Value) If Me.Prod.Value <> "" Then Label6 = Me.Prod.Value Else MsgBox "Productivity / Unit Hour ??" Exit Sub End If Me.Equal.Value = (Label1 + Label2 + Label3 + Label4 + Label5) / Label6 Exit Sub err_msg: MsgBox "Please use clear button first", vbCritical, "Error" End Sub![]()
If you are satisfied with the solution(s) provided, please mark your thread as Solved.
How to mark a thread Solved
Go to the first post
Click edit
Click Go Advanced
Just below the word Title you will see a dropdown with the word No prefix.
Change to Solved
Click Save
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks