+ Reply to Thread
Results 1 to 6 of 6

Thread: Codes to Surpass "Type Mismatch"

  1. #1
    Registered User
    Join Date
    04-13-2009
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    94

    Codes to Surpass "Type Mismatch"

    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
    Attached Files Attached Files
    Last edited by macky1730; 10-11-2010 at 08:59 AM.

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,616

    Re: Codes to Surpass "Type Mismatch"

    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)

  3. #3
    Registered User
    Join Date
    04-13-2009
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    94

    Re: Codes to Surpass "Type Mismatch"

    Quote Originally Posted by royUK View Post
    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
    Hello sir, where to insert the codes? and the error is generated when i press the "calculate" button not the "clear" button

  4. #4
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,616

    Re: Codes to Surpass "Type Mismatch"

    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)

  5. #5
    Registered User
    Join Date
    04-13-2009
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    94

    Re: Codes to Surpass "Type Mismatch"

    thanks sir! it did work but some minor edit

    final code:

    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"
    End Sub
    Reputation added

  6. #6
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,616

    Re: Codes to Surpass "Type Mismatch"

    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)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0